Table of Contents
- Action Cable
- Action Mailbox
- Action Mailer
- Action Pack
- Action Text
- Action View
- Active Job
- Active Model
- Active Record
- Active Storage
- Active Support
- Bundler
- Railties
- Sprockets-rails
I didn’t quite know where to put this information, so for the time being I will talk about the Gems before we get onto an example application. Although you can create a very basic application without have any functional knowledge of Gems and how they work, Gems are such an important part of Ruby and Rails that I think that at the very least we should invest the time up front to understand the Gems that Rails loads by default.
Gems are defined in the file Gemfile, which lives at the project root. This is an editable ASCII configuration file that instructs bundle (I will explain Bundler later) which Gems, and very importantly, which revisions of those Gems should be loaded into the project. A short snippet from the default Gemfile is below:
source ‘https://rubygems.org’
git_source(:github) { |repo| “https://github.com/#{repo}.git” }
ruby ‘3.0.0’
# Bundle edge Rails instead: gem ‘rails’, github: ‘rails/rails’, branch: ‘main’
gem ‘rails’, ‘~> 6.1.4’
# Use sqlite3 as the database for Active Record
gem ‘sqlite3’, ‘~> 1.4’
# Use Puma as the app server
gem ‘puma’, ‘~> 5.0’
# Use SCSS for stylesheets
gem ‘sass-rails’, ‘>= 6’
# Transpile app-like JavaScript. Read more: https://github.com/rails/webpacker
gem ‘webpacker’, ‘~> 5.0’
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem ‘turbolinks’, ‘~> 5’
The first two lines tells Rails where to actually find and download Gems from. As with just about anything in Rails, this can be overridden and Gems can be stored locally, as well as downloaded from you own repositories.
Although Ruby isn’t a Gem, it is listed in the Gemfile to instruct the Bundler which version of Rails to use. The file .ruby-version in your project root also stores the version of Ruby for the project and it is very contradictory for Rails to have the information stored in two places. Basically, if the Ruby version is in the Gemfile, Bundle will use that version. If the Ruby version is not in the Gemfile, Rails will use the version specified by rbenv local. I assume that if rbenv is not installed and the Gemfile file does not specify a Ruby version, although I have not tested to verify (or looked at the source code), Rails will use the Ruby version stored in the file .ruby-version.
Rails is usually much better than this….
When bundle runs, it will create the file Gemfile.lock, which should never be manually edited. The file Gemfile.lock will list which Gems are installed and what version of the Gem is used, as well as which Gems the installed Gem depends upon.
GEM
remote: https://rubygems.org/
specs:
actionpack (= 6.1.4)
activesupport (= 6.1.4)
nio4r (~> 2.0)
websocket-driver (>= 0.6.1)
actionmailbox (6.1.4)
actionpack (= 6.1.4)
activejob (= 6.1.4)
activerecord (= 6.1.4)
activestorage (= 6.1.4)
Each Gem dependency will also be listed with their dependents, so that the Gemfile.lock file contains a full list of all Gems used in the application. Have a look in your Gemfile.lock file and you will see that it has a list of almost 200 Gems and Gems that they rely upon.
If you search for the rails entry in the Gemfile.lock file, you will see that it has the following dependencies:
rails (6.1.4)
actioncable (= 6.1.4)
actionmailbox (= 6.1.4)
actionmailer (= 6.1.4)
actionpack (= 6.1.4)
actiontext (= 6.1.4)
actionview (= 6.1.4)
activejob (= 6.1.4)
activemodel (= 6.1.4)
activerecord (= 6.1.4)
activestorage (= 6.1.4)
activesupport (= 6.1.4)
bundler (>= 1.15.0)
railties (= 6.1.4)
sprockets-rails (>= 2.0.0)
You will notice at a quick glance that the version of most of the dependent Gems is the same as Rails, which gives you a hint at how closely they are interlocked. Let’s have a look at each of these Gems individually, as many of them are going to be important players in your Rails application, even if their work is often invisible to you.
Action Cable
Action Cable seamlessly integrates WebSockets with the rest of your Rails application. It allows for real-time features to be written in Ruby in the same style and form as the rest of your Rails application, while still being performant and scalable. It’s a full-stack offering that provides both a client-side JavaScript framework and a server-side Ruby framework. You have access to your full domain model written with Active Record or your ORM of choice.
You will notice a connections folder in your app folder, which is where connection controllers live.
Action Mailbox
Action Mailbox routes incoming emails to controller-like mailboxes for processing in Rails. It ships with ingresses for Mailgun, Mandrill, Postmark, and SendGrid. You can also handle inbound mails directly via the built-in Exim, Postfix, and Qmail ingresses
Action Mailer
Railguides Description:
Action Mailer allows you to send emails from your application using mailer classes and views.
Action Pack
Web apps on Rails. Simple, battle-tested conventions for building and testing MVC web applications. Works with any Rack-compatible server.
Action Pack is a framework for handling and responding to web requests. It provides mechanisms for routing (mapping request URLs to actions), defining controllers that implement actions, and generating responses by rendering views, which are templates of various formats. In short, Action Pack provides the view and controller layers in the MVC paradigm.
It consists of several modules:
-
-
Action Dispatch, which parses information about the web request, handles routing as defined by the user, and does advanced processing related to HTTP such as MIME-type negotiation, decoding parameters in POST, PATCH, or PUT bodies, handling HTTP caching logic, cookies and sessions.
-
Action Controller, which provides a base controller class that can be subclassed to implement filters and actions to handle requests. The result of an action is typically content generated from views.
-
With the Ruby on Rails framework, users only directly interface with the Action Controller module. Necessary Action Dispatch functionality is activated by default and Action View rendering is implicitly triggered by Action Controller. However, these modules are designed to function on their own and can be used outside of Rails.
You can read more about Action Pack in the Action Controller Overview guide.
I am finding precious little information on Action Pack on the Internet. The documentation link for the rubygems.org entry for Action Pack links to a page that doesn’t mention Action Pack at all. However, the Github README.md file gives me the impression that this is pretty much core Rails. It takes incoming HTTP requests and maps them to the appropriate controller action.
Action Text
Action Text brings rich text content and editing to Rails. It includes the Trix editor that handles everything from formatting to links to quotes to lists to embedded images and galleries. The rich text content generated by the Trix editor is saved in its own RichText model that’s associated with any existing Active Record model in the application. Any embedded images (or other attachments) are automatically stored using Active Storage and associated with the included RichText model.
Having never used the Trix editor, I cannot speak for how well Action Text does its job. I have used TinyMCE in a few projects and it seems to do an excellent job.
Action View
In Rails, web requests are handled by Action Controller and Action View. Typically, Action Controller is concerned with communicating with the database and performing CRUD actions where necessary. Action View is then responsible for compiling the response.
Action View templates are written using embedded Ruby in tags mingled with HTML. To avoid cluttering the templates with boilerplate code, a number of helper classes provide common behavior for forms, dates, and strings. It’s also easy to add new helpers to your application as it evolves.
Some features of Action View are tied to Active Record, but that doesn’t mean Action View depends on Active Record. Action View is an independent package that can be used with any sort of Ruby libraries.
Action View responses can be HTML, HAML, SLIM, JSON, CSV, JavaScript, Postscript or other types.
Active Job
Active Job is a framework for declaring jobs and making them run on a variety of queuing backends. These jobs can be everything from regularly scheduled clean-ups, to billing charges, to mailings. Anything that can be chopped up into small units of work and run in parallel, really.
Please don’t let the statement “…and run in parallel” mislead you. Ruby is still a predominantly single threaded application and while it can have multiple simultaneous jobs running, these are usually time sliced on the same process. Although Ruby supports parallel execution, this is primarily reserved for IO & HTTP based activity.
Active Model
Active Model is a library containing various modules used in developing classes that need some features present on Active Record.
Active Model is a critical part of Rails that you should take the time to become intimate with. You will be dealing with it every day.
Active Record
Active Record is the M in MVC – the model – which is the layer of the system responsible for representing business data and logic. Active Record facilitates the creation and use of business objects whose data requires persistent storage to a database. It is an implementation of the Active Record pattern which itself is a description of an Object Relational Mapping system.
Like Active Model, Active Record is a core element of Rails that you will constantly be using. Because of its sheer size and complexity, you will probably want to invest more time on getting to know Active Record than just about any other Gem.
Active Storage
Active Storage facilitates uploading files to a cloud storage service like Amazon S3, Google Cloud Storage, or Microsoft Azure Storage and attaching those files to Active Record objects. It comes with a local disk-based service for development and testing and supports mirroring files to subordinate services for backups and migrations.
Using Active Storage, an application can transform image uploads or generate image representations of non-image uploads like PDFs and videos, and extract metadata from arbitrary files.
Active Storage vastly simplifies the task of uploading files. The ease of use is impressive.
Active Support
Active Support is one of the components of Ruby on Rails. As its name suggest, it “supports” applications by providing many utilities like encrypting, querying data, callbacks, security utilities, and a lot more. Even though it’s part of Rails, Active Support can be installed in any Ruby project with its gem. Which is what many libraries are doing.
Bundler
Bundler provides a consistent environment for Ruby projects by tracking and installing the exact gems and versions that are needed.
Bundler is an exit from dependency hell, and ensures that the gems you need are present in development, staging, and production. Starting work on a project is as simple as bundle install.
I wouldn’t fully agree with the statement that “Bundler is an exit from dependency hell…”, because dependency hell includes things like conflicting version requirements, which Bundler cannot address. As well, I suggest you prepare yourself for a little “Bundler Hell”, as I have run into numerous situations where the Bundler version specified in the Gemfile.lock file simply wasn’t available for whatever reason. This led to one of the very few times where I needed to manually edit the Gemfile.lock file to change the Bundler version to match what I had available.
Railties
Rails::Railtie is the core of the Rails framework and provides several hooks to extend Rails and/or modify the initialization process.
Every major component of Rails (Action Mailer, Action Controller, Active Record, etc.) implements a railtie. Each of them is responsible for their own initialization. This makes Rails itself absent of any component hooks, allowing other components to be used in place of any of the Rails defaults.
Developing a Rails extension does not require implementing a railtie, but if you need to interact with the Rails framework during or after boot, then a railtie is needed.
For example, an extension doing any of the following would need a railtie:
-
-
-
-
creating initializers
-
configuring a
Railsframework for the application, like setting a generator -
adding
config.*keys to the environment -
setting up a subscriber with
ActiveSupport::Notifications -
adding Rake tasks
-
-
-
Sprockets-rails
Sprockets is a Ruby library for compiling and serving web assets. It features declarative dependency management for JavaScript and CSS assets, as well as a powerful preprocessor pipeline that allows you to write assets in languages like CoffeeScript, Sass and SCSS.