Handle search within translations fields #4

Closed
jipiboily opened this Issue Apr 24, 2012 · 5 comments

Comments

Projects
None yet
3 participants
@jipiboily
Owner

jipiboily commented Apr 24, 2012

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

This comment has been minimized.

Show comment Hide comment
@sbounmy

sbounmy Apr 24, 2012

Collaborator

@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

Collaborator

sbounmy commented Apr 24, 2012

@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

This comment has been minimized.

Show comment Hide comment
@olivierbuffon

olivierbuffon Aug 28, 2012

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

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

@jipiboily

This comment has been minimized.

Show comment Hide comment
@jipiboily

jipiboily Aug 29, 2012

Owner

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
Owner

jipiboily commented Aug 29, 2012

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

This comment has been minimized.

Show comment Hide comment
@olivierbuffon

olivierbuffon Aug 29, 2012

Oh thanks ! Exactly what I tried to do !

Oh thanks ! Exactly what I tried to do !

@jipiboily

This comment has been minimized.

Show comment Hide comment
@jipiboily

jipiboily Aug 29, 2012

Owner

My pleasure!

Owner

jipiboily commented Aug 29, 2012

My pleasure!

j15e added a commit to hooktstudios/spree_multi_lingual that referenced this issue Feb 27, 2013

Multilingual search. Ref #4
- Based on @jipiboily solution, made it cleaner using chained method to avoid repeating spree core search logic & made cleaner SQL query building.

j15e added a commit to hooktstudios/spree_multi_lingual that referenced this issue Feb 27, 2013

Multilingual search. Ref #4
- Based on @jipiboily solution, made it cleaner using chained method to avoid repeating spree core search logic & made cleaner SQL query building.

@j15e j15e referenced this issue Feb 27, 2013

Closed

Multilingual search #36

sbounmy added a commit that referenced this issue Apr 2, 2013

Merge branch 'master' of https://github.com/hooktstudios/spree_multi_…
…lingual into 1-3-stable

* 'master' of https://github.com/hooktstudios/spree_multi_lingual:
  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
  Update email translations (add accents)
  Add translations for Spree::ShippingMethod (no UI controls yet)
  Add translations for Spree::TaxRate (no UI controls yet)

Conflicts:
	README.md
	spree_multi_lingual.gemspec

sbounmy added a commit that referenced this issue Apr 2, 2013

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

@sbounmy sbounmy closed this Apr 2, 2013

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment