Permalink
Browse files

Added support for translated product fields searches.

When the search is made, only the current locale translation will be searched.
  • Loading branch information...
1 parent 6bd4c14 commit ae717e9fd21ed69e3ade259dcbff2abd012d7e55 @fmfdias fmfdias committed Nov 29, 2012
Showing with 19 additions and 0 deletions.
  1. +19 −0 app/models/spree/product_decorator.rb
@@ -44,5 +44,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.with_translations(I18n.locale)
+ end
+ self_scope.where([where_str, values.map { |value| "%#{value}%" } * fields.size].flatten)
+ end
+
end

0 comments on commit ae717e9

Please sign in to comment.