<?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: Gonzalo Moreno</title>
    <description>The latest articles on DEV Community by Gonzalo Moreno (@gon).</description>
    <link>https://dev.to/gon</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%2F100990%2F38a78b78-1d19-416e-8bf4-f3b765f2f58d.jpeg</url>
      <title>DEV Community: Gonzalo Moreno</title>
      <link>https://dev.to/gon</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/gon"/>
    <language>en</language>
    <item>
      <title>Easy toggleable boolean in Ruby on Rails</title>
      <dc:creator>Gonzalo Moreno</dc:creator>
      <pubDate>Tue, 21 Jul 2020 18:38:25 +0000</pubDate>
      <link>https://dev.to/gon/easy-toggleable-boolean-in-ruby-on-rails-56kd</link>
      <guid>https://dev.to/gon/easy-toggleable-boolean-in-ruby-on-rails-56kd</guid>
      <description>&lt;p&gt;There are occasions where we only have to update an attribute within a Rails model and in this case, change only one boolean from True to False or vice versa. To avoid having to enter the model form, check the checkbox, save and be redirected somewhere, it is easier if you implement a couple of lines of code to support toggle with the benefits of Ruby on Rails.&lt;/p&gt;

&lt;h2&gt;
  
  
  Scenario
&lt;/h2&gt;

&lt;p&gt;We have a model called Banner which has a boolean called published and we need to provide a easier interface to toggle that boolean from a list of banners&lt;/p&gt;

&lt;h3&gt;
  
  
  1.- Define a Controller action
&lt;/h3&gt;

&lt;p&gt;We make use of an ActiveRecord::Base method called &lt;a href="https://api.rubyonrails.org/classes/ActiveRecord/Persistence.html#method-i-toggle-21"&gt;toggle!&lt;/a&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="c1"&gt;# app/controllers/banners_controller.rb&lt;/span&gt;

&lt;span class="k"&gt;def&lt;/span&gt; &lt;span class="nf"&gt;toggle_published&lt;/span&gt;
  &lt;span class="vi"&gt;@banner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;toggle!&lt;/span&gt; &lt;span class="ss"&gt;:published&lt;/span&gt;
&lt;span class="k"&gt;end&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  2.- Define the member route to our new action
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight ruby"&gt;&lt;code&gt;&lt;span class="c1"&gt;# config/routes.rb&lt;/span&gt;

&lt;span class="c1"&gt;# Others routes&lt;/span&gt;
&lt;span class="n"&gt;resources&lt;/span&gt; &lt;span class="ss"&gt;:banners&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
  &lt;span class="n"&gt;member&lt;/span&gt; &lt;span class="k"&gt;do&lt;/span&gt;
    &lt;span class="n"&gt;post&lt;/span&gt; &lt;span class="ss"&gt;:toggle_published&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;h3&gt;
  
  
  3.- Use Rails unobtrusive javascript to update the value
&lt;/h3&gt;

&lt;p&gt;Suppose we have our index.html.erb file with a list of banners. We have to make sure of 2 things, include the helper dom_id and add a link to our new route using the remote option to do the request throught AJAX&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight erb"&gt;&lt;code&gt;# app/views/banners/index.html.erb

&lt;span class="nt"&gt;&amp;lt;tr&lt;/span&gt; &lt;span class="na"&gt;id=&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="cp"&gt;&amp;lt;%=&lt;/span&gt; &lt;span class="n"&gt;dom_id&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;banner&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;&lt;span class="s"&gt;"&lt;/span&gt;&lt;span class="nt"&gt;&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;td&lt;/span&gt; &lt;span class="na"&gt;class=&lt;/span&gt;&lt;span class="s"&gt;"published"&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;link_to&lt;/span&gt; &lt;span class="n"&gt;banner&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;published_icon&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;toggle_published_banner_path&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;banner&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="ss"&gt;remote: &lt;/span&gt;&lt;span class="kp"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;method: :post&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="ss"&gt;title: &lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="ss"&gt;:update_published&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="cp"&gt;%&amp;gt;&lt;/span&gt;
  &lt;span class="nt"&gt;&amp;lt;/td&amp;gt;&lt;/span&gt;
&lt;span class="nt"&gt;&amp;lt;/tr&amp;gt;&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h3&gt;
  
  
  4.- Update the DOM with the new content and profit (?)
&lt;/h3&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight javascript"&gt;&lt;code&gt;&lt;span class="c1"&gt;// app/views/banners/toggle_published.js.erb&lt;/span&gt;

&lt;span class="nx"&gt;$&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;#&amp;lt;%= dom_id(@banner) %&amp;gt; .published a&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;).&lt;/span&gt;&lt;span class="nx"&gt;html&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="s2"&gt;&amp;lt;%== @banner.published_icon %&amp;gt;&lt;/span&gt;&lt;span class="dl"&gt;"&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



</description>
      <category>rails</category>
      <category>toggle</category>
      <category>ajax</category>
    </item>
    <item>
      <title>Ruby + jemalloc on a micro EC2 instance</title>
      <dc:creator>Gonzalo Moreno</dc:creator>
      <pubDate>Fri, 27 Sep 2019 05:01:20 +0000</pubDate>
      <link>https://dev.to/gon/ruby-jemalloc-on-a-micro-ec2-instance-2bi1</link>
      <guid>https://dev.to/gon/ruby-jemalloc-on-a-micro-ec2-instance-2bi1</guid>
      <description>&lt;p&gt;After reading several articles about how badly it manages ruby memory, I wanted to do a test on a small application that I run with Ruby on Rails and Sidekiq to see if there are improvements in the performance of my application on a &lt;code&gt;t2.micro&lt;/code&gt; instance (only 1Gb RAM).&lt;/p&gt;

&lt;p&gt;So I got down to work and followed the simple steps to move from a Ruby that uses &lt;code&gt;glibc&lt;/code&gt; by default to Ruby with &lt;code&gt;jemalloc&lt;/code&gt;&lt;/p&gt;

&lt;p&gt;First, update and install jemalloc (instructions for Debian/Ubuntu)&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt update
&lt;span class="nv"&gt;$ &lt;/span&gt;&lt;span class="nb"&gt;sudo &lt;/span&gt;apt &lt;span class="nb"&gt;install &lt;/span&gt;libjemalloc-dev
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Then, reinstall Ruby with jemalloc as option. On my server, I have &lt;code&gt;Ruby 2.5.1&lt;/code&gt; installed, so the command I executed was the following (works with RVM too):&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ RUBY_CONFIGURE_OPTS&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="nt"&gt;--with-jemalloc&lt;/span&gt; rbenv &lt;span class="nb"&gt;install &lt;/span&gt;2.5.1 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;To ensure that your Ruby uses jemalloc, run the following command:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;$ &lt;/span&gt;ruby &lt;span class="nt"&gt;-r&lt;/span&gt; rbconfig &lt;span class="nt"&gt;-e&lt;/span&gt; &lt;span class="s2"&gt;"puts RbConfig::CONFIG['LIBS']"&lt;/span&gt;

&lt;span class="nt"&gt;-lpthread&lt;/span&gt; &lt;span class="nt"&gt;-ljemalloc&lt;/span&gt; &lt;span class="nt"&gt;-ldl&lt;/span&gt; &lt;span class="nt"&gt;-lcrypt&lt;/span&gt; &lt;span class="nt"&gt;-lm&lt;/span&gt; 
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;h2&gt;
  
  
  And the results so far…
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--Cz6tmlof--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/d5qhat2nihotsrw9659k.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--Cz6tmlof--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/d5qhat2nihotsrw9659k.png" alt="Some data from grafana" width="880" height="485"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;I expected the RAM used to decrease, but instead, the swap memory decreased&lt;/p&gt;

&lt;p&gt;&lt;a href="https://res.cloudinary.com/practicaldev/image/fetch/s--slHAJJQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/igjdapbgwfazwkwkhv9n.png" class="article-body-image-wrapper"&gt;&lt;img src="https://res.cloudinary.com/practicaldev/image/fetch/s--slHAJJQJ--/c_limit%2Cf_auto%2Cfl_progressive%2Cq_auto%2Cw_880/https://thepracticaldev.s3.amazonaws.com/i/igjdapbgwfazwkwkhv9n.png" alt="meme" width="594" height="261"&gt;&lt;/a&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  More information
&lt;/h2&gt;

&lt;p&gt;&lt;a href="https://www.joyfulbikeshedding.com/blog/2019-03-14-what-causes-ruby-memory-bloat.html"&gt;This awesome post&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://bubblin.io/blog/jemalloc"&gt;More detailed tutorial&lt;/a&gt;&lt;/p&gt;

&lt;p&gt;&lt;a href="https://www.levups.com/en/blog/2017/optimize-ruby-memory-usage-jemalloc-heroku-scalingo.html"&gt;Another tutorial&lt;/a&gt;&lt;/p&gt;

</description>
      <category>ruby</category>
      <category>jemalloc</category>
      <category>memory</category>
      <category>rails</category>
    </item>
    <item>
      <title>My shiny new personal page</title>
      <dc:creator>Gonzalo Moreno</dc:creator>
      <pubDate>Sun, 07 Oct 2018 00:21:57 +0000</pubDate>
      <link>https://dev.to/gon/my-shiny-new-personal-page-8j2</link>
      <guid>https://dev.to/gon/my-shiny-new-personal-page-8j2</guid>
      <description>&lt;p&gt;After many years of being a developer, I've decided to build my personal page the laziest way, installing Hugo, choosing some template and deploying to AWS S3.&lt;/p&gt;

&lt;p&gt;Well, this is not a tutorial at all, I only share my happiness for what has been achieved and looking for tips to improve the content or the user interface&lt;/p&gt;

&lt;p&gt;My page is &lt;a href="https://gonzalomoreno.me"&gt;gonzalomoreno.me&lt;/a&gt;, was built with &lt;a href="https://gohugo.io"&gt;Hugo&lt;/a&gt;, hosted on AWS S3 and is deployed automatically via Gitlab CI.&lt;/p&gt;

&lt;p&gt;Please share your thoughts, good or bad.&lt;/p&gt;

&lt;p&gt;Best regards to all&lt;/p&gt;

</description>
      <category>showcase</category>
      <category>porfolio</category>
      <category>hugo</category>
      <category>personalpage</category>
    </item>
  </channel>
</rss>
