hstore_translate 0.0.11 → 0.0.12

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,15 +1,15 @@
1
1
  ---
2
2
  !binary "U0hBMQ==":
3
3
  metadata.gz: !binary |-
4
- YWNkYWRkZDNmOGJhYWZmNTlhOWQ1NGZjZGMyYjMyYTg5ZmQ2YmYwYg==
4
+ NmE2YTg4ZTcyNWJhMGI4OTljODc4NDY0Yzg4MzYwZTJiMDI4NjcyYQ==
5
5
  data.tar.gz: !binary |-
6
- MTAxYWNiZTkwZTEzNzlmODI0Njk1NzczZGQ0ZmFjOWQ2NDk2YTUxNA==
6
+ ZGQyYjIyN2M3YmQ4YjFkN2EwN2MzODhjMTU1ODBlMDA4YmNkNTg1OA==
7
7
  SHA512:
8
8
  metadata.gz: !binary |-
9
- OWI0N2RjN2IyZTZmNWZkZTcyZDdiMDZhZWNlOTVmZjE2MWI5OWFiMDc0YWQ3
10
- MjczOGY5ZTBhZTRiZGQ1YWY3MmM3YWExYmExNjYzNjM4ZTdlNTM3ODhkN2Zi
11
- NjhiM2ZhNmYzOTA2Zjk5NzUyZjYxYzM2MjliNWE2NjJmMmEyMTg=
9
+ OWMwMjQ1MmQzYzQzNTBhMGUwYzZkOGM1M2IxZmM1OWZiZjEyOGI2ZDQwZDg3
10
+ ZTY5YzA4MGQ4MDgxYzIzZTYwNDIwMzc2NTU4Mzg1YTZhMDljYzg1YjAyNjA0
11
+ OWE3ZTJhMzkxOTkwZjQ2MzA1ODczMzkwNWRhNjg1YzZjYTEyM2U=
12
12
  data.tar.gz: !binary |-
13
- NjJlN2MzZDc4YjFjZjRlYzg4MmZiODkzMzVlYWY3MThmZWM5MGZlYzBjMjUx
14
- ZmZhNTY3OTk3ZDcyNjk2YjM5MjhkZTUzNWE3MTdhZmFmOTY0OTAzY2NhNDMz
15
- Y2JjOTM3ZmFiOTQwNGUwMjdlYjI4N2IzNTI2ZWY5ZTUwODQxNmI=
13
+ N2UzNDYyZjJmNDNmMTdhNzk4MjUxZDZjZDkwYWQ2Y2NhZjk5Zjc2ODEyM2Ez
14
+ MjUxZTc2ZTQyM2QxMGUyOWZkNTYwOTgwYzdmMmMwY2VlNGNkNDA2NmE3OTdm
15
+ MjgxYjA1ZWMyOThkMDA4MTdkOTgzZGFkOWY5NDVmYjgxZDY3MDA=
data/README.md CHANGED
@@ -55,6 +55,13 @@ post.title # => This database rocks!
55
55
  post.title_he # => אתר זה טוב
56
56
  ```
57
57
 
58
+ To find records using translations without constructing hstore queries by hand:
59
+
60
+ ```ruby
61
+ Post.with_title_translation("This database rocks!") # => #<ActiveRecord::Relation ...>
62
+ Post.with_title_translation("אתר זה טוב", :he) # => #<ActiveRecord::Relation ...>
63
+ ```
64
+
58
65
  In order to make this work, you'll need to define an hstore column for each of
59
66
  your translated attributes, using the suffix "_translations":
60
67
 
@@ -9,15 +9,18 @@ module HstoreTranslate
9
9
  attrs.each do |attr_name|
10
10
  serialize "#{attr_name}_translations", ActiveRecord::Coders::Hstore unless HstoreTranslate::native_hstore?
11
11
 
12
- class_eval <<-RUBY
13
- def #{attr_name}
14
- read_hstore_translation('#{attr_name}')
15
- end
16
-
17
- def #{attr_name}=(value)
18
- write_hstore_translation('#{attr_name}', value)
19
- end
20
- RUBY
12
+ define_method attr_name do
13
+ read_hstore_translation(attr_name)
14
+ end
15
+
16
+ define_method "#{attr_name}=" do |value|
17
+ write_hstore_translation(attr_name, value)
18
+ end
19
+
20
+ define_singleton_method "with_#{attr_name}_translation" do |value, locale = I18n.locale|
21
+ quoted_translation_store = connection.quote_column_name("#{attr_name}_translations")
22
+ where("#{quoted_translation_store} @> hstore(:locale, :value)", locale: locale, value: value)
23
+ end
21
24
  end
22
25
 
23
26
  alias_method_chain :respond_to?, :translates
@@ -1,3 +1,3 @@
1
1
  module HstoreTranslate
2
- VERSION = "0.0.11"
2
+ VERSION = "0.0.12"
3
3
  end
@@ -112,4 +112,11 @@ class TranslatesTest < HstoreTranslate::Test
112
112
  p.reload
113
113
  assert_equal({"en" => "English Title", "fr" => "Titre français"}, p.title_translations)
114
114
  end
115
+
116
+ def test_with_translation_relation
117
+ p = Post.create!(:title_translations => { "en" => "Alice in Wonderland", "fr" => "Alice au pays des merveilles" })
118
+ I18n.with_locale(:en) do
119
+ assert_equal p.title_en, Post.with_title_translation("Alice in Wonderland").first.try(:title)
120
+ end
121
+ end
115
122
  end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: hstore_translate
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.0.11
4
+ version: 0.0.12
5
5
  platform: ruby
6
6
  authors:
7
7
  - Rob Worley
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2013-10-04 00:00:00.000000000 Z
11
+ date: 2013-10-14 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: pg