<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0"><channel><title><![CDATA[Mauricio Ackermann]]></title><description><![CDATA[A blog about Ruby on Rails, Javascript, React, productivity and other hyped technologies.]]></description><link>https://mauricioackermann.com</link><image><url>https://cdn.hashnode.com/res/hashnode/image/upload/v1626546309172/XFyYKjt8m.png</url><title>Mauricio Ackermann</title><link>https://mauricioackermann.com</link></image><generator>RSS for Node</generator><lastBuildDate>Thu, 16 Apr 2026 18:09:56 GMT</lastBuildDate><atom:link href="https://mauricioackermann.com/rss.xml" rel="self" type="application/rss+xml"/><language><![CDATA[en]]></language><ttl>60</ttl><item><title><![CDATA[How to preview emails with ActionMailer::Preview on Rails]]></title><description><![CDATA[We know how it's a pain to test  e-mails in our dev environment. Thinking of it, on Rails 4.1, the ActionMailer::Preview was released, making it possible to view e-mails without actually sending them. Let's learn how to use this tool.
First of all, w...]]></description><link>https://mauricioackermann.com/how-to-preview-emails-with-actionmailerpreview-on-rails</link><guid isPermaLink="true">https://mauricioackermann.com/how-to-preview-emails-with-actionmailerpreview-on-rails</guid><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[Rails]]></category><dc:creator><![CDATA[Mauricio Ackermann]]></dc:creator><pubDate>Tue, 14 Jul 2020 17:28:46 GMT</pubDate><content:encoded><![CDATA[<p>We know how it&#39;s a pain to test <strong> e-mails</strong> in our dev environment. Thinking of it, on Rails 4.1, the <strong>ActionMailer::Preview</strong> was released, making it possible to view e-mails without actually sending them. Let&#39;s learn how to use this tool.</p>
<p>First of all, we need to create a new mailer in our project.</p>
<pre><code>rails generate mailer Welcome
Running via Spring preloader in process 5087
      <span class="hljs-keyword">create</span>  app/mailers/welcome_mailer.rb
      invoke  erb
      <span class="hljs-keyword">create</span>    app/views/welcome_mailer
      invoke  test_unit
      <span class="hljs-keyword">create</span>    <span class="hljs-keyword">test</span>/mailers/welcome_mailer_test.rb
      <span class="hljs-keyword">create</span>    <span class="hljs-keyword">test</span>/mailers/previews/welcome_mailer_preview.rb
</code></pre><p>It&#39;s possible to see that Rails created to files inside test filter: _test/mailers/welcome_mailer<em>test.rb</em> and _test/mailers/previews/welcome_mailer<em>preview.rb</em>. In our case, we are going to use <strong>_test/mailers/previews/welcome_mailer<em>preview.rb</em></strong> to preview our templates.</p>
<p>Now let&#39;s create a method inside <strong>WelcomeMailer</strong> that will be responsible for sending a welcome message to users that signup in our system, assuming we have a model User with an e-mail attribute. To do that, we need to open the file _app/mailers/welcome<em>mailer.rb</em></p>
<pre><code class="lang-ruby"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WelcomeMailer</span> &lt; ApplicationMailer</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_welcome_email</span><span class="hljs-params">(user)</span></span>
    @user = user
    mail({
      <span class="hljs-symbol">to:</span> user.email,
      <span class="hljs-symbol">from:</span> <span class="hljs-string">'eu@mauricioackermann.com.br'</span>,
      <span class="hljs-symbol">subject:</span> <span class="hljs-string">'Seja bem-vindo ao sistema Maurício Ackermann'</span>
      })
  <span class="hljs-keyword">end</span>

<span class="hljs-keyword">end</span>
</code></pre>
<p>Since <strong>WelcomeMailer</strong> is inheriting from ApplicationMailer, he will use the <em>mailer</em> default layout</p>
<pre><code class="lang-ruby"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">ApplicationMailer</span> &lt; ActionMailer::Base</span>
  default <span class="hljs-symbol">from:</span> <span class="hljs-string">'from@example.com'</span>
  layout <span class="hljs-string">'mailer'</span>
<span class="hljs-keyword">end</span>
</code></pre>
<p><em>app/layouts/mailer.html.erb</em></p>
<pre><code class="lang-erb">&lt;!DOCTYPE html&gt;
&lt;html&gt;
  &lt;head&gt;
    &lt;meta http-equiv="Content-Type" content="text/html; charset=utf-8" /&gt;
    &lt;style&gt;
      /* Email styles need to be inline */
    &lt;/style&gt;
  &lt;/head&gt;

  &lt;body&gt;
    &lt;%= yield %&gt;
  &lt;/body&gt;
&lt;/html&gt;
</code></pre>
<p>At last, we need to create an e-mail template. Let&#39;s create a file <strong><em>send_welcome_email.html.erb</em></strong> inside the folder _app/views/welcome<em>mailer</em>, which will be our e-mail view.</p>
<pre><code class="lang-erb">&lt;p&gt;Hello, &lt;%= @user.name %&gt;&lt;/p&gt;
&lt;p&gt;&lt;strong&gt;Thank you&lt;/strong&gt; for registering in our system!&lt;/p&gt;
</code></pre>
<p>Now that we have our mailer and template done let&#39;s test it. All <strong>e-mail previews</strong> are in <em>test/mailers/previews</em>. Open the file created by our generator, <strong>_welcome_mailer<em>preview.html.erb</em></strong>.</p>
<pre><code class="lang-ruby"><span class="hljs-class"><span class="hljs-keyword">class</span> <span class="hljs-title">WelcomeMailerPreview</span> &lt; ActionMailer::Preview</span>

  <span class="hljs-function"><span class="hljs-keyword">def</span> <span class="hljs-title">send_welcome_email</span></span>
    WelcomeMailer.send_welcome_email(User.new(<span class="hljs-symbol">name:</span> <span class="hljs-string">'Maurício Ackermann'</span>, <span class="hljs-symbol">email:</span> <span class="hljs-string">'eu@mauricioackermann.com.br'</span>))
  <span class="hljs-keyword">end</span>

<span class="hljs-keyword">end</span>
</code></pre>
<p>Now to visualize the e-mail, we start the server and access the <strong>link</strong>
http://localhost:3000/rails/mailers/welcome_mailer/send_welcome_email</p>
<p>Did you know about this Rails feature? I hope this post helps you debugging your <strong>projects</strong></p>
]]></content:encoded></item><item><title><![CDATA[How and When to use Yarn on Rails?]]></title><description><![CDATA[Yarn is a package manager similar to NPM, which runs over NodeJS, created and managed by Facebook, to replace NPM, making the package manager faster, once it parallelizes the packages installation.
The advantage of using it with Rails is that you fac...]]></description><link>https://mauricioackermann.com/how-and-when-to-use-yarn-on-rails</link><guid isPermaLink="true">https://mauricioackermann.com/how-and-when-to-use-yarn-on-rails</guid><category><![CDATA[Ruby on Rails]]></category><category><![CDATA[Yarn]]></category><dc:creator><![CDATA[Mauricio Ackermann]]></dc:creator><pubDate>Tue, 14 Jul 2020 05:57:50 GMT</pubDate><content:encoded><![CDATA[<p><strong>Yarn</strong> is a package manager similar to NPM, which runs over <strong>NodeJS</strong>, created and managed by Facebook, to replace NPM, making the <strong>package manager</strong> faster, once it parallelizes the packages installation.</p>
<p>The advantage of using it with <strong>Rails</strong> is that you facilitate the management of CSS and Javascript libraries in your project. Its behaviour is similar to Ruby <strong><em>gems</em></strong>, but in a front-end universe.</p>
<p>On Rails, it's common to search for gems that make more comfortable the use of those libraries in our projects, but we become dependent on updates from their developers. Yarn is the right solution for all these problems.</p>
<p>To install Yarn on Mac, simply run.</p>
<pre><code><span class="hljs-attribute">brew</span> install yarn
</code></pre><p>Now let's see how to apply it in our projects.</p>
<p>To start, let's create a new Rails project:</p>
<pre><code>rails <span class="hljs-built_in">new</span> rails-yarn
cd rails-yarn/
</code></pre><p>And now we start Yarn in our project with the following command:</p>
<pre><code>yarn <span class="hljs-keyword">init</span>
</code></pre><p>Your bash is going to make a bunch of questions about your project. You don't need to answer any of them if you don't want and you can always change it later in your <code>package.json</code></p>
<pre><code><span class="hljs-selector-tag">yarn</span> <span class="hljs-selector-tag">init</span> <span class="hljs-selector-tag">v1</span><span class="hljs-selector-class">.5</span><span class="hljs-selector-class">.1</span>
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">name</span> (<span class="hljs-selector-tag">rails-yarn</span>):
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">version</span> (1<span class="hljs-selector-class">.0</span><span class="hljs-selector-class">.0</span>):
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">description</span>:
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">entry</span> <span class="hljs-selector-tag">point</span> (<span class="hljs-selector-tag">index</span><span class="hljs-selector-class">.js</span>):
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">repository</span> <span class="hljs-selector-tag">url</span>:
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">author</span> (<span class="hljs-selector-tag">Maur</span>í<span class="hljs-selector-tag">cio</span> <span class="hljs-selector-tag">Ackerman</span>):
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">license</span> (<span class="hljs-selector-tag">MIT</span>):
<span class="hljs-selector-tag">question</span> <span class="hljs-selector-tag">private</span> (<span class="hljs-selector-tag">true</span>):
<span class="hljs-selector-tag">success</span> <span class="hljs-selector-tag">Saved</span> <span class="hljs-selector-tag">package</span><span class="hljs-selector-class">.json</span>
✨  <span class="hljs-selector-tag">Done</span> <span class="hljs-selector-tag">in</span> 8<span class="hljs-selector-class">.22s</span>.
</code></pre><p>Observe that when the command finishes running, a new file <strong><em>package.json</em></strong> gets created in your root directory. This file is responsible for all your project information, as well as its dependencies.</p>
<pre><code class="lang-json">{
  <span class="hljs-attr">"name"</span>: <span class="hljs-string">"rails-yarn"</span>,
  <span class="hljs-attr">"private"</span>: <span class="hljs-literal">true</span>,
  <span class="hljs-attr">"dependencies"</span>: {},
  <span class="hljs-attr">"version"</span>: <span class="hljs-string">"1.0.0"</span>,
  <span class="hljs-attr">"main"</span>: <span class="hljs-string">"index.js"</span>,
  <span class="hljs-attr">"author"</span>: <span class="hljs-string">"Mauricio Ackermann"</span>,
  <span class="hljs-attr">"license"</span>: <span class="hljs-string">"MIT"</span>
}
</code></pre>
<p>And now we can add dependencies to our project:</p>
<pre><code><span class="hljs-string">$</span> <span class="hljs-string">yarn</span> <span class="hljs-string">add</span> <span class="hljs-string">jquery</span>
<span class="hljs-string">yarn</span> <span class="hljs-string">add</span> <span class="hljs-string">v1.5.1</span>
<span class="hljs-string">info</span> <span class="hljs-literal">No</span> <span class="hljs-string">lockfile</span> <span class="hljs-string">found.</span>
[<span class="hljs-number">1</span><span class="hljs-string">/4</span>] <span class="hljs-string">🔍</span>  <span class="hljs-string">Resolving</span> <span class="hljs-string">packages...</span>
[<span class="hljs-number">2</span><span class="hljs-string">/4</span>] <span class="hljs-string">🚚</span>  <span class="hljs-string">Fetching</span> <span class="hljs-string">packages...</span>
[<span class="hljs-number">3</span><span class="hljs-string">/4</span>] <span class="hljs-string">🔗</span>  <span class="hljs-string">Linking</span> <span class="hljs-string">dependencies...</span>
[<span class="hljs-number">4</span><span class="hljs-string">/4</span>] <span class="hljs-string">📃</span>  <span class="hljs-string">Building</span> <span class="hljs-string">fresh</span> <span class="hljs-string">packages...</span>
<span class="hljs-string">success</span> <span class="hljs-string">Saved</span> <span class="hljs-string">lockfile.</span>
<span class="hljs-string">warning</span> <span class="hljs-string">Your</span> <span class="hljs-string">current</span> <span class="hljs-string">version</span> <span class="hljs-string">of</span> <span class="hljs-string">Yarn</span> <span class="hljs-string">is</span> <span class="hljs-string">out</span> <span class="hljs-string">of</span> <span class="hljs-string">date.</span> <span class="hljs-string">The</span> <span class="hljs-string">latest</span> <span class="hljs-string">version</span> <span class="hljs-string">is</span> <span class="hljs-string">"1.6.0"</span><span class="hljs-string">,</span> <span class="hljs-string">while</span> <span class="hljs-string">you're</span> <span class="hljs-string">on</span> <span class="hljs-string">"1.5.1"</span><span class="hljs-string">.</span>
<span class="hljs-string">info</span> <span class="hljs-string">To</span> <span class="hljs-string">upgrade,</span> <span class="hljs-attr">run the following command:</span>
<span class="hljs-string">$</span> <span class="hljs-string">curl</span> <span class="hljs-string">-o-</span> <span class="hljs-string">-L</span> <span class="hljs-string">https://yarnpkg.com/install.sh</span> <span class="hljs-string">|</span> <span class="hljs-string">bash</span>
<span class="hljs-string">success</span> <span class="hljs-string">Saved</span> <span class="hljs-number">1</span> <span class="hljs-string">new</span> <span class="hljs-string">dependency.</span>
<span class="hljs-string">info</span> <span class="hljs-string">Direct</span> <span class="hljs-string">dependencies</span>
<span class="hljs-string">└─</span> <span class="hljs-string">jquery@3.3.1</span>
<span class="hljs-string">info</span> <span class="hljs-string">All</span> <span class="hljs-string">dependencies</span>
<span class="hljs-string">└─</span> <span class="hljs-string">jquery@3.3.1</span>
<span class="hljs-string">✨</span>  <span class="hljs-string">Done</span> <span class="hljs-string">in</span> <span class="hljs-number">0.</span><span class="hljs-string">78s.</span>
</code></pre><p>Observe that on the third line, it says: <em>info No lockfile found.</em> and on the 8th line, we have the following output: <em>success Saved lockfile.</em>. Yarn uses a file called yarn.lock to save information about all dependencies versions, similar to what we have on our Ruby Gemfile.lock, which assures consistency between all our ho, avoiding different versions on different environments.</p>
<p>Yarn.lock</p>
<pre><code><span class="hljs-comment"># THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.</span>
<span class="hljs-comment"># yarn lockfile v1</span>


<span class="hljs-attribute">jquery</span>@^<span class="hljs-number">3</span>.<span class="hljs-number">3</span>.<span class="hljs-number">1</span>:
  <span class="hljs-attribute">version</span> <span class="hljs-string">"3.3.1"</span>
  <span class="hljs-attribute">resolved</span> <span class="hljs-string">"https://registry.yarnpkg.com/jquery/-/jquery-3.3.1.tgz#958ce29e81c9790f31be7792df5d4d95fc57fbca"</span>
</code></pre><p>Now that we installed the library let's add it to our project. To do so, we need to edit the file <strong><em>app/assets/javascripts/application.js</em></strong> adding jQuery.</p>
<pre><code class="lang-javascript"><span class="hljs-comment">//= require jquery</span>
</code></pre>
<p>And it's done! Your project has jQuery set up and running, thanks to Yarn. It's essential to add to your <strong><em>.gitignore</em></strong> the <strong><em>node_modules</em></strong> folder to avoid committing all libraries to your git repository.</p>
<p>When you clone a repo, to install the dependencies, you just need to run:</p>
<pre><code><span class="hljs-attribute">yarn</span> install
</code></pre><p>What do you think about this alternative to manage your libraries? Simple and fast, not to mention that it is also a best practice. Leave your comments and thoughts about how Yarn may help your project.</p>
<p>In a future post, I will talk about the Webpack in Rails projects, replacing Sprockets.</p>
]]></content:encoded></item></channel></rss>