Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Include the table name in the with_x_translation #15

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/json_translate/translates.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def translates(*attrs, allow_blank: false)
end

define_singleton_method "with_#{attr_name}_translation" do |value, locale = I18n.locale|
quoted_translation_store = connection.quote_column_name("#{attr_name}#{SUFFIX}")
quoted_translation_store = connection.quote_table_name("#{self.table_name}.#{attr_name}#{SUFFIX}")
translation_hash = { "#{locale}" => value }

if MYSQL_ADAPTERS.include?(connection.adapter_name)
Expand Down
12 changes: 12 additions & 0 deletions test/test_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,15 @@
I18n.available_locales = [:en, :fr]

class Post < ActiveRecord::Base
has_many :tags
translates :title, :body_1

scope :tagged, -> (tag_title) { joins(:tags).merge(Tag.with_title_translation(tag_title)) }
end

class Tag < ActiveRecord::Base
belongs_to :post
translates :title
end

class PostDetailed < Post
Expand Down Expand Up @@ -53,6 +61,10 @@ def create_table
t.column :body_1_translations, column_type
t.column :comment_translations, column_type
end
connection.create_table(:tags, :force => true) do |t|
t.column :title_translations, column_type
t.column :post_id, :integer
end
end
end

Expand Down
8 changes: 8 additions & 0 deletions test/translates_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,14 @@ def test_with_translation_relation
end
end

def test_with_translation_when_ambiguous_column
p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" })
Tag.create!(:title_translations => { "en" => "A Tag", "fr" => "Un tag" }, post: p)
I18n.with_locale(:en) do
assert_equal p.title_en, Post.tagged("A Tag").first.try(:title)
Comment on lines +242 to +245
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" })
Tag.create!(:title_translations => { "en" => "A Tag", "fr" => "Un tag" }, post: p)
I18n.with_locale(:en) do
assert_equal p.title_en, Post.tagged("A Tag").first.try(:title)
post = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" })
Tag.create!(:title_translations => { "en" => "A Tag", "fr" => "Un tag" }, post: post)
I18n.with_locale(:en) do
assert_equal post.title_en, Post.tagged("A Tag").first.title

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'd be happy to make that change, but I originally went with p because that's what was used in all the tests. I think a change like that should be a separate PR and fix update all existing tests in one pass. What do you think?

end
end

def test_with_interpolation_arguments
p = Post.create!(:title_translations => { "en" => "Alice in %{where}" })
I18n.with_locale(:en) do
Expand Down