<?xml version="1.0" encoding="utf-8" standalone="yes"?><rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:content="http://purl.org/rss/1.0/modules/content/"><channel><title>And on MoskitoHero</title><link>/tags/and/</link><description>Recent content in And on MoskitoHero</description><generator>Hugo</generator><language>en-us</language><lastBuildDate>Sat, 03 Oct 2020 00:23:33 +0000</lastBuildDate><atom:link href="/tags/and/index.xml" rel="self" type="application/rss+xml"/><item><title>An alternative to the ActiveRecord after_initialize callbacks</title><link>/post/2020-10-08-an-alternative-to-the-activerecord-after-initialize-callbacks/</link><pubDate>Sat, 03 Oct 2020 00:23:33 +0000</pubDate><guid>/post/2020-10-08-an-alternative-to-the-activerecord-after-initialize-callbacks/</guid><description>Avoiding the callback hell in Rails by creating a simple method in the model to initialize associated records.</description><content:encoded><![CDATA[<div class="paragraph">
<p>I was playing with dynamic nested forms with Stimulus today and I stumbled upon a void left by Rails&#39; ActiveRecord callbacks.</p>
</div>
<div class="paragraph">
<p>I have this Caregiver model within a nested form and I need to create it along with a User entity in my form, to be able to populate the fields. The problem is that creating an <code>after_initialize</code> callback (even with <code>:new_record?</code> will break the general behaviour of my app, and I don’t want to run through all the specs and fix FactoryBot issues etc.</p>
</div>
<div class="paragraph">
<p>So I just decided to create a small method in my Caregiver model:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># caregiver.rb</span>

<span class="k">def</span> <span class="nf">init_with_user</span>
  <span class="nb">self</span><span class="p">.</span><span class="nf">build_user</span>
  <span class="nb">self</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>Then I just need to call it in my view like this:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="erb"><span class="cp">&lt;%=</span> <span class="n">f</span><span class="p">.</span><span class="nf">fields_for</span> <span class="ss">:caregivers</span><span class="p">,</span> <span class="n">caregiver</span><span class="p">.</span><span class="nf">new</span><span class="p">.</span><span class="nf">init_with_user</span><span class="p">,</span> <span class="ss">child_index: </span><span class="s1">&#39;TEMPLATE_RECORD&#39;</span> <span class="k">do</span> <span class="o">|</span><span class="n">cf</span><span class="o">|</span> <span class="cp">%&gt;</span>
  <span class="cp">&lt;%=</span> <span class="n">render</span> <span class="s1">&#39;caregiver_fields&#39;</span><span class="p">,</span> <span class="ss">cf: </span><span class="n">cf</span> <span class="cp">%&gt;</span>
<span class="cp">&lt;%</span> <span class="k">end</span> <span class="cp">%&gt;</span></code></pre>
</div>
</div>
<div class="paragraph">
<p>This makes the code in my view simpler, more explicit, while avoiding the dreaded callback hell.</p>
</div>
]]></content:encoded></item><item><title>Chaining conditions in regular expressions</title><link>/post/2020-10-03-chaining-conditions-in-regular-expressions/</link><pubDate>Sat, 03 Oct 2020 00:23:33 +0000</pubDate><guid>/post/2020-10-03-chaining-conditions-in-regular-expressions/</guid><description>Unleash the full power of regular expressions by chaining conditions together.</description><content:encoded><![CDATA[<div class="imageblock">
<div class="content">
<img src="/assets/img/regexp.jpg" alt="Regexp hell"/>
</div>
</div>
<div class="paragraph">
<p>I recently had to make a semi-complex check on a string to validate it.</p>
</div>
<div class="paragraph">
<p>Basically, I have this model method which produces a serial number based on the ID of the object. Valid returned values for this method are <code>L00000002</code>, <code>L00000587</code> or <code>L00014522</code>. In short, they should be constituted of a capital <code>L</code> followed by 8 digits composed of the <code>id</code> of the object padded with zeros.</p>
</div>
<div class="paragraph">
<p>So I had to find a way to simply created a matcher for this to use in my model spec. I could have built two expectations, one for the length, and one for the composition. But I just decided to believe that regular expressions are powerful enough to do both at the same time. So I headed to <a href="https://rubular.com">rubular</a> and started fiddling and googling around.</p>
</div>
<div class="paragraph">
<p>The solution I came up with was the following (with an <code>id</code> of <code>12</code>, for instance):</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="regexp">/^L(?=[0-9]{8}$)0*12$/</code></pre>
</div>
</div>
<div class="paragraph">
<p>What this does, is - after the starting <code>^L</code> character - create a condition matching whatever follows the <code>?=</code> inside the parentheses. If this condition is true, then the rest of the expression is read. So I just check for any digit (<code>[0-9]</code> - it could have been <code>\d</code> but readability make the first option a better fit IMHO) exacty 8 times (<code>{8}</code>) and then the end of the string with <code>$</code>. If this is matched, then I check any number of zeros (<code>0*</code>) followed by the id (<code>12</code>) and the end of the string (<code>$</code>).</p>
</div>
<div class="paragraph">
<p>Now I could use this expression in my spec, using the <code>Regexp.escape</code> method on my model’s <code>id</code> within a string interpolation:</p>
</div>
<div class="listingblock">
<div class="content">
<pre class="rouge highlight"><code data-lang="ruby"><span class="c1"># spec/models/my_model_spec.rb</span>
<span class="n">describe</span> <span class="s2">&#34;serial_number&#34;</span> <span class="k">do</span>
  <span class="n">let</span><span class="p">(</span><span class="ss">:my_model</span><span class="p">)</span> <span class="p">{</span> <span class="n">create</span><span class="p">(</span><span class="ss">:my_model</span><span class="p">)</span> <span class="p">}</span> <span class="c1"># I use the FactoryBot gem</span>

  <span class="n">it</span> <span class="s2">&#34;returns a valid serial number&#34;</span> <span class="k">do</span>
    <span class="n">expect</span><span class="p">(</span><span class="n">my_model</span><span class="p">.</span><span class="nf">serial_number</span><span class="p">).</span><span class="nf">to</span> <span class="n">match</span><span class="p">(</span><span class="sr">/^L(?=[0-9]{8}$)0*</span><span class="si">#{</span><span class="no">Regexp</span><span class="p">.</span><span class="nf">escape</span><span class="p">(</span><span class="n">my_model</span><span class="p">.</span><span class="nf">id</span><span class="p">.</span><span class="nf">to_s</span><span class="p">)</span><span class="si">}</span><span class="sr">$/</span><span class="p">)</span>
  <span class="k">end</span>
<span class="k">end</span></code></pre>
</div>
</div>
<div class="olist lowerroman">
<ol class="lowerroman" type="i">
<li>
<p>and create the corresponding method like so:</p>
</li>
</ol>
</div>
<div class="listingblock">
<div class="content">
<pre># app/models/my_model.rb
def serial_number
  &#34;L#{id.to_s.rjust(8, &#34;0&#34;)}&#34;
end</pre>
</div>
</div>
<div class="paragraph">
<p>Don’t you just love regular expressions?</p>
</div>
]]></content:encoded></item></channel></rss>