0
私はこのミックスインを改善しようとしています。私には気になる点があります。モジュールの残りの部分をself.included(ベース)の基本プロパティ私はすべてのモジュールメソッドに基底を渡す必要があります。これを行うための良い方法はあります:このミックスを改善するには
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
1つの選択肢は、 'ベースを保持するために、クラスレベルの変数を追加することです'を実行し、それを削除します。個人的には、あなたがすでにしているように、代わりに引数を渡すことをお勧めします。 –