avatarCatalin Ionescu

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4069

Abstract

d="cde7"><a href="https://github.com/Modern-Rails/modern-rails-crm/pull/1/commits/6366ff737122c0e08deb879c50c146220df73b50">This commit</a> summarizes the work done in this chapter.</p><h1 id="bbe5">2. Integrating Webpacker with ActiveAdmin</h1><p id="8fb0">Starting with Rails 6.0, Webpacker has become the default asset generator. We will enable using webpacker in <code>config/initializers/active_admin.rb</code> by uncommenting the following line:</p><div id="f701"><pre><span class="hljs-attr">config.use_webpacker</span> = <span class="hljs-literal">true</span></pre></div><p id="3f0c">We now need to run the generator to create the pack files:</p><div id="c896"><pre>$ bin/rails <span class="hljs-keyword">generate</span> active_admin:webpacker</pre></div><p id="96c5">At the same, let’s do some cleanup. The original <code>rails new</code> generator created a <code>hello_react.jsx</code> and the default Sprockets assets for ActiveAdmin. These are the files I deleted:</p><div id="d3a3"><pre><span class="hljs-symbol">deleted:</span> app<span class="hljs-keyword">/assets/</span>javascripts/active_admin.js <span class="hljs-symbol">deleted:</span> app<span class="hljs-keyword">/assets/</span>stylesheets/active_admin.scss <span class="hljs-symbol">deleted:</span> app<span class="hljs-keyword">/javascript/</span>packs/hello_react.jsx</pre></div><p id="9886"><a href="https://github.com/Modern-Rails/modern-rails-crm/pull/1/commits/89467058cb9890395293ca2f5f3bc71dc50f7d08">This commit</a> summarizes the work done in this chapter.</p><h1 id="01ca">3. Using a modern theme for ActiveAdmin</h1><p id="405a">I’m going to use the <a href="https://github.com/ayann/active_admin_flat_skin">Flat Skin</a> by Armand Niampa theme for Active Admin, but the process of changing the theme is similar no matter which one you pick. <a href="https://github.com/paladini/activeadmin-themes">Here’s a list</a> of some of the theme options.</p><p id="3880">It would be so easy if we could just install the gem, import the assets and be done with it. Unfortunately, the gem is importing assets through Sprockets. Since we switched to Webpacker, we should be consistent and use a single way of bundling assets to avoid any issues down the road. The easiest solution here is simply to pick the asset files and include them, by hand, in our pack files. This could potentially be a problem if the assets were to change over time, but the Flat Skin repository was last updated on September 2017.</p><figure id="eacf"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*Y7dPFjNDc9cYgYAptkedDg.png"><figcaption>Screenshot of the CRM after changing the theme</figcaption></figure><p id="c3a6">This already looks so much better. Here’s what we had to do in order to add this theme.</p><p id="b536">First of all, I copied all the assets in Flat Skin into <code>app/javascript/stylesheets</code>. The result should look like this</p><figure id="eadc"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*frGN4zhtjY73t_fO8naa6Q.png"><figcaption></figcaption></figure><p id="928c">Then, I referenced the new theme in <code>app/javascript/stylesheets/active_admin.scss</code>:</p><div id="1d39"><pre>@import <span class="hljs-string">"@activeadmin/activeadmin/src/scss/mixins"</span>; @import <span class="hljs-string">"@activeadmin/activeadmin/src/scss/base"</span>;</pre></div><div id="f7a9"><pre>+ <span class="hljs-keyword">@import</span> <span class="hljs-string">'./admin_flat_skin/active_admin_flat_skin.scss'</span>;</pre></div><p id="4818">That’s all I had to do to get the screenshot above. <a href="https://github.com/Modern-Rails/modern-rails-crm/pull/1/commits/c060d4fd12aba9c8a67b6121463f2c8a8ef4ca01">Supporting commit for this chaper</a>.</p><h1 id="946e">3.1 Bonus: Use the built-in Date Picker for the Client’s birthday</h1><p id="f167">Our Client Active Admin definition has been very thin so far. Let’s go override the default form generator to leverage the built-in Active Date Picker. ActiveAdmin offers the <code>datepicker</code>

Options

input, which uses the <a href="http://jqueryui.com/datepicker/">jQuery UI datepicker</a>. I will make the following changes in<code>app/admin/clients.rb</code></p><div id="334b"><pre>form <span class="hljs-keyword">do</span> |f| f.semantic_errors *f.<span class="hljs-keyword">object</span>.errors.keys

f.inputs <span class="hljs-keyword">do</span> f.<span class="hljs-keyword">input</span> :<span class="hljs-type">name</span> f.<span class="hljs-keyword">input</span> :phone_number f.<span class="hljs-keyword">input</span> :email f.<span class="hljs-keyword">input</span> :birthday, <span class="hljs-keyword">as</span>: :datepicker <span class="hljs-keyword">end</span>

f.actions <span class="hljs-keyword">end</span></pre></div><p id="47b3">Active Admin uses <a href="https://github.com/formtastic/formtastic">Formtastic</a> to build it’s forms. The one clear benefit of it is that <code>form.input</code> automatically detects the field type and generates the appropriate input field. For example, <code>f.input :email</code> generates an input field with type email:</p><div id="f706"><pre><input <span class="hljs-attribute">id</span>=<span class="hljs-string">"client_email"</span> <span class="hljs-attribute">type</span>=<span class="hljs-string">"email"</span> <span class="hljs-attribute">value</span>=<span class="hljs-string">""</span> <span class="hljs-attribute">name</span>=<span class="hljs-string">"client[email]"</span>></pre></div><p id="ca97">In some cases, we might want to override the default values and then we will use the <code>as:</code> parameter like we do for the birthday. This will yield the following datepicker:</p><figure id="b4fa"><img src="https://cdn-images-1.readmedium.com/v2/resize:fit:800/1*DwPfgLCSZpIYq_vVdpk3DQ.png"><figcaption></figcaption></figure><p id="eaeb"><a href="https://github.com/Modern-Rails/modern-rails-crm/pull/1/commits/ebba0cbaef74c12cae6522b51f2c0f36bfa121ea">Supporting commit for this subchapter</a>.</p><h1 id="f6ff">3.2 Bonus: Make the admin be the root path</h1><p id="6505">We are planning to make Active Admin (and the <code>/admin</code> path) be the center of our universe. It seems fit that we will make it be the root path as well. In our <code>config/routes.rb</code> we will add the following line at the top:</p><div id="8e51"><pre>root <span class="hljs-keyword">to</span>: <span class="hljs-string">"admin/dashboard#index"</span></pre></div><p id="2ff3">Per the <a href="https://guides.rubyonrails.org/routing.html#using-root">Rails documentation</a>: ‘You should put the <code>root</code> route at the top of the file, because it is the most popular route and should be matched first’.</p><p id="a6a0"><a href="https://github.com/Modern-Rails/modern-rails-crm/pull/1/commits/a4c505466e33f896d14c7dae7c3d3ef98a722841">This commit summarizes the change</a>.</p><h1 id="7b13">Recap</h1><p id="f163">In this tutorial, we made a big improvement to our development setup by adding the configuration needed to use Invoker. We also made the necessary setup to transition Active Admin to Webpacker, ensuring our application follows the current best practices. Finally, we added a solid looking theme on top of Active Admin, ensuring a good UI and UX.</p><p id="d559">As always, there’s a release tag on Github if you want to follow the code changes up to this part: <a href="https://github.com/Modern-Rails/modern-rails-crm/releases/tag/0.2.0">Link to Github</a>.</p><p id="06e9">In the next part, we will focus on setting up RSpec, Capybara (to run end-to-end system tests), FactoryBot for creating test data and write our first system test for the Clients resource.</p><p id="a1bc">Catalin (<a href="https://twitter.com/cionescu1">@cionescu1</a>) is a Ruby on Rails consultant, founder of <a href="https://www.organisely.app/">Organisely — A Free Appointment Management Software</a> and author of <a href="https://readmedium.com/building-a-modern-crm-in-ruby-on-rails-part-1-3b62f7b4dc7d">Modern Rails — Building a CRM in Ruby on Rails</a>.</p></article></body>

Modern CRM in Ruby on Rails — Part 2 — Make it beautiful

We’re continuing the series on building a Customer Relationship Management system with Ruby on Rails.

Modern CRM in Ruby on Rails — Part 2

Contents

  1. Part 1 — Settings up Rails, ActiveAdmin and create the Clients model
  2. Part 2 (this article)

Review

In the last article, we went through all the steps needed to set up our development environment with Ruby and Ruby on Rails, we installed ActiveAdmin into our application and created the model and AA resource for Clients. The code is available here.

Screen recording of the CRM so far

In this tutorial we will

  1. Set up Invoker
  2. Integrate Webpacker with ActiveAdmin
  3. Use a modern theme for ActiveAdmin

1. Setting up Invoker

This is a huge quality of life improvement for every developer. Remember that in the first part of the tutorial, we talked about having to start 2 processes to run the application (one for Rails — bin/rails s and one for Webpacker — bin/webpack-dev-server). Down the road, we will need to set up Sidekiq as well for processing background jobs (like cron jobs or sending emails). At that point, we will have to start 3 processes in 3 different terminal windows.

Introducing Invoker. You can install it through Ruby Gems:

$ gem install invoker

Invoker is a utility for managing processes in a development environment. Invoker supports DNS and proxying HTTP/HTTPS requests over a .test TLD. After installing the gem, you need to set it up (only needs to run once) using: sudo invoker setup.

In order to set up Invoker for our application, we’ll need to create a .ini file in our root folder. For now, we’ll instruct it to start the Rails and Webpacker processes.

[webpacker]
directory = /Users/work/modern-rails
command = bin/webpack-dev-server
[modern-rails]
directory = /Users/work/modern-rails
command = bundle exec rails s -p $PORT

We’ll save the file above in invoker.ini. Now, instead of running two processes, we’ll only need to run invoker start invoker.ini and the application will be accessible at modern-rails.test rather than localhost:3000.

Pro tip:

If you get the following error, then you are running into the Host Authorization, a new middleware included in Rails to prevent DNS attacks. By default, the middleware allows requests from 0.0.0.0 and localhost.

The solution is simply adding the config into config/environments/development.rb:

config.hosts << "modern-rails.test"

This commit summarizes the work done in this chapter.

2. Integrating Webpacker with ActiveAdmin

Starting with Rails 6.0, Webpacker has become the default asset generator. We will enable using webpacker in config/initializers/active_admin.rb by uncommenting the following line:

config.use_webpacker = true

We now need to run the generator to create the pack files:

$ bin/rails generate active_admin:webpacker

At the same, let’s do some cleanup. The original rails new generator created a hello_react.jsx and the default Sprockets assets for ActiveAdmin. These are the files I deleted:

deleted:    app/assets/javascripts/active_admin.js
deleted:    app/assets/stylesheets/active_admin.scss
deleted:    app/javascript/packs/hello_react.jsx

This commit summarizes the work done in this chapter.

3. Using a modern theme for ActiveAdmin

I’m going to use the Flat Skin by Armand Niampa theme for Active Admin, but the process of changing the theme is similar no matter which one you pick. Here’s a list of some of the theme options.

It would be so easy if we could just install the gem, import the assets and be done with it. Unfortunately, the gem is importing assets through Sprockets. Since we switched to Webpacker, we should be consistent and use a single way of bundling assets to avoid any issues down the road. The easiest solution here is simply to pick the asset files and include them, by hand, in our pack files. This could potentially be a problem if the assets were to change over time, but the Flat Skin repository was last updated on September 2017.

Screenshot of the CRM after changing the theme

This already looks so much better. Here’s what we had to do in order to add this theme.

First of all, I copied all the assets in Flat Skin into app/javascript/stylesheets. The result should look like this

Then, I referenced the new theme in app/javascript/stylesheets/active_admin.scss:

@import "~@activeadmin/activeadmin/src/scss/mixins";
@import "~@activeadmin/activeadmin/src/scss/base";
+ @import './admin_flat_skin/active_admin_flat_skin.scss';

That’s all I had to do to get the screenshot above. Supporting commit for this chaper.

3.1 Bonus: Use the built-in Date Picker for the Client’s birthday

Our Client Active Admin definition has been very thin so far. Let’s go override the default form generator to leverage the built-in Active Date Picker. ActiveAdmin offers the datepicker input, which uses the jQuery UI datepicker. I will make the following changes inapp/admin/clients.rb

form do |f|
  f.semantic_errors *f.object.errors.keys

  f.inputs do
    f.input :name
    f.input :phone_number
    f.input :email
    f.input :birthday, as: :datepicker
  end

  f.actions
end

Active Admin uses Formtastic to build it’s forms. The one clear benefit of it is that form.input automatically detects the field type and generates the appropriate input field. For example, f.input :email generates an input field with type email:

<input id="client_email" type="email" value="" name="client[email]">

In some cases, we might want to override the default values and then we will use the as: parameter like we do for the birthday. This will yield the following datepicker:

Supporting commit for this subchapter.

3.2 Bonus: Make the admin be the root path

We are planning to make Active Admin (and the /admin path) be the center of our universe. It seems fit that we will make it be the root path as well. In our config/routes.rb we will add the following line at the top:

root to: "admin/dashboard#index"

Per the Rails documentation: ‘You should put the root route at the top of the file, because it is the most popular route and should be matched first’.

This commit summarizes the change.

Recap

In this tutorial, we made a big improvement to our development setup by adding the configuration needed to use Invoker. We also made the necessary setup to transition Active Admin to Webpacker, ensuring our application follows the current best practices. Finally, we added a solid looking theme on top of Active Admin, ensuring a good UI and UX.

As always, there’s a release tag on Github if you want to follow the code changes up to this part: Link to Github.

In the next part, we will focus on setting up RSpec, Capybara (to run end-to-end system tests), FactoryBot for creating test data and write our first system test for the Clients resource.

Catalin (@cionescu1) is a Ruby on Rails consultant, founder of Organisely — A Free Appointment Management Software and author of Modern Rails — Building a CRM in Ruby on Rails.

Technology
Software Engineering
Programming
Ruby on Rails
Active Admin
Recommended from ReadMedium