A list of commonly used gems
Preface
As I summarized last month, I think that the return from apps to the web is happening a little in the SaaS boom. Even if you can use a lightweight framework for just the API of an mobile app, a full-stack framework is more flexible when you also need to render the complex web pages.
Especially for B2B services, there are cases where there is not much access but the business requirements are complex, and the capability of ORM directly leads to business competitiveness, so there are many cases where the simple Rails and ActiveRecord are suitable.
Development in Ruby on Rails is concrete, but I've never summarized it. So, I'll write about the most commonly used libraries (gem).
How to choose a gem
It is important to look into other companies' case studies, but it is also good to check the Ruby Toolbox to get a popularity.
If you only look at the number of stars, an old and unmaintained gem may have a higher rating, so checking recent commits and momentum is important.
Upgrading Rails often comes with hassle. It is important to add only gems that are truly necessary for maintainability.
Authentication
Some web frameworks have built-in user authentication mechanisms, but Rails does not provide a login page.
- devise
- authlogic
- sorcery
are popular gems. devise is the most popular authentication gem.
If you want to provide SNS login, you should use another gem such as OmniAuth. Minor authentication gems sometimes doesn't have third party gem that supports social login.
Anyway, devise has shortcomings, for example, there have been issues in the past about routing is not a resource oriented.
Testing
Testing gem is mainly done by
- rspec
- minitest
These are top two. Some people say minitest is enough. However from my experience (in Japanese company), rspec is often used. rspec supports more syntax and matchers.
I use factory_bot to generate seed data for test. It was called factory_girl in the past, but the name changed because it is a better name. (The python factory_boy is still boy, which is a bit confusing for me.)
The test data can be put in the actual DB, but you can use a method build
that will not put the data in unnecessary cases. You can speed up the test by avoiding IO.
In the past, we used to use a gem called timecop to test the behavior of stored data change in different timing. Now the same thing can be achieved with built-in travel_to
.
Administration
If you just want to see the DB values as they are, you can use a gem, but if you need customization, I recommend not using a gem. Just simply implement the page in admin namespace.
- (rails_admin)
Model
- annotate
gem to add table definitions to model files.
- (activerecord-import)
gem that allows bulk importing. You can also use built-in insert_all since Rails 6
- (enumerize)
Rails has a built-in enum function, but there is a opinion that enumerize is more convenient for i18n.
- discard
Discard is a gem for soft delete. Similar gem paranoia is designed to overwrite built-in methods. They stopped maintenance because keep up with Rails changes was hard.
The design of discard is add another method. They don't override built-in method.
file upload
- carrierwave
A gem for file upload. Unlike ActiveStorage (described below), the model and uploader are independent. form helper is rich. Even if the user fails validation after upload, you can keep the attachment for resubmission.
- active_storage
Rails built-in file upload gem. Similar paperclip gem has been deprecated due to the introduction of ActiveStorage.
The file upload and table row is deeply coupled.
Forms
There are gems such as reform, simple_form, etc., but even if you want to add the Form Object, I recommended to use the bare class (PORO: Plain Old Ruby Object) or Active Model.
Decorator
- draper
- active_decorator
These are well-known
json serialization
- jbuilder
Although some people say it is slow, jbuilder is sufficient in many cases.
active_model_serializers, which is tightly coupled with the model, is deprecated because its maintenance has stopped.
Pagination
The following two are popular. Personally, I often use kaminari.
- kaminari
- will_paginate
view
It is possible to embed variables in plain html by using erb, but it requires closing tags and visibility is not good.
- haml
- slim
are commonly used.
If you want to use HTML snippets, you need to do the reverse conversion. That is a pain, but I put up with it (I personally think a design like himl is ideal).
If the view has many lines, there are cases you want to use a view component. You can use render_in since Rails 6.1 or gems below.
- (cells)
- (view_component)
Using partials and implementations from scratch is also an option.
lint
- rubocop
A gem to check if Ruby follows style guide. auto correct is available.
- haml-lint
haml linter gem.
- overcommit
A gem to run lint based on git operations.
Error detection
- bullet
gem to detect N+1
- (brakeman)
gem that checks if the library needs to be updated in terms of vulnerabilities. Now you can do something similar with vulnerability check on GitHub.
Job queue
These are famous.
- Sidekiq
- Delayed::Job
- (Resque)
I choose Sidekiq if I can prepare Redis. if you want to complete with RDB only, Delayed::Job is a choice.
Execute periodically
- whenever
It is hard to edit crontab directly, so traditionally, whenever is often used on a representative server. However, recently many companies use docker-based server management. In that case, we tend to set up tasks in docker on demand.
Deploy
- capistrano
Capistrano is not for docker-based servers, but it is often used for fixed servers.
Full text search
This is more about infrastructure than Ruby. Many companies use Elasticsearch or Solr. Recently, Elasticsearch is dominant. However, the relationship with AWS is bad.
- elasticsearch-rails
meilisearch is also active in ruby bindings and has a gem called meilisearch-rails.
Sessions
- redis-actionpack
- (kredis)
In the past, some companies used memcached, but these days session management is often done with Redis.
redis-rails is deprecated. For cache, you can use built-in rails mechanism. For session, redis-actionpack is recommended.
There are gems Kredis that is more compatible with Ruby syntax.
Web server
- unicorn
- puma
are often used.
Impressions
There are some areas where Rails is weak compared to Laravel and Django, so I would like to summarize those in the future.