I’m using ActiveSupport::Concern and I know there are a few approaches to organizing these into a /app/model/concern folder for say generic concerns but if I wanted to tie a concern to a specific model I have see a few approaches and would like to see some pros and cons
class Alert < ActiveRecord::Base
include Shareable
concerns in the /app/models/alert folder
class Alert
module Shareable
extends ActiveSupport::Concern
or
module Alert::Shareable
extends ActiveSupport::Concern
or
module Alert
module Shareable
extends ActiveSupport::Concern
Not really sure if there is a best way to do this or if I should use only modules or class module. I know it is trivial and they all seem to work but organizationally wasn’t sure if there is a best approach. Thanks!
If your model is
Alert, you definitely don’t wantmodule Alert(#3). #1 and #2 are basically the same, but more often you see the #2 style.Let me explain a little further.
The
module X::Ystyle will only work ifXhas already been defined. It’s saying “create this moduleYunderXand I don’t care ifXis a class or module, just do it.For #3, since
Alertis already defined as aclass, you’ll get this error:TypeError: Alert is not a module.Let me know if you need more clarification.