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

query methods work with column with _translations suffix #10

Open
wants to merge 2 commits 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
8 changes: 8 additions & 0 deletions lib/awesome_hstore_translate/active_record/class_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,14 @@ def with_fallbacks(&block)
translation_options[:fallbacks] = before_state
end

def get_column_name(attr)
column_name = attr.to_s
# detect column from original hstore_translate
column_name << '_translations' if !has_attribute?(column_name) && has_attribute?("#{column_name}_translations")

column_name
end

protected

def toggle_fallback
Expand Down
14 changes: 2 additions & 12 deletions lib/awesome_hstore_translate/active_record/instance_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def read_translated_attribute(attr, locale = I18n.locale)
end

def read_raw_attribute(attr)
read_attribute(get_column_name(attr))
read_attribute(self.class.get_column_name(attr))
end

def write_translated_attribute(attr, value, locale= I18n.locale)
Expand All @@ -31,22 +31,12 @@ def write_translated_attribute(attr, value, locale= I18n.locale)
end

def write_raw_attribute(attr, value)
write_attribute(get_column_name(attr), value)
write_attribute(self.class.get_column_name(attr), value)
end

def get_fallback_for_locale(locale)
I18n.fallbacks[locale] if I18n.respond_to?(:fallbacks)
end

private

def get_column_name(attr)
column_name = attr.to_s
# detect column from original hstore_translate
column_name << '_translations' if !has_attribute?(column_name) && has_attribute?("#{column_name}_translations")

column_name
end
end
end
end
9 changes: 8 additions & 1 deletion lib/awesome_hstore_translate/active_record/query_methods.rb
Original file line number Diff line number Diff line change
Expand Up @@ -58,10 +58,17 @@ def order(*args)
private

def translated_attributes(opts)
opts.select{ |key, _| self.translated_attribute_names.include?(key) }
return opts unless self.respond_to?(:translated_attribute_names)

opts
.select{ |key, _| self.translated_attribute_names.include?(key) }
.map{ |key, value| [get_column_name(key), value] }
.to_h
end

def untranslated_attributes(opts)
return opts unless self.respond_to?(:translated_attribute_names)

opts.reject{ |key, _| self.translated_attribute_names.include?(key) }
end
end
Expand Down
2 changes: 1 addition & 1 deletion lib/awesome_hstore_translate/version.rb
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module AwesomeHstoreTranslate
VERSION = '0.3.1'
VERSION = '0.3.2'
end
23 changes: 23 additions & 0 deletions test/awesome_hstore_translate_legacy_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -140,4 +140,27 @@ def test_persists_translations_assigned_to_localized_accessors
def test_class_method_translates?
assert_equal true, PageWithoutFallbacks.translates?
end

def test_where_query_with_translated_value
PageWithFallbacks.create!(:title_raw => {'en' => 'English title', 'de' => 'Deutscher Titel'})
exp = PageWithFallbacks.create!(:title_raw => {'en' => 'Another English title', 'de' => 'Noch ein Deutscher Titel'})
res = PageWithFallbacks.where(title: 'Another English title').first
assert_equal(exp.id, res.id)
end

def test_where_query_with_translated_value_and_other
PageWithFallbacks.create!(:title_raw => {'en' => 'English title', 'de' => 'Deutscher Titel'},
author: 'Awesome')
exp = PageWithFallbacks.create!(:title_raw => {'en' => 'English title', 'de' => 'Deutscher Titel'},
author: 'Spectacular')
res = PageWithFallbacks.where(title: 'English title', author: 'Spectacular').first
assert_equal(exp.id, res.id)
end

def test_find_by_query_with_translated_value
PageWithFallbacks.create!(:title_raw => {'en' => 'English title', 'de' => 'Deutscher Titel'})
exp = PageWithFallbacks.create!(:title_raw => {'en' => 'Another English title', 'de' => 'Noch ein Deutscher Titel'})
res = PageWithFallbacks.find_by(title: 'Another English title')
assert_equal(exp.id, res.id)
end
end