Skip to content

Commit

Permalink
Merge branch '1-3-stable'
Browse files Browse the repository at this point in the history
* 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 0290483
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 1 deletion.
27 changes: 27 additions & 0 deletions lib/spree/core/search/base_decorator.rb
Original file line number Diff line number Diff line change
@@ -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
2 changes: 1 addition & 1 deletion lib/spree_multi_lingual/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
41 changes: 41 additions & 0 deletions spec/lib/search/base_spec.rb
Original file line number Diff line number Diff line change
@@ -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
1 change: 1 addition & 0 deletions spec/spec_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 0290483

Please sign in to comment.