awesome_hstore_translate 0.2.2 → 0.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: d8f66324c452a30143941262a58e3f45ed167b62
4
- data.tar.gz: a10f57987da1fc94474bbd233c0be1e7f488991d
3
+ metadata.gz: 27419212e7681dce9c73a1b3e6b46f2b9e8a3815
4
+ data.tar.gz: 833c8bb2d84488c700b8fbcad69f9101378bee67
5
5
  SHA512:
6
- metadata.gz: 48affbf85e24d2ca9b6fb750983abbf1ecab8c5016392cd19ee2dcf95c09d77a208b72fbd7dca1e04f5ee031229fa69cc60a5e247cb656e3d46cea383cf734a8
7
- data.tar.gz: a596402fdb07093f51aa8c9a612a98046f262b52e5601c24800cb8bdef827bef6e5b2208cf6155a78258db5e44651f6be842f11fb8a2bb29b3f4dfb994ffeec9
6
+ metadata.gz: 3086c68e3504fcc602f60f980b1daab90685877bd295e616b3dc4b2d20380b241923e1a6e7519194f65737e777ee3dfce25c7973eeeb188fc76c1ddb787f11b2
7
+ data.tar.gz: 9e4c7bfece6fa94d6261d98f294bf015140f34197818aa2bb321f791fb484a450c00e92b5cc56cd9f6836dca852f232703c95c9b147064b0c9b429f8816d6a22
data/.gitignore CHANGED
@@ -17,6 +17,7 @@
17
17
  .idea/dictionaries
18
18
  .idea/vcs.xml
19
19
  .idea/jsLibraryMappings.xml
20
+ .idea/dataSources
20
21
 
21
22
  # Sensitive or high-churn files:
22
23
  .idea/dataSources.ids
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.0` Support record selection via ActiveRecord (e. g. `where`, `find_by`, ..)
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
- query.where!(normal_attrs, *rest) unless normal_attrs.empty?
7
+ translated_attrs = translated_attributes(opts)
8
+ untranslated_attrs = untranslated_attributes(opts)
10
9
 
11
- translated_attrs.each do |attribute|
12
- if opts[attribute].is_a?(String)
13
- query.where!(":key = any(avals(#{attribute}))", key: opts[attribute])
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 & opts.keys
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
@@ -1,3 +1,3 @@
1
1
  module AwesomeHstoreTranslate
2
- VERSION = '0.2.2'
2
+ VERSION = '0.3.0'
3
3
  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.2
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: 2016-10-20 00:00:00.000000000 Z
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