Now, web developers are able to access ready-made solutions instead of writing the functions from scratch. Of course, this has a great positive impact on the speed of web application development.
When we look at Ruby from this point of view, it stands out due to extensive libraries called Ruby on Rails gems. They offer particular functionalities to the Ruby applications you build up. Suppose you want to have an authentication function in your program - you don't need to write one. You can get it in the simple form of a Ruby on Rails gem. And this is only one example among thousands of top Ruby gems available for free.
In the present article, we are going to uncover the top Ruby on Rails gems we use in web development at Codica. We will also consider what specific tasks each of them helps us carry out.
A Ruby gem: what’s behind it?
Each Ruby on Rails gem contains a particular element of functionality. It includes any files related to this functionality. With the best RoR gems, you can find a solution almost for any task, which will significantly cut the time you need for web application development.
These libraries have their own storage called RubyGems and a package manager - gem bundler. Both of them include tools to easily handle the installation of gems and a server for their distribution. The first version of RubyGems was released in 2004.
A robust community is one of Ruby’s assets. The most proactive Ruby gem enthusiasts find solutions single-handedly and implement them in code. Then they merge these libraries into open source, and that’s where the life of RoR gems begins.
By the time this article was created, the total Ruby on Rails gems number was over 174,563.
Such an amount of ready solutions removes the need to “reinvent the wheel” each time you face a challenge. Being in free access, many functions are already written, and they cover all the web app development aspects: from ensuring code quality to deployment.
Top Ruby on Rails gems that we choose
Whether we build an MVP for a startup or create an e-business solution, Ruby on Rails gems serve us as a solid background.
For example, recently, we created a great custom e-commerce solution using Ruby on Rails gems.
With this solution, users can do many things using just one platform by moving between its parts. This marketplace offers many options to businesses. Here, users can sell goods and services and share the shopping experience with each other.
We started this web app development with the minimum viable product creation. The MVP helped us to gather the first user feedback and add more functionality to the solution.
Below you can see a video about the one of our most complex Ruby on Rails apps.
In this article, we have prepared a list of top Ruby on Rails gems helping us build great products.
One of the basic characteristics of the code is its quality and simplicity. It directly impacts the speed and performance of an application.
To discover and remove bulky code blocks and to carry out refactoring properly, we use two libraries.
RuboCop is extremely flexible, and most aspects of its behavior can be tweaked via various configuration options. Apart from reporting problems in your code, RuboCop can also automatically fix some issues for you.
Overcommit is a Ruby on Rails gem for configuring Git hooks. It is excellent for keeping the code quality high. It allows tuning git hooks for linters launch before every commit.
This tool provides a simple way to create and manage commands (service objects). The simple_command gem enables code to be easy to test, reuse, and debug.
dry-rb assists Ruby on Rail developers in writing flexible, clear, and maintainable code. Every dry-rb gem completes a common task. Yet, together they make an efficient platform for any kind of Ruby app.
Bundler-audit allows you to monitor the versions of libraries and indicates when you need to upgrade and to which version. This program helps you to maintain up-to-date versions of Ruby on Rails gems on the project.
Brakeman is a free vulnerability scanner. It is specifically designed for Ruby on Rails web apps. It statically analyzes the Rails app code to identify security matters at any web application development stage.
This gem analyzes Ruby classes, modules, and methods. It reports if your code is not readable or too complicated to understand for other software engineers.
No matter how high the Ruby code quality is, sometimes bugs and errors are inevitable. To uncover and eliminate bugs, our team finds the following awesome Ruby gems quite useful.
Better Errors is a Ruby gem to show errors. It displays helpful information about an error and has an excellent visual impact. Better_errors replaces the standard Rails error page. It is also applied outside Rails in any Rack app as Rack middleware.
Byebug is a simple-to-use and feature-rich debugger for Ruby. This gem allows line-by-line fulfillment of Ruby code. It pauses between tasks, and executing code, changing the variables on the move.
Get them from our monthly newsletter.
The web development process is impossible without quality control of the Ruby on Rails application created. Here come QA testing services, which allow us to assess the quality. To run and manage tests smoothly, our developers prefer to use the following best Ruby on Rails gems.
The Rswag gem helps to generate beautiful API (application programming interface) documentation.
It provides an awesome UI (user interface) to research and test operations straight from your RSpec integration tests.
With this library, you don't need to change tests when you change the HTTP library.
It enables setting and checking expectations on HTTP requests. Also, you receive intelligent matching of the similar URIs in various representations (non-encoded and encoded forms).
The gem VCR is a great choice for doing integrated tests in Ruby applications. It lets software specialists automate the process of stubbing the web requests through the Webmock gem (or other similar).
For this, it records cassette files with all the responses to external APIs and HTTP requests. Thus, it enables us to execute the test suite quickly. So, we do not depend on their states and the disponibility of these APIs.
RSpec suggests behaviour driven development for Ruby. This Ruby gem provides standards for Ruby on Rails app testing. It offers more readable DSL (digital subscriber line), as compared to TestUnit standard testing library. Beyond that, it easily integrates with many other Ruby gems that facilitate the testing process.
By the way, you may find interesting our GitHub report on RSpec best practices.
Capybara is an acceptance test framework for web applications. It flawlessly runs in tandem with RSpec. The Ruby gem is responsible for integrated tests that imitate users’ actions in a browser.
It allows describing the authorization scenario in a few lines (go on the homepage, enter a login and password, and click the Log-in button). Then it suggests various convenient methods for test debugging.
Capybara-Screenshot is a Ruby gem for automatic saving of screenshots if a Capybara scenario breaks down. Together with Capybara and Cucumber, RSpec, or Minitest, the gem allows viewing the source code.
It takes a screenshot (when relevant) for each failure in a test suite. It stores HTML for the failed web page and a screenshot image (when using capybara-webkit, Selenium, or poltergeist). Such an option is quite valuable when you need to find a problem in failing rounds.
The gem makes tests written in TestUnit, RSpec and Cucumber pass faster. How? It runs them in parallel on multiple CPUs (central processing units).
To accelerate the process, the code uses all cores of the systems. It runs the test and uses additional test databases. The gem supports Rails and other Ruby frameworks.
factory_bot is a library for setting up Ruby objects as test data. It has a straightforward definition syntax.
The library maintains build strategies. These are saved instances, unsaved instances, attribute hashes, and stubbed objects. It supports numerous factories for the same class (user, admin_user), including factory inheritance.
chromedriver-helper provides easy installation and use of chromedriver. Above that, it serves as the Chromium project's selenium webdriver adapter. It installs an executable chromedriver-helper in a gem path, enabling Selenium to invoke it as the web driver.
When required, the scenario will download the relevant binary for a platform and install it into ~/.chromedriver-helper, then execute it. Here, the Ruby gem allows particular projects even to select which version of chromedriver they want to run.
Shoulda Matchers is a Ruby gem providing simple one-liner tests for common Rails functionality. Shoulda Matchers accelerates writing the tests for a typical capacity.
SimpleCov is one of the useful Ruby on Rails gems for testing. It shows the percentage of code covered with writing unit tests.
Thus, the gem encourages Ruby on Rails developers to write 100%-tested code, which greatly adds to its quality.
Database Cleaner provides code coverage for Ruby 1.9+ with a powerful configuration library. This gem also provides automatic merging of coverage across test suites.
It’s often applied in tandem with RSpec and is used to clean a testing database when launching tests. The gem enables tuning the time of the database cleanup: before the tests launch and before/after running each of the test cases.
Rspec::Retry is a tool that provides :retry randomly failing RSpec example. It adds a :retry option for occasionally failing RSpec patterns.
If a model has the :retry option, RSpec will retry the example a particular number of times until the pattern works.
Faker is a library for generating demo data such as names, addresses, and phone numbers. It enables generating realistic test data, and fill up your database with more than a couple of records while developing. Moreover, this gem is super handy for taking screenshots.
Authentication and authorization
Most of the Ruby on Rails apps require an authentication and authorization system. Be it an e-commerce site or a publishing platform, users’ data protection always ranks first. To implement a secure solution, we use several of the best Ruby on Rails gems.
Devise is a flexible authentication solution for Rails based on Warden. Devise helps build an identification system of any complexity level. This level varies from authentication via email and password to a referral system. In a nutshell, it has 10 modules, including:
- Confirmable: sends emails with confirmation instructions. It verifies whether an account is already confirmed during sign-in;
- Recoverable: resets the user password and sends reset instructions;
- Rememberable: manages the token generation and clearing to remember the user from a saved cookie;
- Trackable: tracks sign-in account, timestamps, and IP address.
ruby-jwt gem is a simple Ruby implementation of the RFC 7519 OAuth JSON Web Token (JWT) standard. As a tool for checking user authentication, it’s considered to be one of the safest information transfer systems between two parties.
For a practical application of this gem, check our article How We Built a Slack Bot for Time Tracking with Vue.js and Ruby on Rails. There we specify how we implemented JWT when creating a Time Bot for Slack.
In this scheme, an application server is configured. So, it checks whether an incoming JWT is the one generated with the authentication server.
Pundit is a gem that offers a set of helpers. They lead you to apply object-oriented design patterns and simple Ruby objects to build an authorization system.
This gem is easy to use. It has minimal permissions. Besides, it is excellent for controlling role-based authorization. You can do it by using policies determined in simple Ruby classes. Pundit helps to create a simple, scalable, and robust authorization system.
CanCanCan is a super-convenient authorization Ruby on Rails gem. It allows for creating and handling users’ roles and rules in web apps, providing a set of helpers for views and controllers.
Even the ability.rb file in the model directory can contain understandable and readable instructions. These instructions include a description of rights for each user group. This file smoothly interacts with gems providing users with authentication.
OmniAuth is a flexible authentication system utilizing Rack middleware.
It’s a library allowing generate providers for authentication via social networks accounts. For example, via Google or Facebook. The library already contains an extensive list of ready-to-use providers.
Sometimes, we need to build web applications that allow users to upload different kinds of files. For example, these can be documentation or pictures.
To make this feature as helpful and user-friendly as possible, we apply a few top Ruby on Rails gems.
Shrine is a gem for handling file attachments in Ruby web apps. It supports downloading, uploading, processing, and deleting IO (input/output) objects. Shrine uses robust streaming for low memory usage. This gem provides a high-level interface.
This interface helps attach uploaded files to database records. It saves their location and metadata to a database column. This interface supports direct uploads and background jobs for a fully asynchronous user experience.
MiniMagick serves as a mini-replacement for RMagick. This Ruby gem allows reducing Ruby processes memory. It makes available all the command-line options of ImageMagick.
In some projects, the amount of information is too big. In this case, finding the required data might be a challenge. To execute the search option as user-friendly as possible, we take advantage of one utility.
Elasticsearch is a popular search system widely used in enterprises.
This Ruby gem integrates the Elasticsearch engine into Rails applications with its full set of features. For example, these features include full-text search.
pg_search builds ActiveRecord named scopes when applying PostgreSQL full-text search.
It is quite a decent alternative to Elasticsearch when using PostgreSQL. The utility is easy to configure. Besides, it allows the implementation of Autocomplete and Search by associations.
HasScope allows you to map incoming controller parameters to named scopes in your resources. This is its most common usage.
However, it can be used anywhere. For example, HasScope will help a RoR developer with plain old Ruby objects. Also, we can use it to manage the given value and call the scope in a custom way.
Building and supplying an extensive web application usually involves managing a vast amount of data. The following utilities help our team to implement a comprehensive database administration tool.
Active Admin is the administration framework for Ruby on Rails applications.
The plugin allows for generating administration-style interfaces. It separates common business application patterns. How? Activeadmin drastically simplifies the implementation of elegant interfaces for RoR developers.
Administrate is a Rails engine. It helps construct a user-responsive admin dashboard. For now, we haven’t used it yet, though we’re exploring its possibilities and looking forward to implementing it in our future projects.
More best Ruby on Rails gems for various tasks
And as a bonus, we’ll highlight for you the best Ruby on Rails gems that you may find interesting. We will list them according to their popularity (the number of stars on Github).
Sidekiq is a simple and efficient background processing tool for Ruby on Rails development.
It applies threads to manage many functions in the same process simultaneously. Sidekiq does not require Rails. Still, it will closely combine with Rails to simplify background processing as much as possible.
We like to use Sidekiq to create profitable web solutions. For example, we have successfully developed an attractive and efficient fitness app, Impact Professional Training.
Thanks to our best practices in development, the client has grown his fitness business. And he tells about the results in the video below.
sidekiq-scheduler is an extension to Sidekiq that pushes jobs in a scheduled way. It does it by mimicking cron utility.
It applies Rufus Scheduler under the hood, an in-memory scheduler. In general, sidekiq-scheduler overcomes the single point of failure issue.
FriendlyId provides great assistance when working with permalink plugins for ActiveRecord.
It allows creating of readable good-looking URLs. Also, it helps to manage human-friendly strings as if they were digital ids for ActiveRecord models.
dotenv is a Ruby gem to load environment variables from a dotfile which can be outside the main project folder. This way, dotenv allows safe storage of the web app configuration data.
Slim is a template language attempting to reduce the syntax to the essential code elements without becoming cryptic.
It started as an exercise to see how much could be removed from a standard HTML template (<, >, closing tags, etc.). The more people showed interest for Slim, the more functionality grew along with the flexibility of the syntax.
Redis is a BSD (Berkeley Software Distribution) licensed Ruby-client open-source library. It represents an in-memory data structure store.
This Ruby gem is used as a database, cache, and message broker. Redis provides support for data structures such as strings, hashes, lists, sets, and sorted sets with range queries. It also supports bitmaps, hyperloglogs, and geospatial indexes with radius queries and streams.
This Ruby gem includes built-in replication, Lua scripting, LRU eviction, and transactions. It also comprises several levels of on-disk persistence. Above that, Redis allows high availability via Redis Sentinel. Furthermore, it provides automatic partitioning with Redis Cluster.
Annotate simplifies work with Rails models. It adds a comment summarizing the current scheme to the models' files. This feature makes code building much easier because there’s no need to check a scheme file every time.
Config provides the easiest way to combine multi-environment yaml settings with Rails, Sinatra, Pandrino, and others of Ruby. It offers an attractive and easy solution for simply managing of environment-specific parameters.
i18n-tasks is a Ruby gem for working with translation in an app. It can be applied with any project that uses the Ruby i18n gem, which is a default in Rails.
It provides an interface for working with locales. It helps find missing translations, and normalizes files with translations. Also, this gem adds new translations via Google Translate API.
Money-Rails is a great Ruby gem for working with currencies. It allows automating updates in currency rates and converting prices into the required currency.
With it, the data will be stored in your database by default. You can adjust it for any data store as it grows.
RouteTranslator is a tool to help a RoR developer to administer the translations of app routes. It has a simple dictionary format.
Initially, it was a fork of the translate_routes plugin. But later transformed into a fully-fledged translation instrument.
Pagy is a lightweight and ultra-quick pagination library. It splits extensive data collections into subsets to display on many pages.
Instead of Ruby objects, Pagy applies integers for the calculations. The Pagy's code produces its own URLs, HTML, interpolation, and pluralization. Also, it applies specialized code instead of generic helpers.
Below you can see the comparison of Pagy and other gems' output in the same environment.
Ancestry enables the records of a Ruby on Rails ActiveRecord model to be arranged as a hierarchy. It applies the materialized path pattern. Also, Ancestry exposes all the standard tree structure relations. This allows all of them to be taken in one SQL query.
Other features comprise STI support, depth caching, and depth constraints. These are also easy migration from older gems and integrity checking. Moreover, Ancestry provides multiple strategies for dealing with orphaned records and more.
jsonapi-serializer is a gem for adding accessors to the attributes that are serialized with support for defaults and types.
This gem allows us to format our JSON simply. So, we can pick only the data we need and access our relationships with one request.
AASM (acts_as_state_machine) is a gem for adding state machines to Ruby classes (ActiveRecord, plain Ruby, NoBrainer, Mongoid, Dynamoid).
AASM enables you to keep track of the current class state. At the same time, with it, you can be sure that the transitions of states are valid.
Discard is a gem that provides soft deletes of records in the database. In general, this gem is an easy-to-use ActiveRecord mixin. With it, you can add conventions for flagging records as discarded.
At Codica, we appreciate Ruby for all its features. These are its focus, clarity, and excellent programming language readability. The last one is often considered human-like. Best Ruby gems greatly add up to Ruby’s advantages, as they bring rapid software development speed to any project type.
The Ruby gems we have listed in this article are helping us build great products for different spheres. For example, these are e-commerce, traveling, finance, investment domain, and others web application projects.
Of course, there are thousands of Ruby gems, and each of them has great value for Ruby on Rails development. Even with many years of experience in Ruby, we discover more and more useful Ruby gems and the opportunities they provide. There’s always room for improvement.
We hope our list of top Ruby on Rails gems will help you with your projects.