Permalink
Browse files

Merge branch '1-3-stable'

* 1-3-stable:
  using quoted translation table name from product model
  Fix for SQL injections, use ActiveRecord querying
  Search for Spree 1.2 updated logic
  Update spree version to 1.2.x
  Multilingual search. Ref #4
  • Loading branch information...
sbounmy committed Apr 2, 2013
2 parents 787753d + d50b1f3 commit 02904834c73003671ac5ebce74ffef6067e85e02
@@ -0,0 +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
+
+ 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
+
+ alias_method_chain :get_base_scope, :multi_lingual
+
+end
@@ -20,7 +20,7 @@ class Engine < Rails::Engine
end
def self.activate
- Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
+ Dir.glob(File.join(File.dirname(__FILE__), "../../{app,lib}/**/*_decorator*.rb")) do |c|
Rails.configuration.cache_classes ? require(c) : load(c)
end
end
@@ -0,0 +1,41 @@
+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
View
@@ -31,6 +31,7 @@
# instead of true.
config.use_transactional_fixtures = true
config.include Spree::UrlHelpers
+ config.include FactoryGirl::Syntax::Methods
config.include Capybara::DSL
config.after(:each) do

0 comments on commit 0290483

Please sign in to comment.