TIL: Cool Rails concerns
by Graham Marlow
There's something super elegant about Writebook's
use of concerns. I especially like Book:Sluggable
:
module Book::Sluggable
extend ActiveSupport::Concern
included do
before_save :generate_slug, if: -> { slug.blank? }
end
def generate_slug
self.slug = title.parameterize
end
end
Here's a few reasons:
- Nesting concerns in a model folder is neat when that concern is an
encapsulation of model-specific functionality:
app/models/book/sluggable.rb
. - Concerns don't have to be big. They do have to be single-purpose.
- Reminds me of a great article by Jorge Manrubla: Vanilla Rails is plenty. Down with service objects!