\documentclass[10pt,a4paper]{article} % Packages \usepackage{fancyhdr} % For header and footer \usepackage{multicol} % Allows multicols in tables \usepackage{tabularx} % Intelligent column widths \usepackage{tabulary} % Used in header and footer \usepackage{hhline} % Border under tables \usepackage{graphicx} % For images \usepackage{xcolor} % For hex colours %\usepackage[utf8x]{inputenc} % For unicode character support \usepackage[T1]{fontenc} % Without this we get weird character replacements \usepackage{colortbl} % For coloured tables \usepackage{setspace} % For line height \usepackage{lastpage} % Needed for total page number \usepackage{seqsplit} % Splits long words. %\usepackage{opensans} % Can't make this work so far. Shame. Would be lovely. \usepackage[normalem]{ulem} % For underlining links % Most of the following are not required for the majority % of cheat sheets but are needed for some symbol support. \usepackage{amsmath} % Symbols \usepackage{MnSymbol} % Symbols \usepackage{wasysym} % Symbols %\usepackage[english,german,french,spanish,italian]{babel} % Languages % Document Info \author{Andrzej Krzywda (pawelpacana)} \pdfinfo{ /Title (rails-refactoring-recipes.pdf) /Creator (Cheatography) /Author (Andrzej Krzywda (pawelpacana)) /Subject (Rails Refactoring Recipes Cheat Sheet) } % Lengths and widths \addtolength{\textwidth}{6cm} \addtolength{\textheight}{-1cm} \addtolength{\hoffset}{-3cm} \addtolength{\voffset}{-2cm} \setlength{\tabcolsep}{0.2cm} % Space between columns \setlength{\headsep}{-12pt} % Reduce space between header and content \setlength{\headheight}{85pt} % If less, LaTeX automatically increases it \renewcommand{\footrulewidth}{0pt} % Remove footer line \renewcommand{\headrulewidth}{0pt} % Remove header line \renewcommand{\seqinsert}{\ifmmode\allowbreak\else\-\fi} % Hyphens in seqsplit % This two commands together give roughly % the right line height in the tables \renewcommand{\arraystretch}{1.3} \onehalfspacing % Commands \newcommand{\SetRowColor}[1]{\noalign{\gdef\RowColorName{#1}}\rowcolor{\RowColorName}} % Shortcut for row colour \newcommand{\mymulticolumn}[3]{\multicolumn{#1}{>{\columncolor{\RowColorName}}#2}{#3}} % For coloured multi-cols \newcolumntype{x}[1]{>{\raggedright}p{#1}} % New column types for ragged-right paragraph columns \newcommand{\tn}{\tabularnewline} % Required as custom column type in use % Font and Colours \definecolor{HeadBackground}{HTML}{333333} \definecolor{FootBackground}{HTML}{666666} \definecolor{TextColor}{HTML}{333333} \definecolor{DarkBackground}{HTML}{1ED4C3} \definecolor{LightBackground}{HTML}{F0FCFB} \renewcommand{\familydefault}{\sfdefault} \color{TextColor} % Header and Footer \pagestyle{fancy} \fancyhead{} % Set header to blank \fancyfoot{} % Set footer to blank \fancyhead[L]{ \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{C} \SetRowColor{DarkBackground} \vspace{-7pt} {\parbox{\dimexpr\textwidth-2\fboxsep\relax}{\noindent \hspace*{-6pt}\includegraphics[width=5.8cm]{/web/www.cheatography.com/public/images/cheatography_logo.pdf}} } \end{tabulary} \columnbreak \begin{tabulary}{11cm}{L} \vspace{-2pt}\large{\bf{\textcolor{DarkBackground}{\textrm{Rails Refactoring Recipes Cheat Sheet}}}} \\ \normalsize{by \textcolor{DarkBackground}{Andrzej Krzywda (pawelpacana)} via \textcolor{DarkBackground}{\uline{cheatography.com/20181/cs/3027/}}} \end{tabulary} \end{multicols}} \fancyfoot[L]{ \footnotesize \noindent \begin{multicols}{3} \begin{tabulary}{5.8cm}{LL} \SetRowColor{FootBackground} \mymulticolumn{2}{p{5.377cm}}{\bf\textcolor{white}{Cheatographer}} \\ \vspace{-2pt}Andrzej Krzywda (pawelpacana) \\ \uline{cheatography.com/pawelpacana} \\ \uline{\seqsplit{rails-refactoring}.com/recipes} \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Cheat Sheet}} \\ \vspace{-2pt}Published 11th December, 2014.\\ Updated 13th May, 2016.\\ Page {\thepage} of \pageref{LastPage}. \end{tabulary} \vfill \columnbreak \begin{tabulary}{5.8cm}{L} \SetRowColor{FootBackground} \mymulticolumn{1}{p{5.377cm}}{\bf\textcolor{white}{Sponsor}} \\ \SetRowColor{white} \vspace{-5pt} %\includegraphics[width=48px,height=48px]{dave.jpeg} Measure your website readability!\\ www.readability-score.com \end{tabulary} \end{multicols}} \begin{document} \raggedright \raggedcolumns % Set font size to small. Switch to any value % from this page to resize cheat sheet text: % www.emerson.emory.edu/services/latex/latex_169.html \footnotesize % Small font. \begin{multicols*}{2} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Explicitly render views with locals}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. Go to the view, replace all `@ivar` with `var` \newline % Row Count 1 (+ 1) 2. Do the same in all partials that are called from the view and always pass the params to partials explicitly with `render "products/form", \{product: product\}` \newline % Row Count 5 (+ 4) 3. At the end of the action add an explicit render call with a full path and the locals: `render "products/new", :locals =\textgreater{} \{product: product\}` \newline % Row Count 8 (+ 3) 4. Find all controllers that were using the views/partials that you changed and apply the same.% Row Count 10 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Extract render/redirect methods}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. Identify all `render` and `redirect` calls in your controller's actions. \newline % Row Count 2 (+ 2) 2. Extract a private method for each `render` and `redirect` call you found with descriptive name that shows your intention. \newline % Row Count 5 (+ 3) 3. Find and remove any duplicated methods you might created during this refactoring in the same controller.% Row Count 8 (+ 3) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Extract a Single Action Controller class}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. A new route declaration above the previous (first wins) \newline % Row Count 2 (+ 2) 2. Create an empty controller which inherits from the previous \newline % Row Count 4 (+ 2) 3. Copy the action content to the new controller \newline % Row Count 5 (+ 1) 4. Remove the action from the previous controller \newline % Row Count 6 (+ 1) 5. Copy the filters/methods that are used by the action to the new controller \newline % Row Count 8 (+ 2) 6. Make the new controller inherit from the `ApplicationController` \newline % Row Count 10 (+ 2) 7. Change routes to add `except: {[}:foo\_action{]}`% Row Count 11 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Extract routing constraint}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. Go to the controller, duplicate existing action method under different name. \newline % Row Count 2 (+ 2) 2. Create a constraint that can recognize which action should be called. Put it in `routes.rb` \newline % Row Count 4 (+ 2) 3. Duplicate the relevant routing rule in `routes.rb` \newline % Row Count 6 (+ 2) 4. Protect first routing rule with the constraint. \newline % Row Count 8 (+ 2) 5. Change the second routing rule so it delegates to the new controller action. If necessary, protect it with similar constraint. \newline % Row Count 11 (+ 3) 6. Remove the irrelevant code from controller actions. Make them do only one thing. \newline % Row Count 13 (+ 2) 7. (Optionally) Move the constraint(s) to separate file(s).% Row Count 15 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Extract a repository object}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. Create a class called `ProductsRepository` inside the same file as the controller \newline % Row Count 2 (+ 2) 2. Find all calls to typical \seqsplit{`Product.find/all/new/save/create`} methods in the controller \newline % Row Count 4 (+ 2) 3. Create those methods in the repo object \newline % Row Count 5 (+ 1) 4. Add a private method, called repo in the controller (possibly in the `ApplicationController`) where you instantiate the repo. \newline % Row Count 8 (+ 3) 5. Move the repository class to `app/repos/`% Row Count 9 (+ 1) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Extract a service object using the SimpleDelegator}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. Move the action definition into new class and inherit from `SimpleDelegator`. \newline % Row Count 2 (+ 2) 2. Step by step bring back controller responsibilities into the controller. \newline % Row Count 4 (+ 2) 3. Remove inheriting from `SimpleDelegator`. \newline % Row Count 5 (+ 1) 4. (Optional) Use exceptions for control flow in unhappy paths.% Row Count 7 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} \begin{tabularx}{8.4cm}{X} \SetRowColor{DarkBackground} \mymulticolumn{1}{x{8.4cm}}{\bf\textcolor{white}{Extract an adapter object}} \tn \SetRowColor{white} \mymulticolumn{1}{x{8.4cm}}{1. Extract external library code to private methods of your controller \newline % Row Count 2 (+ 2) 2. Parametrize these methods - remove explicit request / params / session statements \newline % Row Count 4 (+ 2) 3. Pack return values from external lib calls into simple data structures. \newline % Row Count 6 (+ 2) 4. Create an adapter class inside the same file as the controller \newline % Row Count 8 (+ 2) 5. Move newly created controller methods to adapter (one by one), replace these method calls with calls to adapter object \newline % Row Count 11 (+ 3) 6. Pack exceptions raised by an external library to your exceptions \newline % Row Count 13 (+ 2) 7. Move your adapter to another file (ex. \seqsplit{app/adapters/your\_adapter}.rb)% Row Count 15 (+ 2) } \tn \hhline{>{\arrayrulecolor{DarkBackground}}-} \end{tabularx} \par\addvspace{1.3em} % That's all folks \end{multicols*} \end{document}