我正在尝试改进这个mixin并且困扰我的一件事是我似乎无法让其他模块知道self.included(base)中的基本属性所以我有将base传递给每个模块方法。有没有更好的方法:
module SearchSort
def self.included(base)
# binds included class's class methods
base.send :extend, ClassMethods
initialize_scopes(base)
end
def self.initialize_scopes(base)
initialize_type_scope(base)
initialize_product_name_scope(base)
end
def self.initialize_type_scope(base)
base.scope :for_work_type, lambda { |work_type|
Rails.logger.debug("-----(45) work_type #{work_type}")
terms = process_terms(work_type)
base.where(
terms.map { '(LOWER(workable_type) LIKE ?)' }.join(' AND '),
*terms.map { |e| [e] * 1 }.flatten)
}
end
def self.initialize_product_name_scope(base)
base.scope :for_product_name, lambda { |product_name|
terms = process_terms(product_name)
base.where(
terms.map { '(LOWER(products.name) LIKE ?)' }.join(' AND '),
*terms.map { |e| [e] * 1 }.flatten
).joins(:product)
}
end
module ClassMethods
def pid_opts
[%w(Newly\ Added newly_added), %w(Waiting waiting),
%w(Ready ready), %w(Working working),
%w(Error error), %w('Error Retry', 'error_retry'),
%w(Done done), %w(Gated gated)
]
end
end
end
答案 0 :(得分:0)
在included
方法中使用自动注册魔术,我只是在模块中添加初始化方法,如initialize_scopes
,并使用该模块从类扩展的地方调用
extend SearchSort
initialize_scopes
因为使用了extend,所以模块中定义的方法在类上下文中执行(它都是关于self
的上下文)。
举个例子,我将这种模式用于acts_as_api
:
module ApiHandling
def expose_api(*fields)
acts_as_api
api_accessible :ios_v1 do |template|
fields.each { |field| template.add field }
end
end
end
像这样使用:
class Event < ActiveRecord::Base
extend ApiHandling
expose_api :id, :name, :description, ...