<?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: Edward Loveall</title>
    <description>The latest articles on DEV Community by Edward Loveall (@edwardloveall).</description>
    <link>https://dev.to/edwardloveall</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%2F58964%2F6a8ea484-6ae3-431d-8d1b-1cbfe8810ae6.jpg</url>
      <title>DEV Community: Edward Loveall</title>
      <link>https://dev.to/edwardloveall</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/edwardloveall"/>
    <language>en</language>
    <item>
      <title>The Rails presence Method</title>
      <dc:creator>Edward Loveall</dc:creator>
      <pubDate>Thu, 07 Jun 2018 15:30:55 +0000</pubDate>
      <link>https://dev.to/edwardloveall/the-rails-presence-method-3h46</link>
      <guid>https://dev.to/edwardloveall/the-rails-presence-method-3h46</guid>
      <description>&lt;blockquote&gt;
&lt;p&gt;Note: This article was originally written at &lt;a href="https://blog.edwardloveall.com/rails-presence-method"&gt;https://blog.edwardloveall.com/rails-presence-method&lt;/a&gt;&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Rails has a convenient method that I discovered the other day: &lt;code&gt;presence&lt;/code&gt;. All it does is return itself if &lt;code&gt;present?&lt;/code&gt;. It's a pretty simple method:&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="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;presence&lt;/span&gt;
  &lt;span class="nb"&gt;self&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;present?&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The &lt;a href="http://api.rubyonrails.org/classes/Object.html#method-i-presence"&gt;documentation&lt;/a&gt; has a great example that simplifies this:&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="n"&gt;state&lt;/span&gt;   &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:state&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;   &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:state&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt;
&lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:country&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:country&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt;
&lt;span class="n"&gt;region&lt;/span&gt;  &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;state&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;country&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'US'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;to this:&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="n"&gt;region&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:state&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;presence&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="n"&gt;params&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="ss"&gt;:country&lt;/span&gt;&lt;span class="p"&gt;].&lt;/span&gt;&lt;span class="nf"&gt;presence&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'US'&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Here's another use case. Imagine your app has a page where you can search for users. There's a &lt;code&gt;show.html.erb&lt;/code&gt; template, and two partials: &lt;code&gt;user.html.erb&lt;/code&gt; and &lt;code&gt;no_results.html.erb&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Your controller will search for users and assign them to an instance variable. If no users were found, we'd rather show the &lt;code&gt;no_results&lt;/code&gt; partial instead of a blank page:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="vi"&gt;@users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;present?&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="vi"&gt;@users&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;else&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="s1"&gt;'no_results'&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="cp"&gt;&amp;lt;%&lt;/span&gt; &lt;span class="k"&gt;end&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;With &lt;code&gt;presence&lt;/code&gt; we can make this code shorter but still readable.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="vi"&gt;@users&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;presence&lt;/span&gt; &lt;span class="o"&gt;||&lt;/span&gt; &lt;span class="s1"&gt;'no_results'&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Most of the time &lt;code&gt;present?&lt;/code&gt; is probably what you're looking for, but sometimes &lt;code&gt;presence&lt;/code&gt; really cleans up your code. Keep it in mind.&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>Comparing Rails and Lucky: Partials</title>
      <dc:creator>Edward Loveall</dc:creator>
      <pubDate>Thu, 22 Feb 2018 17:51:33 +0000</pubDate>
      <link>https://dev.to/edwardloveall/comparing-rails-and-lucky-partials-49e3</link>
      <guid>https://dev.to/edwardloveall/comparing-rails-and-lucky-partials-49e3</guid>
      <description>&lt;p&gt;&lt;em&gt;Note: This article originally appeared here: &lt;a href="https://blog.edwardloveall.com/comparing-rails-and-lucky-partials" rel="noopener noreferrer"&gt;https://blog.edwardloveall.com/comparing-rails-and-lucky-partials&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;If you're a Rails developer, you've likely used partials. They're a great way of splitting up a view into many reusable parts.&lt;/p&gt;

&lt;p&gt;Consider displaying a search form:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;&lt;span class="nt"&gt;&amp;lt;nav&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"main"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;render&lt;/span&gt; &lt;span class="s2"&gt;"search"&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/nav&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;We use &lt;code&gt;render&lt;/code&gt; to insert a partial with the filename &lt;code&gt;_search.html.erb&lt;/code&gt;. In this instance, it will render an input field and a button to fetch search results. This is especially useful when we want to use this search form all over the app.&lt;/p&gt;

&lt;p&gt;&lt;a href="https://luckyframework.org" rel="noopener noreferrer"&gt;Lucky&lt;/a&gt; also has the ability to &lt;a href="https://luckyframework.org/guides/rendering-html/#extract-partials-and-shared-code" rel="noopener noreferrer"&gt;reuse parts of a page&lt;/a&gt;, but in a slightly different way. These differences make for a more streamlined and safe approach.&lt;/p&gt;

&lt;p&gt;Lucky will generate HTML programmatically rather than with templates. For example:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight crystal"&gt;&lt;code&gt;&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;content&lt;/span&gt;
  &lt;span class="n"&gt;nav&lt;/span&gt; &lt;span class="ss"&gt;class: &lt;/span&gt;&lt;span class="s2"&gt;"main"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="s2"&gt;"My Awesome App"&lt;/span&gt;

    &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="s2"&gt;"Sign In"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="no"&gt;Users&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;New&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;This gets converted into HTML. It's a fresh change of pace from flipping back and forth between regular HTML and Ruby. But rendering partials is even nicer. Partials in Lucky are called &lt;em&gt;Components&lt;/em&gt;. Here's how to use one.&lt;/p&gt;

&lt;p&gt;In &lt;code&gt;src/components/searches/search_component.cr&lt;/code&gt; you would create a module:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight crystal"&gt;&lt;code&gt;&lt;span class="k"&gt;module&lt;/span&gt; &lt;span class="nn"&gt;Searches::SearchComponent&lt;/span&gt;
  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;render_search&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;search_form&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;SearchForm&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="n"&gt;form_for&lt;/span&gt; &lt;span class="no"&gt;Searches&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;New&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
      &lt;span class="n"&gt;text_input&lt;/span&gt; &lt;span class="n"&gt;search_form&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;query&lt;/span&gt;
      &lt;span class="n"&gt;submit&lt;/span&gt; &lt;span class="s2"&gt;"Search"&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;This is the same as our &lt;code&gt;_search.html.erb&lt;/code&gt; partial in Rails. If we wanted to use this on a page, we can include our new module and call the &lt;code&gt;render_search&lt;/code&gt; method:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight crystal"&gt;&lt;code&gt;&lt;span class="kp"&gt;include&lt;/span&gt; &lt;span class="no"&gt;Searches&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;SearchComponent&lt;/span&gt;

&lt;span class="n"&gt;needs&lt;/span&gt; &lt;span class="n"&gt;search_form&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="no"&gt;SearchForm&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;content&lt;/span&gt;
  &lt;span class="n"&gt;nav&lt;/span&gt; &lt;span class="ss"&gt;class: &lt;/span&gt;&lt;span class="s2"&gt;"main"&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;h1&lt;/span&gt; &lt;span class="s2"&gt;"My Awesome App"&lt;/span&gt;
    &lt;span class="n"&gt;render_search&lt;/span&gt; &lt;span class="vi"&gt;@search_form&lt;/span&gt;

    &lt;span class="n"&gt;link&lt;/span&gt; &lt;span class="s2"&gt;"Sign In"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="no"&gt;Users&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;New&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;This looks a lot like rendering a partial in Rails and the result &lt;em&gt;is&lt;/em&gt; very similar. The big difference here is Lucky is written with Crystal which uses &lt;a href="https://en.wikipedia.org/wiki/Type_system#Static_type_checking" rel="noopener noreferrer"&gt;type checking&lt;/a&gt;. Type checking ensures that the only way to call a method is by passing all arguments.&lt;/p&gt;

&lt;p&gt;Our &lt;code&gt;render_search&lt;/code&gt; method requires a &lt;code&gt;SearchForm&lt;/code&gt; object. Because of type checking, there is no way to call that method unless we pass a &lt;code&gt;SearchForm&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;Compare this to the Rails &lt;code&gt;render&lt;/code&gt; method. We can't guarantee a &lt;code&gt;SearchForm&lt;/code&gt; will always be available to the partial. I'll bet that anyone who has worked with Rails for a year or more has experienced this pain.&lt;/p&gt;

&lt;p&gt;Lucky makes it impossible to forget these required objects. If we left out &lt;code&gt;@search_form&lt;/code&gt; above, the app won't even compile, let alone crash when running.&lt;/p&gt;

&lt;blockquote&gt;
&lt;p&gt;Put another way, to use a partial or component we need some kind of identifier. In the case of Rails, the only identifier is the file name, &lt;em&gt;not&lt;/em&gt; the objects that are used inside of it. In the case of Lucky, it's the name of the method and the objects you pass to it, &lt;em&gt;including&lt;/em&gt; the objects that are used inside. If you can't pass all the objects every time, the app will refuse to compile.&lt;/p&gt;
&lt;/blockquote&gt;

&lt;p&gt;Of course, we can (and should!) use this technique often:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;render_search(@search_form)
render_sign_in(@sign_in_form)
banner(@annoucments)
user_list(@users)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;I haven't decided on the best way to name these. They could all be &lt;code&gt;render&lt;/code&gt; or &lt;code&gt;display&lt;/code&gt; and are only differentiated by their method arguments. Or maybe they're all verbose like &lt;code&gt;render_search_form&lt;/code&gt;. Or maybe something else! I haven't settled on the best pattern for this, yet.&lt;/p&gt;

&lt;p&gt;One thing that comes up over and over again when working with Lucky is safety. As wonderful as Rails is, we can often find ourselves rendering a view with a &lt;code&gt;nil&lt;/code&gt; or incomplete object. Since Lucky has Crystal as its foundation, it's a much safer framework to work with than Rails. With safety comes benefits like fewer crashes and unwanted side-effects. But it also enables flexibility and developer confidence.&lt;/p&gt;

</description>
      <category>crystal</category>
      <category>ruby</category>
      <category>rails</category>
      <category>lucky</category>
    </item>
  </channel>
</rss>
