<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Vivek Kumar</title>
    <description>The latest articles on DEV Community by Vivek Kumar (@vivekktiwary).</description>
    <link>https://dev.to/vivekktiwary</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F289232%2Ff279f7f3-ff2e-4ccb-b099-c1af98231650.jpeg</url>
      <title>DEV Community: Vivek Kumar</title>
      <link>https://dev.to/vivekktiwary</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/vivekktiwary"/>
    <language>en</language>
    <item>
      <title>Patterns to refactor thick ActiveRecord models</title>
      <dc:creator>Vivek Kumar</dc:creator>
      <pubDate>Tue, 10 Dec 2019 11:32:47 +0000</pubDate>
      <link>https://dev.to/vivekktiwary/patterns-to-refactor-thick-activerecord-models-2d5</link>
      <guid>https://dev.to/vivekktiwary/patterns-to-refactor-thick-activerecord-models-2d5</guid>
      <description>&lt;p&gt;There are several ways to refactor your thick &lt;code&gt;active record&lt;/code&gt; models, but in this post, we will be talking only about one pattern which has helped me a lot in taking out responsibilities from the model.&lt;/p&gt;

&lt;p&gt;This pattern is known as the &lt;a href="https://en.wikipedia.org/wiki/Decorator_pattern"&gt;Decorator Pattern&lt;/a&gt;.&lt;/p&gt;

&lt;p&gt;In simple terms, this pattern is about adding responsibility to an object at runtime.&lt;/p&gt;

&lt;p&gt;Let's see an example to understand this pattern in Rails context.&lt;/p&gt;

&lt;p&gt;Suppose you have a &lt;code&gt;project&lt;/code&gt; model with different types of projects like &lt;code&gt;in-house&lt;/code&gt; projects and &lt;code&gt;third-party&lt;/code&gt; projects. A project can be started if certain conditions are met, otherwise it should not start and the condition for starting a project depends on its type.&lt;/p&gt;

&lt;p&gt;How would we go about writing this logic?&lt;/p&gt;

&lt;p&gt;One approach is to define a &lt;code&gt;start&lt;/code&gt; method in the &lt;code&gt;project&lt;/code&gt; model and check the type of project and then make a decision whether to start the project or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# project.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;Project&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationRecord&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;start&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'in-house'&lt;/span&gt;
      &lt;span class="c1"&gt;# do checking whether the project can be started or not for an in-house project&lt;/span&gt;
    &lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'third-party'&lt;/span&gt;
      &lt;span class="c1"&gt;# do checking whether the project can be started or not for third-party project&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;But there is one problem with this approach, it will violate the &lt;code&gt;open-closed&lt;/code&gt; principle when new project types will come. Moreover, it will also make the &lt;code&gt;project&lt;/code&gt; model thick with more project types.&lt;/p&gt;

&lt;p&gt;So what can we do about it?&lt;/p&gt;

&lt;p&gt;We can move the logic for the start method to another object and just wrap the &lt;code&gt;project&lt;/code&gt; object with this object to check whether the project can be started or not.&lt;/p&gt;

&lt;p&gt;This pattern will not solve the &lt;code&gt;open-closed&lt;/code&gt; violation(more on that in a later post...) but it will help in making the project model a bit thin.&lt;/p&gt;

&lt;p&gt;To do that first we have to define a &lt;code&gt;decorate&lt;/code&gt; method in the &lt;code&gt;project&lt;/code&gt; model.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# project.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;decorate&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;klass&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="no"&gt;ProjectDecorator&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
  &lt;span class="n"&gt;klass&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;new&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nb"&gt;self&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And then, we would obtain the decorated project object by calling this method from wherever we wanted to check whether we can start the project or not.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# project_start_service.rb&lt;/span&gt;
&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;execute&lt;/span&gt;
  &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;start&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;project&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;decorate&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;can_start?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;

&lt;span class="c1"&gt;#project_decorator.rb&lt;/span&gt;
&lt;span class="k"&gt;class&lt;/span&gt; &lt;span class="nc"&gt;ProjectDecorator&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;BaseDecorator&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;can_start?&lt;/span&gt;
    &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'in-house'&lt;/span&gt;
      &lt;span class="c1"&gt;# check start constraint for in-house project&lt;/span&gt;
    &lt;span class="k"&gt;elsif&lt;/span&gt; &lt;span class="n"&gt;type&lt;/span&gt; &lt;span class="o"&gt;==&lt;/span&gt; &lt;span class="s1"&gt;'third-party'&lt;/span&gt;
      &lt;span class="c1"&gt;# check start constraint for third-party project&lt;/span&gt;
    &lt;span class="k"&gt;end&lt;/span&gt;
  &lt;span class="k"&gt;end&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;In the above example, we are checking whether we can start the project or not from the &lt;code&gt;project_start_service.rb&lt;/code&gt; by calling &lt;code&gt;project.decorate.can_start?&lt;/code&gt;, the &lt;code&gt;can_start?&lt;/code&gt; method is defined on the &lt;code&gt;project decorator&lt;/code&gt; and contains the logic of checking whether to start the project or not.&lt;/p&gt;

&lt;p&gt;With the above pattern, we would be able to move out related responsibilities out of our model. This would improve the &lt;code&gt;readability&lt;/code&gt; and these small decorator objects will have only a single well defined responsibilities.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>activerecord</category>
      <category>ruby</category>
      <category>decorators</category>
    </item>
  </channel>
</rss>
