<?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: Mauricio Ackermann</title>
    <description>The latest articles on DEV Community by Mauricio Ackermann (@mbackermann).</description>
    <link>https://dev.to/mbackermann</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%2F205277%2Fad7aa354-83aa-4638-bf99-fb5a6bbe5258.jpeg</url>
      <title>DEV Community: Mauricio Ackermann</title>
      <link>https://dev.to/mbackermann</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/mbackermann"/>
    <language>en</language>
    <item>
      <title>How to preview emails with ActionMailer::Preview on Rails</title>
      <dc:creator>Mauricio Ackermann</dc:creator>
      <pubDate>Tue, 14 Jul 2020 18:36:17 +0000</pubDate>
      <link>https://dev.to/mbackermann/how-to-preview-emails-with-actionmailer-preview-on-rails-4o8k</link>
      <guid>https://dev.to/mbackermann/how-to-preview-emails-with-actionmailer-preview-on-rails-4o8k</guid>
      <description>&lt;p&gt;We know how it's a pain to test ** e-mails** in our dev environment. Thinking of it, on Rails 4.1, the &lt;strong&gt;ActionMailer::Preview&lt;/strong&gt; was released, making it possible to view e-mails without actually sending them. Let's learn how to use this tool.&lt;/p&gt;

&lt;p&gt;First of all, we need to create a new mailer in our project.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails generate mailer Welcome
Running via Spring preloader in process 5087
      create  app/mailers/welcome_mailer.rb
      invoke  erb
      create    app/views/welcome_mailer
      invoke  test_unit
      create    test/mailers/welcome_mailer_test.rb
      create    test/mailers/previews/welcome_mailer_preview.rb
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's possible to see that Rails created to files inside test filter: &lt;em&gt;test/mailers/welcome_mailer_test.rb&lt;/em&gt; and &lt;em&gt;test/mailers/previews/welcome_mailer_preview.rb&lt;/em&gt;. In our case, we are going to use &lt;strong&gt;&lt;em&gt;test/mailers/previews/welcome_mailer_preview.rb&lt;/em&gt;&lt;/strong&gt; to preview our templates.&lt;/p&gt;

&lt;p&gt;Now let's create a method inside &lt;strong&gt;WelcomeMailer&lt;/strong&gt; 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 &lt;em&gt;app/mailers/welcome_mailer.rb&lt;/em&gt;&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;class&lt;/span&gt; &lt;span class="nc"&gt;WelcomeMailer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ApplicationMailer&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_welcome_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="vi"&gt;@user&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;user&lt;/span&gt;
    &lt;span class="n"&gt;mail&lt;/span&gt;&lt;span class="p"&gt;({&lt;/span&gt;
      &lt;span class="ss"&gt;to: &lt;/span&gt;&lt;span class="n"&gt;user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;email&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;from: &lt;/span&gt;&lt;span class="s1"&gt;'eu@mauricioackermann.com.br'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;
      &lt;span class="ss"&gt;subject: &lt;/span&gt;&lt;span class="s1"&gt;'Seja bem-vindo ao sistema Maurício Ackermann'&lt;/span&gt;
      &lt;span class="p"&gt;})&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;Since &lt;strong&gt;WelcomeMailer&lt;/strong&gt; is inheriting from ApplicationMailer, he will use the &lt;em&gt;mailer&lt;/em&gt; default layout&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;class&lt;/span&gt; &lt;span class="nc"&gt;ApplicationMailer&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionMailer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Base&lt;/span&gt;
  &lt;span class="n"&gt;default&lt;/span&gt; &lt;span class="ss"&gt;from: &lt;/span&gt;&lt;span class="s1"&gt;'from@example.com'&lt;/span&gt;
  &lt;span class="n"&gt;layout&lt;/span&gt; &lt;span class="s1"&gt;'mailer'&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;&lt;em&gt;app/layouts/mailer.html.erb&lt;/em&gt;&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;!DOCTYPE html&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;html&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;head&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;meta&lt;/span&gt; &lt;span class="na"&gt;http-equiv=&lt;/span&gt;&lt;span class="s"&gt;"Content-Type"&lt;/span&gt; &lt;span class="na"&gt;content=&lt;/span&gt;&lt;span class="s"&gt;"text/html; charset=utf-8"&lt;/span&gt; &lt;span class="nt"&gt;/&amp;gt;&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;style&amp;gt;&lt;/span&gt;
      &lt;span class="c"&gt;/* Email styles need to be inline */&lt;/span&gt;
    &lt;span class="nt"&gt;&amp;lt;/style&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/head&amp;gt;&lt;/span&gt;

  &lt;span class="nt"&gt;&amp;lt;body&amp;gt;&lt;/span&gt;
    &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="k"&gt;yield&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/body&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/html&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;At last, we need to create an e-mail template. Let's create a file &lt;strong&gt;&lt;em&gt;send_welcome_email.html.erb&lt;/em&gt;&lt;/strong&gt; inside the folder &lt;em&gt;app/views/welcome_mailer&lt;/em&gt;, which will be our e-mail view.&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;p&amp;gt;&lt;/span&gt;Hello, &lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="vi"&gt;@user&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;name&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;p&amp;gt;&amp;lt;strong&amp;gt;&lt;/span&gt;Thank you&lt;span class="nt"&gt;&amp;lt;/strong&amp;gt;&lt;/span&gt; for registering in our system!&lt;span class="nt"&gt;&amp;lt;/p&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we have our mailer and template done let's test it. All &lt;strong&gt;e-mail previews&lt;/strong&gt; are in &lt;em&gt;test/mailers/previews&lt;/em&gt;. Open the file created by our generator, &lt;strong&gt;&lt;em&gt;welcome_mailer_preview.html.erb&lt;/em&gt;&lt;/strong&gt;.&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;class&lt;/span&gt; &lt;span class="nc"&gt;WelcomeMailerPreview&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;&lt;/span&gt; &lt;span class="no"&gt;ActionMailer&lt;/span&gt;&lt;span class="o"&gt;::&lt;/span&gt;&lt;span class="no"&gt;Preview&lt;/span&gt;

  &lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;send_welcome_email&lt;/span&gt;
    &lt;span class="no"&gt;WelcomeMailer&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;send_welcome_email&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="no"&gt;User&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="ss"&gt;name: &lt;/span&gt;&lt;span class="s1"&gt;'Maurício Ackermann'&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;email: &lt;/span&gt;&lt;span class="s1"&gt;'eu@mauricioackermann.com.br'&lt;/span&gt;&lt;span class="p"&gt;))&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;Now to visualize the e-mail, we start the server and access the &lt;strong&gt;link&lt;/strong&gt;&lt;br&gt;
&lt;a href="http://localhost:3000/rails/mailers/welcome_mailer/send_welcome_email"&gt;http://localhost:3000/rails/mailers/welcome_mailer/send_welcome_email&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;Did you know about this Rails feature? I hope this post helps you debugging your &lt;strong&gt;projects&lt;/strong&gt;&lt;/p&gt;

</description>
      <category>rails</category>
      <category>ruby</category>
    </item>
    <item>
      <title>How and When to use Yarn on Rails?</title>
      <dc:creator>Mauricio Ackermann</dc:creator>
      <pubDate>Tue, 14 Jul 2020 06:50:28 +0000</pubDate>
      <link>https://dev.to/mbackermann/how-and-when-to-use-yarn-on-rails-3jm4</link>
      <guid>https://dev.to/mbackermann/how-and-when-to-use-yarn-on-rails-3jm4</guid>
      <description>&lt;p&gt;&lt;strong&gt;Yarn&lt;/strong&gt; is a package manager similar to NPM, which runs over &lt;strong&gt;NodeJS&lt;/strong&gt;, created and managed by Facebook, to replace NPM, making the &lt;strong&gt;package manager&lt;/strong&gt; faster, once it parallelizes the packages installation.&lt;/p&gt;

&lt;p&gt;The advantage of using it with &lt;strong&gt;Rails&lt;/strong&gt; is that you facilitate the management of CSS and Javascript libraries in your project. Its behaviour is similar to Ruby &lt;strong&gt;&lt;em&gt;gems&lt;/em&gt;&lt;/strong&gt;, but in a front-end universe.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;To install Yarn on Mac, simply run.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;brew install yarn
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now let's see how to apply it in our projects.&lt;/p&gt;

&lt;p&gt;To start, let's create a new Rails project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;rails new rails-yarn
cd rails-yarn/
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we start Yarn in our project with the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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 &lt;code&gt;package.json&lt;/code&gt;&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn init v1.5.1
question name (rails-yarn):
question version (1.0.0):
question description:
question entry point (index.js):
question repository url:
question author (Maurício Ackerman):
question license (MIT):
question private (true):
success Saved package.json
✨  Done in 8.22s.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Observe that when the command finishes running, a new file &lt;strong&gt;&lt;em&gt;package.json&lt;/em&gt;&lt;/strong&gt; gets created in your root directory. This file is responsible for all your project information, as well as its dependencies.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight json"&gt;&lt;code&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"name"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"rails-yarn"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"private"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"dependencies"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="p"&gt;{},&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"version"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"1.0.0"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"main"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"index.js"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"author"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"Mauricio Ackermann"&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt;&lt;span class="w"&gt;
  &lt;/span&gt;&lt;span class="nl"&gt;"license"&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="w"&gt; &lt;/span&gt;&lt;span class="s2"&gt;"MIT"&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;&lt;span class="w"&gt;
&lt;/span&gt;&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And now we can add dependencies to our project:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;$ yarn add jquery
yarn add v1.5.1
info No lockfile found.
[1/4] 🔍  Resolving packages...
[2/4] 🚚  Fetching packages...
[3/4] 🔗  Linking dependencies...
[4/4] 📃  Building fresh packages...
success Saved lockfile.
warning Your current version of Yarn is out of date. The latest version is "1.6.0", while you're on "1.5.1".
info To upgrade, run the following command:
$ curl -o- -L https://yarnpkg.com/install.sh | bash
success Saved 1 new dependency.
info Direct dependencies
└─ jquery@3.3.1
info All dependencies
└─ jquery@3.3.1
✨  Done in 0.78s.
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Observe that on the third line, it says: &lt;em&gt;info No lockfile found.&lt;/em&gt; and on the 8th line, we have the following output: &lt;em&gt;success Saved lockfile.&lt;/em&gt;. 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.&lt;/p&gt;

&lt;p&gt;Yarn.lock&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;# THIS IS AN AUTOGENERATED FILE. DO NOT EDIT THIS FILE DIRECTLY.
# yarn lockfile v1


jquery@^3.3.1:
  version "3.3.1"
  resolved "https://registry.yarnpkg.com/jquery/-/jquery-3.3.1.tgz#958ce29e81c9790f31be7792df5d4d95fc57fbca"
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Now that we installed the library let's add it to our project. To do so, we need to edit the file &lt;strong&gt;&lt;em&gt;app/assets/javascripts/application.js&lt;/em&gt;&lt;/strong&gt; adding jQuery.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;//= require jquery&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;And it's done! Your project has jQuery set up and running, thanks to Yarn. It's essential to add to your &lt;strong&gt;&lt;em&gt;.gitignore&lt;/em&gt;&lt;/strong&gt; the &lt;strong&gt;&lt;em&gt;node_modules&lt;/em&gt;&lt;/strong&gt; folder to avoid committing all libraries to your git repository.&lt;/p&gt;

&lt;p&gt;When you clone a repo, to install the dependencies, you just need to run:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;yarn install
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;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.&lt;/p&gt;

&lt;p&gt;In a future post, I will talk about the Webpack in Rails projects, replacing Sprockets.&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>rails</category>
      <category>yarn</category>
      <category>assets</category>
    </item>
    <item>
      <title>Apollo Server + Prisma + Mongo - Anyone with this stack?</title>
      <dc:creator>Mauricio Ackermann</dc:creator>
      <pubDate>Tue, 31 Dec 2019 01:47:16 +0000</pubDate>
      <link>https://dev.to/mbackermann/apollo-server-prisma-mongo-anyone-with-this-stack-1nm5</link>
      <guid>https://dev.to/mbackermann/apollo-server-prisma-mongo-anyone-with-this-stack-1nm5</guid>
      <description>&lt;p&gt;Anyone using this stack? I'm about to start a new GraphQL project with NodeJS and I was thinking of using this stack. Can you please share your experience and thoughts about it?&lt;/p&gt;

</description>
      <category>discuss</category>
      <category>graphql</category>
      <category>node</category>
      <category>javascript</category>
    </item>
  </channel>
</rss>
