awesome_hstore_translate 0.2.1 → 0.2.2
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +18 -0
- data/lib/awesome_hstore_translate/active_record/core.rb +1 -1
- data/lib/awesome_hstore_translate/active_record/instance_methods.rb +5 -6
- data/lib/awesome_hstore_translate/active_record/query_methods.rb +2 -2
- data/lib/awesome_hstore_translate/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d8f66324c452a30143941262a58e3f45ed167b62
|
4
|
+
data.tar.gz: a10f57987da1fc94474bbd233c0be1e7f488991d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 48affbf85e24d2ca9b6fb750983abbf1ecab8c5016392cd19ee2dcf95c09d77a208b72fbd7dca1e04f5ee031229fa69cc60a5e247cb656e3d46cea383cf734a8
|
7
|
+
data.tar.gz: a596402fdb07093f51aa8c9a612a98046f262b52e5601c24800cb8bdef827bef6e5b2208cf6155a78258db5e44651f6be842f11fb8a2bb29b3f4dfb994ffeec9
|
data/README.md
CHANGED
@@ -49,6 +49,9 @@ Make sure that the datatype of this columns is `hstore`:
|
|
49
49
|
```ruby
|
50
50
|
class CreatePages < ActiveRecord::Migration
|
51
51
|
def change
|
52
|
+
# Make sure you enable the hstore extenion
|
53
|
+
enable_extension 'hstore' unless extension_enabled?('hstore')
|
54
|
+
|
52
55
|
create_table :pages do |t|
|
53
56
|
t.column :title, :hstore
|
54
57
|
t.column :content, :hstore
|
@@ -143,6 +146,21 @@ Page.create!(:title_en => 'Another English title', :title_de => 'Noch ein Deutsc
|
|
143
146
|
Page.where(title: 'Another English title') # => Page
|
144
147
|
```
|
145
148
|
|
149
|
+
### Limitations
|
150
|
+
`awesome_hstore_translate` patches ActiveRecord, which create the limitation, that a with `where` chained `first_or_create` and `first_or_create!` **doesn't work** as expected.
|
151
|
+
Here is an example, which **won't** work:
|
152
|
+
|
153
|
+
``` ruby
|
154
|
+
Page.where(title: 'Titre français').first_or_create!
|
155
|
+
```
|
156
|
+
|
157
|
+
A workaround is:
|
158
|
+
|
159
|
+
``` ruby
|
160
|
+
Page.where(title: 'Titre français').first_or_create!(title: 'Titre français')
|
161
|
+
```
|
162
|
+
|
163
|
+
The where clause is internally rewritten to `WHERE 'Titre français' = any(avals(title))`, so the `title: 'Titre français'` is not bound to the scope.
|
146
164
|
|
147
165
|
### Upgrade from [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
|
148
166
|
1. Replace the [`hstore_translate`](https://github.com/Leadformance/hstore_translate) with `awesome_hstore_translate` in your Gemfile
|
@@ -4,16 +4,15 @@ module AwesomeHstoreTranslate
|
|
4
4
|
protected
|
5
5
|
|
6
6
|
def read_translated_attribute(attr, locale = I18n.locale)
|
7
|
-
locales =
|
8
|
-
locales << locale
|
7
|
+
locales = Array(locale)
|
9
8
|
locales += get_fallback_for_locale(locale) || [] if translation_options[:fallbacks]
|
10
9
|
|
11
10
|
translations = read_raw_attribute(attr)
|
12
11
|
|
13
12
|
if translations
|
14
|
-
locales.uniq.each do |cur|
|
15
|
-
if translations.has_key?(cur
|
16
|
-
return translations[cur
|
13
|
+
locales.map(&:to_s).uniq.each do |cur|
|
14
|
+
if translations.has_key?(cur) && !translations[cur].blank?
|
15
|
+
return translations[cur]
|
17
16
|
end
|
18
17
|
end
|
19
18
|
end
|
@@ -50,4 +49,4 @@ module AwesomeHstoreTranslate
|
|
50
49
|
end
|
51
50
|
end
|
52
51
|
end
|
53
|
-
end
|
52
|
+
end
|
@@ -6,11 +6,11 @@ module AwesomeHstoreTranslate
|
|
6
6
|
translated_attrs = translated_attributes(opts)
|
7
7
|
normal_attrs = opts.reject{ |key, _| translated_attrs.include? key}
|
8
8
|
query = spawn
|
9
|
-
query.where!(normal_attrs) unless normal_attrs.empty?
|
9
|
+
query.where!(normal_attrs, *rest) unless normal_attrs.empty?
|
10
10
|
|
11
11
|
translated_attrs.each do |attribute|
|
12
12
|
if opts[attribute].is_a?(String)
|
13
|
-
query.where!("
|
13
|
+
query.where!(":key = any(avals(#{attribute}))", key: opts[attribute])
|
14
14
|
else
|
15
15
|
super
|
16
16
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: awesome_hstore_translate
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.2
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Robin Bühler
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2016-
|
12
|
+
date: 2016-10-20 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|