如何覆盖lib / spree / search / base.rb

我需要覆盖get_products_conditions_for中的get_products_conditions_for方法,这样做的最佳方法是什么?

我尝试将其添加到初始化程序中:

 Spree::Search::Base.class_eval do def get_products_conditions_for(base_scope, query) base_scope.like_any([:name, :description], query.split) | base_scope.joins("JOIN taggings on taggings.taggable_id = spree_products.id JOIN tags on tags.id = taggings.tag_id").where("tags.name = ?", query.split) end end 

启动服务器时导致此错误: uninitialized constant Spree::Search (NameError)

我也尝试将其添加到“/lib/spree/search/base.rb”和“/lib/spree/search/tags_search.rb”

 module Spree::Search class TagsSearch < Spree::Search::Base def get_products_conditions_for(base_scope, query) base_scope.like_any([:name, :description], query.split) | base_scope.joins("JOIN taggings on taggings.taggable_id = spree_products.id JOIN tags on tags.id = taggings.tag_id").where("tags.name = ?", query.split) end end end 

然后Spree::Config.searcher = TagsSearch中的Spree::Config.searcher = TagsSearch

我甚至尝试通过在应用程序内的同一目录结构中放置副本来完全替换文件,或者没有任何反应,或者我得到上述错误……

我要做的是集成acts_as_taggable_on ,这已经完成并正常工作,但搜索显然不会返回这些标签的结果……

编辑:好的,所以在Steph的回答之后我尝试过:

 module Spree::Search class TagsSearch < Spree::Search::Base def get_products_conditions_for(base_scope, query) base_scope.like_any([:name, :description], query.split) | base_scope.joins("JOIN taggings on taggings.taggable_id = spree_products.id JOIN tags on tags.id = taggings.tag_id").where("tags.name = ?", query.split) end end end 

app/models/search/tags_search.rb和Steph的lib/spree/search/tags_search.rb的代码建议

还有这个:

 config.to_prepare do Spree::Core::Search::Base.send(:include, TagsSearch) end 

config/environments/development.rb

启动服务器时会导致以下结果:

uninitialized constant TagsSearch (NameError)

我需要覆盖此类中的get_products_conditions_for方法,这样做的最佳方法是什么?

在这种特殊情况下,我inheritance了该类并覆盖了所需的方法。

和施普雷3一样,

  1. 在config / initializers / spree.rb中,Spree.config块config.searcher_class= Spree::MySearch
  2. 使用以下内容创建文件lib / spree / my_search.rb:

     module Spree class MySearch < Spree::Core::Search::Base def method_to_be_overridden # Your new definition here end end end 

    注意:以上是修改Spree中搜索者类的规定方法

我建议使用ActiveSupport :: Concern ,它可能看起来像这样:

 module YourAwesomeModule extend ActiveSupport::Concern included do alias :spree_get_products_conditions_for :get_products_conditions_for def get_products_conditions_for(base_scope, query) custom_get_products_conditions_for(base_scope, query) end end module InstanceMethods def custom_get_products_conditions_for(base_scope, query) #your stuff end end end Spree::Core::Search::Base.send(:include, YourAwesomeModule) 

这是做了几件事:

  • 使用ActiveSupport :: Concern是一种很好/干净的方法来扩展给定类的类和实例方法。
  • 别名保留了Spree方法。 如果你不愿意,你不必这样做。

在开发过程中,因为默认配置设置导致非缓存类但不重新加载lib /模块,您可能需要在config / environments / development.rb中添加它:

 config.to_prepare do Spree::Core::Search::Base.send(:include, YourAwesomeModule) end