Skip to content
This repository
Browse code

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...
commit 02904834c73003671ac5ebce74ffef6067e85e02 2 parents 787753d + d50b1f3
Bounmy Stéphane authored April 02, 2013
27  lib/spree/core/search/base_decorator.rb
... ...
@@ -0,0 +1,27 @@
  1
+Spree::Core::Search::Base.class_eval do
  2
+  def get_products_conditions_for(base_scope, query)
  3
+    if query.blank?
  4
+      base_scope
  5
+    else
  6
+      conditions = []
  7
+      keywords = []
  8
+      query.split.each do |keyword|
  9
+        [:name, :description].map do |field|
  10
+          conditions << "`#{Spree::Product.translations_table_name}`.#{field} LIKE ?"
  11
+          keywords << "%#{keyword}%"
  12
+        end
  13
+      end
  14
+      base_scope.where(conditions.join(' OR '), *keywords)
  15
+    end
  16
+  end
  17
+
  18
+  protected
  19
+
  20
+    def get_base_scope_with_multi_lingual
  21
+      base_scope = Spree::Product.active.joins(:translations).where("locale = ?", I18n.locale)
  22
+      base_scope.merge(get_base_scope_without_multi_lingual)
  23
+    end
  24
+
  25
+    alias_method_chain :get_base_scope, :multi_lingual
  26
+
  27
+end
2  lib/spree_multi_lingual/engine.rb
@@ -20,7 +20,7 @@ class Engine < Rails::Engine
20 20
     end
21 21
 
22 22
     def self.activate
23  
-      Dir.glob(File.join(File.dirname(__FILE__), "../../app/**/*_decorator*.rb")) do |c|
  23
+      Dir.glob(File.join(File.dirname(__FILE__), "../../{app,lib}/**/*_decorator*.rb")) do |c|
24 24
         Rails.configuration.cache_classes ? require(c) : load(c)
25 25
       end
26 26
     end
41  spec/lib/search/base_spec.rb
... ...
@@ -0,0 +1,41 @@
  1
+require 'spec_helper'
  2
+
  3
+describe Spree::Core::Search::Base do
  4
+
  5
+  before do
  6
+    include ::Spree::ProductFilters
  7
+    @product1 = create(:product, :name => "RoR Shirt", :name_fr => "RoR Chemise", :name_es => "RoR Camisa", :price => 9.00, :on_hand => 1)
  8
+    @product2 = create(:product, :name => "Trouser", :name_fr => "Pantalon", :name_es => "Pantalones", :price => 9.00, :on_hand => 1)
  9
+  end
  10
+
  11
+  it "returns all products by default" do
  12
+    params = { :per_page => "" }
  13
+    searcher = Spree::Core::Search::Base.new(params)
  14
+    searcher.retrieve_products.count.should == 2
  15
+  end
  16
+
  17
+  it 'can return translated product name with correct i18n' do
  18
+    params = { :per_page => "",
  19
+      :keywords => 'Camisa' }
  20
+    I18n.locale = :es
  21
+    searcher = Spree::Core::Search::Base.new(params)
  22
+    searcher.retrieve_products.should == [@product1]
  23
+  end
  24
+
  25
+  it 'should not return with incorrect i18n' do
  26
+    params = { :per_page => "",
  27
+      :keywords => 'Camisa' }
  28
+    I18n.locale = :fr
  29
+    searcher = Spree::Core::Search::Base.new(params)
  30
+    searcher.retrieve_products.should == []
  31
+  end
  32
+
  33
+  it 'should not return product base attribute name' do
  34
+    params = { :per_page => "",
  35
+      :keywords => 'Shirt' }
  36
+    I18n.locale = :fr
  37
+    searcher = Spree::Core::Search::Base.new(params)
  38
+    searcher.retrieve_products.should == []
  39
+  end
  40
+
  41
+end
1  spec/spec_helper.rb
@@ -31,6 +31,7 @@
31 31
   # instead of true.
32 32
   config.use_transactional_fixtures = true
33 33
   config.include Spree::UrlHelpers
  34
+  config.include FactoryGirl::Syntax::Methods
34 35
   config.include Capybara::DSL
35 36
 
36 37
   config.after(:each) do

0 notes on commit 0290483

Please sign in to comment.
Something went wrong with that request. Please try again.