Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

better search approach and now returning product without translation …

  • Loading branch information...
commit 70f0a62de2cc726309005ba6e21b737bc6a40928 1 parent f4e9c4f
@sbounmy sbounmy authored
View
19 app/models/spree/product_decorator.rb
@@ -46,5 +46,24 @@ def duplicate
p.save!
p
end
+
+ def self.like_any(fields, values)
+ has_translated_fields = false
+ where_str = fields.map do |field|
+ if self.translated?(field)
+ has_translated_fields = true
+ next Array.new(values.size, "#{self.translation_class.quoted_table_name}.#{field} #{LIKE} ?").join(' OR ')
+ end
+ next Array.new(values.size, "#{self.quoted_table_name}.#{field} #{LIKE} ?").join(' OR ')
+ end
+ where_str = where_str.join(' OR ')
+ self_scope = self
+ #only use translations scope if we are searching translated fields.
+ if has_translated_fields
+ self_scope = self_scope.joins(:translations).where("#{self.translation_class.quoted_table_name}.locale = ?", I18n.locale)
+ end
+
+ self_scope.where([where_str, values.map { |value| "%#{value}%" } * fields.size].flatten)
+ end
end
View
46 lib/spree/core/search/base_decorator.rb
@@ -1,27 +1,27 @@
-Spree::Core::Search::Base.class_eval do
- def get_products_conditions_for(base_scope, query)
- if query.blank?
- base_scope
- else
- conditions = []
- keywords = []
- query.split.each do |keyword|
- [:name, :description].map do |field|
- conditions << "`#{Spree::Product.translations_table_name}`.#{field} LIKE ?"
- keywords << "%#{keyword}%"
- end
- end
- base_scope.where(conditions.join(' OR '), *keywords)
- end
- end
+# Spree::Core::Search::Base.class_eval do
+# def get_products_conditions_for(base_scope, query)
+# if query.blank?
+# base_scope
+# else
+# conditions = []
+# keywords = []
+# query.split.each do |keyword|
+# [:name, :description].map do |field|
+# conditions << "`#{Spree::Product.translations_table_name}`.#{field} LIKE ?"
+# keywords << "%#{keyword}%"
+# end
+# end
+# base_scope.where(conditions.join(' OR '), *keywords)
+# end
+# end
- protected
+# protected
- def get_base_scope_with_multi_lingual
- base_scope = Spree::Product.active.joins(:translations).where("locale = ?", I18n.locale)
- base_scope.merge(get_base_scope_without_multi_lingual)
- end
+# def get_base_scope_with_multi_lingual
+# base_scope = Spree::Product.active.joins(:translations).where("locale = ?", I18n.locale)
+# base_scope.merge(get_base_scope_without_multi_lingual)
+# end
- alias_method_chain :get_base_scope, :multi_lingual
+# alias_method_chain :get_base_scope, :multi_lingual
-end
+# end
View
41 spec/lib/search/base_spec.rb
@@ -1,41 +0,0 @@
-require 'spec_helper'
-
-describe Spree::Core::Search::Base do
-
- before do
- include ::Spree::ProductFilters
- @product1 = create(:product, :name => "RoR Shirt", :name_fr => "RoR Chemise", :name_es => "RoR Camisa", :price => 9.00, :on_hand => 1)
- @product2 = create(:product, :name => "Trouser", :name_fr => "Pantalon", :name_es => "Pantalones", :price => 9.00, :on_hand => 1)
- end
-
- it "returns all products by default" do
- params = { :per_page => "" }
- searcher = Spree::Core::Search::Base.new(params)
- searcher.retrieve_products.count.should == 2
- end
-
- it 'can return translated product name with correct i18n' do
- params = { :per_page => "",
- :keywords => 'Camisa' }
- I18n.locale = :es
- searcher = Spree::Core::Search::Base.new(params)
- searcher.retrieve_products.should == [@product1]
- end
-
- it 'should not return with incorrect i18n' do
- params = { :per_page => "",
- :keywords => 'Camisa' }
- I18n.locale = :fr
- searcher = Spree::Core::Search::Base.new(params)
- searcher.retrieve_products.should == []
- end
-
- it 'should not return product base attribute name' do
- params = { :per_page => "",
- :keywords => 'Shirt' }
- I18n.locale = :fr
- searcher = Spree::Core::Search::Base.new(params)
- searcher.retrieve_products.should == []
- end
-
-end
Please sign in to comment.
Something went wrong with that request. Please try again.