Rails: Is that possible to define named scope in a module?

爷,独闯天下 提交于 2019-11-28 18:41:14

Yes it is

module Foo
  def self.included(base)
    base.class_eval do
      scope :your_scope, lambda {}
    end
  end
end

As of Rails 3.1 the syntax is simplified a little by ActiveSupport::Concern:

Now you can do

require 'active_support/concern'

module M
  extend ActiveSupport::Concern

  included do
    scope :disabled, where(:disabled => true)
  end

  module ClassMethods
   ...
  end
end

ActiveSupport::Concern also sweeps in the dependencies of the included module, here is the documentation

[update, addressing aceofbassgreg's comment]

The Rails 3.1 and later ActiveSupport::Concern allows an include module's instance methods to be included directly, so that it's not necessary to create an InstanceMethods module inside the included module. Also it's no longer necessary in Rails 3.1 and later to include M::InstanceMethods and extend M::ClassMethods. So we can have simpler code like this:

require 'active_support/concern'
module M
  extend ActiveSupport::Concern
  # foo will be an instance method when M is "include"'d in another class
  def foo
    "bar"
  end

  module ClassMethods
    # the baz method will be included as a class method on any class that "include"s M
    def baz
      "qux"
    end
  end

end

class Test
  # this is all that is required! It's a beautiful thing!
  include M
end

Test.new.foo # ->"bar"
Test.baz # -> "qux"

As for Rails 4.x you can use gem scopes_rails

It can generate scopes file and include it to your model.

Also, it can automatically generate scopes for state_machines states.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!