Reputation: 1849
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!
Upvotes: 1
Views: 905
Reputation: 3890
If your model is Alert
, you definitely don't want module 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::Y
style will only work if X
has already been defined. It's saying "create this module Y
under X
and I don't care if X
is a class or module, just do it.
For #3, since Alert
is already defined as a class
, you'll get this error: TypeError: Alert is not a module
.
Let me know if you need more clarification.
Upvotes: 2