Skip to content

Commit

Permalink
Let ransack know about translations
Browse files Browse the repository at this point in the history
Since globalize 5 won't store translated data on default tables we need
to tweak the table / attribute name to let it fetch the data on the
right place.

This is just a proof of concept on what we could do to
improve the spree / ransack / globalize love here. Not sure this will
work with associations yet or even if it will work at all in all
scenarios
  • Loading branch information
huoxito authored and alepore committed Jul 4, 2015
1 parent a70bde3 commit 9d6c72f
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
17 changes: 17 additions & 0 deletions app/models/spree/globalize/translatable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,23 @@ module Translatable
included do
accepts_nested_attributes_for :translations
end

class_methods do
def ransack(params = {}, options = {})
names = params.keys

names.each do |n|
translated_attribute_names.each do |t|
if n.to_s.starts_with? t.to_s
params[:"translations_#{n}"] = params[n]
params.delete n
end
end
end

super(params, options)
end
end
end
end
end
5 changes: 5 additions & 0 deletions spec/models/product_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ module Spree
expect(product.taxons).to include(taxon)
end

it "handle translation in ransack" do
result = described_class.ransack(name_cont: product.name[0..2]).result
expect(result.first).to eq product
end

# Regression tests for #466
describe ".like_any" do
context "allow searching products through their translations" do
Expand Down

0 comments on commit 9d6c72f

Please sign in to comment.