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 +8 -8
- data/README.md +7 -0
- data/lib/hstore_translate/translates.rb +12 -9
- data/lib/hstore_translate/version.rb +1 -1
- data/test/translates_test.rb +7 -0
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
NmE2YTg4ZTcyNWJhMGI4OTljODc4NDY0Yzg4MzYwZTJiMDI4NjcyYQ==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
ZGQyYjIyN2M3YmQ4YjFkN2EwN2MzODhjMTU1ODBlMDA4YmNkNTg1OA==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OWMwMjQ1MmQzYzQzNTBhMGUwYzZkOGM1M2IxZmM1OWZiZjEyOGI2ZDQwZDg3
|
10
|
+
ZTY5YzA4MGQ4MDgxYzIzZTYwNDIwMzc2NTU4Mzg1YTZhMDljYzg1YjAyNjA0
|
11
|
+
OWE3ZTJhMzkxOTkwZjQ2MzA1ODczMzkwNWRhNjg1YzZjYTEyM2U=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
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
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
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
|
data/test/translates_test.rb
CHANGED
@@ -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.
|
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-
|
11
|
+
date: 2013-10-14 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: pg
|