Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
RailsConf 2014 Talk, Tuesday, 2:30, Ballroom 4, Concerns, Decorators, Presenters, Service Objects, Helpers, Help Me Decide!
Rails Concerns
Code: Github Fat Code Refactoring Techniques Pull Request Concerns
Example: Breakout Concern Emailable from User model.
Why?
has_many
,belongs_to
,validates
,scope
, etc.)What?
a large model file into logical chunks with associated tests.
How?
multiple models. Prefer to discover a "domain" rather than some "technical"
aspect. A domain grouping is like
Taggable
. A technical grouping would belike
FinderMethods
or "ValidationMethods"app/models/concerns
.models, such as
app/models/user/
.extends ActiveSupport::Concern
. RubyMine has a nice default file template.and include the concern file in the model.
Steps
/apps/models/concerns/model_name
. Omit sub-directory andModelName::
ifonly one concern for given model or a shared concern.
included
block.include
statement in original model class:/spec/models/model_name_spec.rb
and into/spec/models/concerns/model_name/model_name_spec.rb
or/spec/models/concerns/concern_name_spec.rb
depending on how you created yourconcern.
Advantages
because using concerns involves simply moving the methods and tests into
separate files. And code accessing those methods does not need to change.
developers.
the right source and test file. Simpler than plain Ruby methods of
include
and
extend
.Disadvantages
organized. In other words, there's no real changes to the total public API
surface area of the model.
Alternatives
include
orextend
another Module.multiple models, you may create non-ActiveRecord models, especially in cases
where the functionality crosses multiple models.
Decorator is a better choice. See Draper Decorators.
Notes
concern into smaller files by having a parent concern
include
several childconcerns. See the Concerns API for an example.
break up a controller into several controllers that are referenced in the
routes.rb
file, so you may not need to use aConcern
for simply reducingthe size of a controller. In the controller case, concerns are useful for
sharing code without using inheritance.
Concerns References
concerns.
other concerns.