awesome_hstore_translate 0.2.2 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/.gitignore +1 -0
- data/README.md +13 -2
- data/awesome_hstore_translate.gemspec +1 -1
- data/lib/awesome_hstore_translate/active_record/query_methods.rb +36 -7
- data/lib/awesome_hstore_translate/version.rb +1 -1
- metadata +3 -3
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 27419212e7681dce9c73a1b3e6b46f2b9e8a3815
|
4
|
+
data.tar.gz: 833c8bb2d84488c700b8fbcad69f9101378bee67
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 3086c68e3504fcc602f60f980b1daab90685877bd295e616b3dc4b2d20380b241923e1a6e7519194f65737e777ee3dfce25c7973eeeb188fc76c1ddb787f11b2
|
7
|
+
data.tar.gz: 9e4c7bfece6fa94d6261d98f294bf015140f34197818aa2bb321f791fb484a450c00e92b5cc56cd9f6836dca852f232703c95c9b147064b0c9b429f8816d6a22
|
data/.gitignore
CHANGED
data/README.md
CHANGED
@@ -15,7 +15,8 @@ This gem uses PostgreSQLs hstore datatype and ActiveRecord models to translate m
|
|
15
15
|
- [x] `v0.1.0` Language specific accessors
|
16
16
|
- [x] `v0.2.0` Awesome Hstore Translate as drop in replace for [`hstore_translate`](https://github.com/Leadformance/hstore_translate)
|
17
17
|
- `with_[attr]_translation(str)` is not supported
|
18
|
-
- [x] `v0.2.
|
18
|
+
- [x] `v0.2.2` Support record selection via ActiveRecord (e. g. `where`, `find_by`, ..)
|
19
|
+
- [x] `v0.3.0` Support record ordering via ActiveRecord `order`
|
19
20
|
- [ ] `backlog` Support `friendly_id` (see `friendly_id-awesome_hstore` gem)
|
20
21
|
|
21
22
|
## Requirements
|
@@ -143,7 +144,17 @@ p.title_de # => Deutscher Titel
|
|
143
144
|
Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
|
144
145
|
Page.create!(:title_en => 'Another English title', :title_de => 'Noch ein Deutscher Titel')
|
145
146
|
|
146
|
-
Page.where(title: 'Another English title') # => Page
|
147
|
+
Page.where(title: 'Another English title') # => Page with title 'Another English title'
|
148
|
+
```
|
149
|
+
|
150
|
+
### Order
|
151
|
+
`awesome_hstore_translate` patches ActiveRecord, so you can conviniently use `order` as you like.
|
152
|
+
|
153
|
+
```ruby
|
154
|
+
Page.create!(:title_en => 'English title', :title_de => 'Deutscher Titel')
|
155
|
+
Page.create!(:title_en => 'Another English title', :title_de => 'Noch ein Deutscher Titel')
|
156
|
+
|
157
|
+
Page.all.order(title: :desc) # => Page with title 'English title'
|
147
158
|
```
|
148
159
|
|
149
160
|
### Limitations
|
@@ -7,7 +7,7 @@ Gem::Specification.new do |spec|
|
|
7
7
|
spec.name = 'awesome_hstore_translate'
|
8
8
|
spec.version = AwesomeHstoreTranslate::VERSION
|
9
9
|
spec.authors = ['Robin Bühler', 'Rob Worley']
|
10
|
-
spec.email = ['public@openscript.ch']
|
10
|
+
spec.email = ['public+rubygems@openscript.ch']
|
11
11
|
|
12
12
|
spec.summary = 'Using PostgreSQLs hstore datatype to provide ActiveRecord models data translation.'
|
13
13
|
spec.description = 'This gem uses PostgreSQLs hstore datatype and ActiveRecord models to translate model data. It is based on the gem hstore_translate by Rob Worely.'
|
@@ -3,18 +3,43 @@ module AwesomeHstoreTranslate
|
|
3
3
|
module QueryMethods
|
4
4
|
def where(opts = :chain, *rest)
|
5
5
|
if opts.is_a?(Hash)
|
6
|
-
translated_attrs = translated_attributes(opts)
|
7
|
-
normal_attrs = opts.reject{ |key, _| translated_attrs.include? key}
|
8
6
|
query = spawn
|
9
|
-
|
7
|
+
translated_attrs = translated_attributes(opts)
|
8
|
+
untranslated_attrs = untranslated_attributes(opts)
|
10
9
|
|
11
|
-
|
12
|
-
|
13
|
-
|
10
|
+
unless untranslated_attrs.empty?
|
11
|
+
query.where!(untranslated_attrs, *rest)
|
12
|
+
end
|
13
|
+
|
14
|
+
translated_attrs.each do |key, value|
|
15
|
+
if value.is_a?(String)
|
16
|
+
query.where!(":value = any(avals(#{key}))", value: value)
|
14
17
|
else
|
15
18
|
super
|
16
19
|
end
|
17
20
|
end
|
21
|
+
|
22
|
+
query
|
23
|
+
else
|
24
|
+
super
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
def order(*args)
|
29
|
+
if args.is_a?(Array)
|
30
|
+
check_if_method_has_arguments!(:order, args)
|
31
|
+
query = spawn
|
32
|
+
translated_attrs = translated_attributes(args[0])
|
33
|
+
untranslated_attrs = untranslated_attributes(args[0])
|
34
|
+
|
35
|
+
unless untranslated_attrs.empty?
|
36
|
+
query.order!(untranslated_attrs)
|
37
|
+
end
|
38
|
+
|
39
|
+
translated_attrs.each do |key, value|
|
40
|
+
query.order!("#{key} -> '#{I18n.locale.to_s}' #{value}")
|
41
|
+
end
|
42
|
+
|
18
43
|
query
|
19
44
|
else
|
20
45
|
super
|
@@ -24,7 +49,11 @@ module AwesomeHstoreTranslate
|
|
24
49
|
private
|
25
50
|
|
26
51
|
def translated_attributes(opts)
|
27
|
-
self.translated_attribute_names
|
52
|
+
opts.select{ |key, _| self.translated_attribute_names.include?(key) }
|
53
|
+
end
|
54
|
+
|
55
|
+
def untranslated_attributes(opts)
|
56
|
+
opts.reject{ |key, _| self.translated_attribute_names.include?(key) }
|
28
57
|
end
|
29
58
|
end
|
30
59
|
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.
|
4
|
+
version: 0.3.0
|
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:
|
12
|
+
date: 2017-02-03 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: activerecord
|
@@ -138,7 +138,7 @@ dependencies:
|
|
138
138
|
description: This gem uses PostgreSQLs hstore datatype and ActiveRecord models to
|
139
139
|
translate model data. It is based on the gem hstore_translate by Rob Worely.
|
140
140
|
email:
|
141
|
-
- public@openscript.ch
|
141
|
+
- public+rubygems@openscript.ch
|
142
142
|
executables:
|
143
143
|
- console
|
144
144
|
- setup
|