[Awesome Ruby Gem] Use rails-patterns gem to providde lightweight, standardized, rails-oriented patterns in Rails
rails-patterns
A collection of lightweight, standardized, rails-oriented patterns used by RubyOnRails Developers @ Selleo
-
Query - complex querying on active record relation
-
Service - useful for handling processes involving multiple steps
-
Collection - when in need to add a method that relates to the collection as whole
-
Form - when you need a place for callbacks, want to replace strong parameters or handle virtual/composite resources
-
Calculation - when you need a place for calculating a simple value (numeric, array, hash) and/or cache it
-
Rule and Ruleset - when you need a place for conditional logic
Installation
You can install it as a gem:
1 | gem install rails-patterns |
or add it into a Gemfile (Bundler):
1 | # Gemfile |
Then, run bundle install
.
1 | bundle install |
Usages
Query
When to use it
One should consider using query objects pattern when in need to perform complex querying on active record relation. Usually one should avoid using scopes for such purpose. As a rule of thumb, if scope interacts with more than one column and/or joins in other tables, it should be moved to query object. Also whenever a chain of scopes is to be used, one should consider using query object too. Some more information on using query objects can be found in this article.
Assumptions and rules
-Query objects are always used by calling class-level .call
method
-
Query objects require
ActiveRecord::Relation
orActiveRecord::Base
as constructor argument -
Default relation (see above) can be defined by using queries macro
-
Query objects have to implement #query method that returns
ActiveRecord::Relation
-
Query objects provide access to consecutive keyword arguments using
#options
hash
Other
Because of the fact, that QueryObject implements .call
method, those can be used to construct scopes if required. (read more…)
Examples
Declaration
1 | class RecentlyActivatedUsersQuery < Patterns::Query |
Usage
1 | RecentlyActivatedUsersQuery.call |
Service
When to use it
Service objects are commonly used to mitigate problems with model callbacks that interact with external classes (read more…). Service objects are also useful for handling processes involving multiple steps. E.g. a controller that performs more than one operation on its subject (usually a model instance) is a possible candidate for Extract ServiceObject (or Extract FormObject) refactoring. In many cases service object can be used as scaffolding for replace method with object refactoring. Some more information on using services can be found in this article.
Assumptions and rules
-
Service objects are always used by calling class-level .call method
-
Service objects have to implement #call method
-
Calling service object’s .call method executes #call and returns service object instance
-
A result of #call method is accessible through #result method
-
It is recommended for #call method to be the only public method of service object (besides state readers)
-
It is recommended to name service object classes after commands (e.g. ActivateUser instead of UserActivation)
Other
A bit higher level of abstraction is provided by business_process gem.
Examples
Declaration
1 | class ActivateUser < Patterns::Service |
Usage
1 | user_activation = ActivateUser.call(user) |
Collection
When to use it
One should consider using collection pattern when in need to add a method that relates to the collection a whole. Popular example for such situation is for paginated collections, where for instance #current_page getter makes sense only in collection context. Also collections can be used as a container for mapping or grouping logic (especially if the mapping is not 1-1 in terms of size). Collection might also act as a replacement for models not inheriting from ActiveRecord::Base (e.g. StatusesCollection, ColorsCollection etc.). What is more, collections can be used if we need to encapsulate “flagging” logic - for instance if we need to render a separator element between collection elements based on some specific logic, we can move this logic from view layer to collection and yield an additional flag to control rendering in view.
Assumptions and rules
-
Collections include Enumerable
-
Collections can be initialized using .new, .from and .for (aliases)
-
Collections have to implement #collection method that returns object responding to #each
-
Collections provide access to consecutive keyword arguments using #options hash
-
Collections provide access to first argument using #subject
Examples
Declaration
1 | class ColorsCollection < Patterns::Collection |
Usage
1 | ColorsCollection.new |
Form
When to use it
Form objects, just like service objects, are commonly used to mitigate problems with model callbacks that interact with external classes (read more…). Form objects can also be used as replacement for ActionController::StrongParameters strategy, as all writable attributes are re-defined within each form. Finally form objects can be used as wrappers for virtual (with no model representation) or composite (saving multiple models at once) resources. In the latter case this may act as replacement for ActiveRecord::NestedAttributes. In some cases FormObject can be used as scaffolding for replace method with object refactoring. Some more information on using form objects can be found in this article.
Assumptions and rules
-
Forms include ActiveModel::Validations to support validation.
-
Forms include Virtus.model to support attribute static method with all corresponding capabilities.
-
Forms can be initialized using .new.
-
Forms accept optional resource object as first constructor argument.
-
Forms accept optional attributes hash as latter constructor argument.
-
Forms have to implement #persist method that returns falsey (if failed) or truthy (if succeeded) value.
-
Forms provide access to first constructor argument using #resource.
-
Forms are saved using their #save or #save! methods.
-
Forms will attempt to pre-populate their fields using resource#attributes and public getters for resource
-
Form’s fields are populated with passed-in attributes hash reverse-merged with pre-populated attributes if possible.
-
Forms provide #as builder method that populates internal @form_owner variable (can be used to store current user).
-
Forms allow defining/overriding their #param_key method result by using .param_key static method. This defaults to #resource#model_name#param_key.
-
Forms delegate #persisted? method to #resource if possible.
-
Forms do handle ActionController::Parameters as attributes hash (using to_unsafe_h)
-
It is recommended to wrap #persist method in transaction if possible and if multiple model are affected.
Examples
Declaration
1 | class UserForm < Patterns::Form |
Usage
1 | form = UserForm.new(User.find(1), params[:person]) |
Calculation
When to use it
Calculation objects provide a place to calculate simple values (i.e. numeric, arrays, hashes), especially when calculations require interacting with multiple classes, and thus do not fit into any particular one. Calculation objects also provide simple abstraction for caching their results.
Assumptions and rules
-
Calculations have to implement #result method that returns any value (result of calculation).
-
Calculations do provide .set_cache_expiry_every method, that allows defining caching period.
-
When .set_cache_expiry_every is not used, result is not being cached.
-
Calculations return result by calling any of following methods: .calculate, .result_for or .result.
-
First argument passed to calculation is accessible by #subject private method.
-
Arguments hash passed to calculation is accessible by #options private method.
-
Caching takes into account arguments passed when building cache key.
-
To build cache key, #cache_key of each argument value is used if possible.
-
By default Rails.cache is used as cache store.
Examples
Declaration
1 | class AverageHotelDailyRevenue < Patterns::Calculation |
Usage
1 | hotel = Hotel.find(123) |
Rule and Ruleset
When to use it
Rule objects provide a place for dislocating/extracting conditional logic.
Use it when:
-
given complex condition is duplicated in multiple places in your codebase
-
part of condition logic can be reused in some other place
-
there is a need to instantiate condition itself for some reason (i.e. to represent it in the interface)
-
responsibility of your class is blurred by complex conditional logic, and as a result…
-
…tests for your class require multiple condition branches / nested contexts
Assumptions and rules
-
Rule has
#satisfied?
,#applicable?
,#not_applicable?
and#forceable?
methods available. -
Rule has to implement at least #satisfied? method. #not_applicable? and #forceable? are meant to be overridable.
-
#forceable?
makes sense in scenario where condition is capable of being force-satisfied regardless if -
its actually satisfied or not. Is true by default.
-
Override
#not_applicable?
when method is applicable only under some specific conditions. Is false by default. -
Rule requires a subject as first argument.
-
Multiple rules and rulesets can be combined into new ruleset as both share same interface and can be used interchangeably (composite pattern).
Forcing rules
On some occasions there is a situation in which some condition should be overridable. Let’s say we may want send shipping notification even though given order was not paid for and under regular circumstances such notification should not be sent. In this case, while regular logic with some automated process would not trigger delivery, an action triggered by user from UI could do it, by passing force: true option to #satisified? methods.
It might be good idea to test for #forceable? on the UI level to control visibility of such link/button.
Overriding #forceable
can be useful to prevent some edge cases, i.e. ContactInformationProvidedRule might check if customer for given order has provided any contact means by which a notification could be delivered. If not, ruleset containing such rule (and the rule itself) would not be “forceable” and UI could reflect that by querying #forceable?.
Regular and strong rulesets
While regular Ruleset can be satisfied or forced if any of its rules in not applicable, the StrongRuleset is not satisfied and not “forceable” if any of its rules is not applicable.
#not_applicable? vs #applicable?
It might be surprising that is is the negated version of the #applicable? predicate methods that is overridable. However, from the actual usage perspective, it usually easier to conceptually define when condition makes no sense than other way around.
Examples
Declaration
1 | class OrderIsSentRule < Rule |
Usage
1 | OrderIsPaidRule.new(order).satisfied? |
References
[1] [Selleo/pattern: A collection of lightweight, standardized, rails-oriented patterns. - https://github.com/Selleo/pattern(https://github.com/Selleo/pattern)
[2] rails-patterns | RubyGems.org | your community gem host - https://rubygems.org/gems/rails-patterns/
[6] - https://samuelmullen.com/2013/05/the-problem-with-rails-callbacks/