Shared scopes via module?
Asked Answered
S

3

28

I want to DRY up several models by moving shared scopes into a module, something like:

module CommonScopes
  extend ActiveSupport::Concern

  module ClassMethods
    scope :ordered_for_display, order("#{self.to_s.tableize}.rank asc")
  end
end

I also want to create shared specs that test the module. Unfortunately when I try to include the shared scope in my model I get:

undefined method `order' for CommonScopes::ClassMethods:Module

Any ideas? Thanks!

Scott answered 6/9, 2011 at 17:31 Comment(0)
B
17

You can use instance_eval

module CommonScopes
  extend ActiveSupport::Concern

  def self.included(klass)
    klass.instance_eval do
      scope :ordered_for_display, order("#{self.to_s.tableize}.rank asc")
    end
  end
end
Burkhalter answered 6/9, 2011 at 17:54 Comment(4)
Don't you think lambdas should be preferred now?Eleonoreleonora
@Eleonoreleonora I believe calling scope without a lambda is deprecated in rails 4.Burkhalter
exactly what I was saying :) (but I think it is just the preferred syntax for now, and not deprecated yet)Eleonoreleonora
This is also sort of wrong in that it nullifies the point of extending ActiveSupport::Concern. The scope should either go in a block like included do; ... end since that is part of ActiveRecord::Concern, or we should not bother extending it, since the above self.included syntax is built into every Ruby module, and has nothing to do with ActiveSupport::Concern. Ideally we pick one way or the other, since ActiveSupport::Concern takes care of that boilerplate code that we're reimplementing in this answer's example. This is for both Rails 3 and 4, as in mdemolin's answerPeaceful
E
68

As in rails 4 scope syntax you can simply use a lambda to delay the execution of the code (works in rails 3 too):

module CommonScopes
  extend ActiveSupport::Concern

  included do
    scope :ordered_for_display, -> { order("#{self.to_s.tableize}.rank asc") }
  end
end
Eleonoreleonora answered 26/11, 2013 at 14:3 Comment(0)
B
17

You can use instance_eval

module CommonScopes
  extend ActiveSupport::Concern

  def self.included(klass)
    klass.instance_eval do
      scope :ordered_for_display, order("#{self.to_s.tableize}.rank asc")
    end
  end
end
Burkhalter answered 6/9, 2011 at 17:54 Comment(4)
Don't you think lambdas should be preferred now?Eleonoreleonora
@Eleonoreleonora I believe calling scope without a lambda is deprecated in rails 4.Burkhalter
exactly what I was saying :) (but I think it is just the preferred syntax for now, and not deprecated yet)Eleonoreleonora
This is also sort of wrong in that it nullifies the point of extending ActiveSupport::Concern. The scope should either go in a block like included do; ... end since that is part of ActiveRecord::Concern, or we should not bother extending it, since the above self.included syntax is built into every Ruby module, and has nothing to do with ActiveSupport::Concern. Ideally we pick one way or the other, since ActiveSupport::Concern takes care of that boilerplate code that we're reimplementing in this answer's example. This is for both Rails 3 and 4, as in mdemolin's answerPeaceful
K
2

Because your scope method is called immediately when your module is parsed by Ruby and it's not accessible from your CommonScopes module..

But you can replace your scope call by a class method:

module CommonScopes
  extend ActiveSupport::Concern

  module ClassMethods
    def ordered_for_display
      order("#{self.to_s.tableize}.rank asc")
     end
  end
end
Kathlyn answered 6/9, 2011 at 17:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.