3

このクラスのget_products_conditions_forメソッドをオーバーライドする必要があります。これを行う最善の方法は何ですか?lib/spree/search/base.rbをオーバーライドする方法

私は初期にこれを追加しようとしました:

サーバー起動時にこのエラーが発生し
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に/まくります/検索/ 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 

、その後application.rbでSpree::Config.searcher = TagsSearch ...

私も完全に私が何をしようとしている...

をされたアプリ内で同じディレクトリ構造にレプリカを配置することにより、ファイルを交換する、のいずれか何も起こらない、または私が言及したエラーを取得しようとしました...

EDITを行い、働いているacts_as_taggable_onを統合し、しかし検索は明らかにこれらのタグから結果を返しません: [OK]をので、ステフの答えの後に私が試した:で

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 

を3210およびlib/spree/search/tags_search.rb

これでステフのコードの提案:私はお勧め

uninitialized constant TagsSearch (NameError)

+0

あなたはどうやってインストールされていますか?宝石?プラグイン?スプレーの読み順のような音が問題です。 – siannopollo

+0

宝石として、それは他の何かのようになるとは思わない... –

答えて

0

:サーバを起動するときに、次になりconfig/environments/development.rb

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

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のを使用して

  • ::懸念は、所与のクラスのクラスとインスタンスメソッドを拡張するための素敵な/きれいな方法です。
  • エイリアスは、Spreeメソッドを保持します。あなたがしたくない場合は、このビットを行う必要はありません。

デフォルトの設定の設定は、非キャッシュクラスになりますがlibに/モジュールをリロードないので、開発中に、あなたはこの内部の設定/環境/ development.rb追加する必要があるかもしれませんが:

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

感謝、私は応答で私の質問を編集しました... –

+1

TagSearchで名前空間と継承を削除してみてください。 Spree :: Search :: Baseに属している必要はなく、Spree :: Search :: Baseから継承する必要もありません。私の例では、Rails.root/lib/your_awesome_module.rbには、私が記述したモジュールが含まれています。また、ActiveSupport :: concernとインクルードビットを含める必要があります。 –

+0

ご迷惑をおかけして申し訳ありません。 config/environments/development.rbのlib/awesome_search.rbとconfig.to_prepareブロックにあなたのコードがありますが、まだ似たようなエラーが出ています: "初期化されていない定数AwesomeSearch(NameError)" –

1

このクラスのget_products_conditions_forメソッドをオーバーライドする必要がありますが、これを行う最善の方法は何ですか?

この特定のケースでは、クラスを継承し、必要なメソッドをオーバーライドしました。天国3のよう

config.searcher_class= Spree::MySearch

  • は以下の内容を含むファイルのlib /酒宴/ my_search.rbを作成

    設定/イニシャライザ/ spree.rbで
    1. 、Spree.configブロック:

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

      注:上記はスプリー

    2. にサーチャー・クラスを変更するための所定の方法であります
  • 関連する問題