jipiboily / spree_multi_lingual

Spree multi-lingual is a plugin to make multi locale store with Spree possible
BSD 3-Clause "New" or "Revised" License
49 stars 56 forks source link

Handle search within translations fields #4

Closed jipiboily closed 11 years ago

jipiboily commented 12 years ago

Currently, search is only searching in Spree::Product and not in the the Spree::Product::Translation (spree_product_translations). I did something in my project about that, but maybe something less custom could be done within this extension.

@sbounmy have you faced that problem too?

sbounmy commented 12 years ago

@jipiboily honnestly, I haven't tried yet to use the search. Would be great to support it, but seems like Spree 1.1.X is using ransack instead of solr

olivierbuffon commented 12 years ago

@jipiboily Can you share what you've done to fix your search problem ? Even if it's sketchy...

jipiboily commented 12 years ago

This is some dirty fix in a decorator, but this works for now (would need a better version):

Spree::Core::Search::Base.class_eval do
  def get_products_conditions_for(base_scope, query)
    # completely rewritten original method to make it work based on locale.
    sql = ""
    query.split.each_with_index do |keyword, i|
      sql += " OR " if i > 0
      sql += " spree_product_translations.name LIKE '%#{keyword}%' "
      sql += " OR spree_product_translations.description LIKE '%#{keyword}%' "
      sql += " OR spree_product_translations.caracteristiques LIKE '%#{keyword}%' "
      sql += " OR spree_products.south_shore_article_id LIKE '%#{keyword}%' "
    end
    base_scope.where(sql)
  end

  protected
    def get_base_scope
      base_scope = Spree::Product.active.joins(:translations).where("locale = ?", I18n.locale)
      # useless for us
      # base_scope = base_scope.in_taxon(taxon) unless taxon.blank?
      base_scope = get_products_conditions_for(base_scope, keywords) unless keywords.blank?
      # useless for us
      # base_scope = base_scope.on_hand unless Spree::Config[:show_zero_stock_products]
      base_scope = add_search_scopes(base_scope)
      base_scope
    end
end
olivierbuffon commented 12 years ago

Oh thanks ! Exactly what I tried to do !

jipiboily commented 12 years ago

My pleasure!