Abstractions in Rails

Abstractions in Rails

As a powerful framework, Rails makes it easy for developers to build common patterns. It abstracts interfaces that can be used for integration with 3rd party service vendors without concerning about service-specific implementations.

Abstractions in Rails at a glance

As of major version 6.1, Rails provides following abstractions:

Active Record

Active Record is the most fundamental component in Rails. Every Rails developer should be familiar with it already. As an ORM framework, it abstracts the interactions with database, including persisting and retrieving model objects. With Active Record, developers hardly need to write any raw SQL query, if at all. Its database-agnostic Query Interface is not only easy to read and write, but also secure.

Active Job

Active Job provides a consistent interface for various types of background jobs. Developers don’t have to concern about different APIs, instead they program to an interface with one method perform. After the job is defined, it can be either executed instantly (perform_now), or enqueued and executed later (perform_later). But in either case, it calls the perform method, which is really the only thing that need to be implemented.

Choosing and switching from different queueing backends is also as easy as changing a few lines of configuration code. However, the choice largely depends on the features required. This page lists a number of natively supported backend adapters and their supported features.

Apart from those come with Rails, there are some other queueing backends that can be supported through Ruby Gem. For example, aws-sdk-rails Gem “provides a lightweight, high performance SQS backend for ActiveJob”.

Action Mailer

Action Mailer abstracts the email sending process. Since sending email outside the request-response cycle is one of the most common tasks, Action Mailer can be easily integrated with Active Job – calling deliver_later will send emails asynchronously, which rely on the queueing backend of Active Job.

Action Mailer comes with 4 pre-defined delivery methods: :smtp, :sendmail, :test and :file. But it’s possible to add more via Gems. For example, aws-sdk-rails Gem “automatically register SES as an ActionMailer delivery method”.

Active Storage

Active Storage abstracts interactions with file storage. Specifically, it deals with attaching files to Active Records and removing them.

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.

Rails guide

Rails 6.1 comes with native support for major cloud storage service providers, as is mentioned above. Developers can also implement support for other services.

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Close Bitnami banner