mobility 0.8.1 → 0.8.2

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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 379c09aba475fbc7b7491e64cbbbd7eb6fa3ac46b546c318ca645345f2a548cf
4
- data.tar.gz: 5359a8a04ea4db2e30d43e14f79ba2c962555362e4a8e5f1d5412d481b101c9f
3
+ metadata.gz: 68b8f21d39ab133839062c63ed91f6cf225464b1780525ff3e96b33937c98c0c
4
+ data.tar.gz: 66f0db0183fc1568c290fdddb52edce9dfbb85536dcf1ebd8921e6fa3b1bbd36
5
5
  SHA512:
6
- metadata.gz: fe6c5ade9de6a5f35ef84c5daa8c822e397e608f60c9f3d34451b1091a89f2eed5c9c58801afd1603c0ad1ceb2c58d26865a3b0297b4f2fa5f8ac3886712788c
7
- data.tar.gz: 8b6fbbf4c0833d6921c002136ec10ce6baec7dd0b20886b9c4dece81099cd4444e1bbd6dd5067d0392ec8a7b538ae1a170340107d6aa60325437e9859cb507ee
6
+ metadata.gz: 21902f555813178915a0f265e3ecde8a777907e654d40d733761c95898e0cf70764457441233d0591b9591bb22ff74741c48d47aac3ebc914a4400c03d232136
7
+ data.tar.gz: 5a8c377f1161de9bf533315094c31e5c40fa722ff06d14006086eb389acacad228303a98c3872b39adc09e2de684e0959df90a4c63bc4a52dd7d2ab22ecf56af
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -2,6 +2,11 @@
2
2
 
3
3
  ## 0.8
4
4
 
5
+ ### 0.8.2 (September 28, 2018)
6
+ * Support pluck, select and group methods on translated attributes
7
+ ([#284](https://github.com/shioyama/mobility/pull/284),
8
+ [#285](https://github.com/shioyama/mobility/pull/285))
9
+
5
10
  ### 0.8.1 (September 25, 2018)
6
11
  * Default ActiveRecord Table/KeyValue backends to OUTER JOIN
7
12
  ([#277](https://github.com/shioyama/mobility/pull/277))
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mobility (0.8.0)
4
+ mobility (0.8.1)
5
5
  i18n (>= 0.6.10, < 2)
6
6
  request_store (~> 1.0)
7
7
 
@@ -42,7 +42,7 @@ GEM
42
42
  database_cleaner (1.7.0)
43
43
  diff-lcs (1.3)
44
44
  erubi (1.7.1)
45
- ffi (1.9.24)
45
+ ffi (1.9.25)
46
46
  formatador (0.2.5)
47
47
  generator_spec (0.9.4)
48
48
  activesupport (>= 3.0.0)
@@ -108,19 +108,19 @@ GEM
108
108
  ffi (>= 0.5.0, < 2)
109
109
  request_store (1.4.1)
110
110
  rack (>= 1.4)
111
- rspec (3.7.0)
112
- rspec-core (~> 3.7.0)
113
- rspec-expectations (~> 3.7.0)
114
- rspec-mocks (~> 3.7.0)
115
- rspec-core (3.7.1)
116
- rspec-support (~> 3.7.0)
117
- rspec-expectations (3.7.0)
111
+ rspec (3.8.0)
112
+ rspec-core (~> 3.8.0)
113
+ rspec-expectations (~> 3.8.0)
114
+ rspec-mocks (~> 3.8.0)
115
+ rspec-core (3.8.0)
116
+ rspec-support (~> 3.8.0)
117
+ rspec-expectations (3.8.1)
118
118
  diff-lcs (>= 1.2.0, < 2.0)
119
- rspec-support (~> 3.7.0)
120
- rspec-mocks (3.7.0)
119
+ rspec-support (~> 3.8.0)
120
+ rspec-mocks (3.8.0)
121
121
  diff-lcs (>= 1.2.0, < 2.0)
122
- rspec-support (~> 3.7.0)
123
- rspec-support (3.7.1)
122
+ rspec-support (~> 3.8.0)
123
+ rspec-support (3.8.0)
124
124
  ruby_dep (1.5.0)
125
125
  shellany (0.0.1)
126
126
  sqlite3 (1.3.13)
@@ -128,7 +128,7 @@ GEM
128
128
  thread_safe (0.3.6)
129
129
  tzinfo (1.2.5)
130
130
  thread_safe (~> 0.1)
131
- yard (0.9.14)
131
+ yard (0.9.16)
132
132
 
133
133
  PLATFORMS
134
134
  ruby
data/README.md CHANGED
@@ -51,7 +51,7 @@ Installation
51
51
  Add this line to your application's Gemfile:
52
52
 
53
53
  ```ruby
54
- gem 'mobility', '~> 0.8.1'
54
+ gem 'mobility', '~> 0.8.2'
55
55
  ```
56
56
 
57
57
  Mobility is cryptographically signed. To be sure the gem you install hasn't
@@ -716,6 +716,15 @@ locale(s) and value(s) passed in to the query. Details of how this is done can
716
716
  be found in the [Wiki page for the KeyValue
717
717
  backend](https://github.com/shioyama/mobility/wiki/KeyValue-Backend#querying).
718
718
 
719
+ You can also use methods like `order`, `select`, `pluck` and `group` on
720
+ translated attributes just as you would with normal attributes, and Mobility
721
+ will handle generating the appropriate SQL:
722
+
723
+ ```ruby
724
+ Post.i18n.pluck(:title)
725
+ #=> ["foo", "bar", ...]
726
+ ```
727
+
719
728
  If you would prefer to avoid the `i18n` scope everywhere, you can define it as
720
729
  a default scope on your model:
721
730
 
@@ -4,7 +4,36 @@ require "mobility/arel/visitor"
4
4
 
5
5
  module Mobility
6
6
  module Arel
7
+ module MobilityExpressions
8
+ include ::Arel::Expressions
9
+
10
+ # @note This is necessary in order to ensure that when a translated
11
+ # attribute is selected with an alias using +AS+, the resulting
12
+ # expression can still be counted without blowing up.
13
+ #
14
+ # Extending +::Arel::Expressions+ is necessary to convince ActiveRecord
15
+ # that this node should not be stringified, which otherwise would
16
+ # result in garbage SQL.
17
+ #
18
+ # @see https://github.com/rails/rails/blob/847342c25c61acaea988430dc3ab66a82e3ed486/activerecord/lib/active_record/relation/calculations.rb#L261
19
+ def as(*)
20
+ super
21
+ .extend(::Arel::Expressions)
22
+ .extend(Countable)
23
+ end
24
+
25
+ module Countable
26
+ # @note This allows expressions with selected translated attributes to
27
+ # be counted.
28
+ def count(*args)
29
+ left.count(*args)
30
+ end
31
+ end
32
+ end
33
+
7
34
  class Attribute < ::Arel::Attributes::Attribute
35
+ include MobilityExpressions
36
+
8
37
  attr_reader :backend_class
9
38
  attr_reader :locale
10
39
  attr_reader :attribute_name
@@ -14,10 +14,10 @@ module Mobility
14
14
  HstoreQuestion
15
15
  ].each do |name|
16
16
  const_set name, (Class.new(Binary) do
17
- include ::Arel::Expressions
18
17
  include ::Arel::Predications
19
18
  include ::Arel::OrderPredications
20
19
  include ::Arel::AliasPredication
20
+ include ::Mobility::Arel::MobilityExpressions
21
21
 
22
22
  def lower
23
23
  super self
@@ -57,6 +57,7 @@ can be run again to add new attributes or locales.)
57
57
  # on model table
58
58
  def self.build_node(attr, locale)
59
59
  model_class.arel_table[Column.column_name_for(attr, locale)]
60
+ .extend(::Mobility::Arel::MobilityExpressions)
60
61
  end
61
62
 
62
63
  private
@@ -22,7 +22,35 @@ enabled for any one attribute on the model.
22
22
  extend FindByMethods.new(*attributes.names)
23
23
  singleton_class.send :alias_method, Mobility.query_method, :__mobility_query_scope__
24
24
  end
25
+ attributes.backend_class.include self
25
26
  end
27
+
28
+ def attribute_alias(attribute, locale = Mobility.locale)
29
+ "__mobility_%s_%s__" % [attribute, ::Mobility.normalize_locale(locale)]
30
+ end
31
+ end
32
+
33
+ # @note We use +instance_variable_get+ here to get the +AttributeSet+
34
+ # rather than the hash of attributes. Getting the full hash of
35
+ # attributes is a performance hit and better to avoid if unnecessary.
36
+ # TODO: Improve this.
37
+ def read(locale, **)
38
+ if (model_attributes_defined? &&
39
+ model_attributes.key?(alias_ = Query.attribute_alias(attribute, locale)))
40
+ model_attributes[alias_].value
41
+ else
42
+ super
43
+ end
44
+ end
45
+
46
+ private
47
+
48
+ def model_attributes_defined?
49
+ model.instance_variable_defined?(:@attributes)
50
+ end
51
+
52
+ def model_attributes
53
+ model.instance_variable_get(:@attributes)
26
54
  end
27
55
 
28
56
  module QueryMethod
@@ -109,6 +137,28 @@ enabled for any one attribute on the model.
109
137
  end
110
138
  end
111
139
 
140
+ if ::ActiveRecord::VERSION::STRING >= '5.0'
141
+ %w[pluck group select].each do |method_name|
142
+ define_method method_name do |*attrs|
143
+ return super(*attrs) unless attrs.any?(&@klass.method(:mobility_attribute?))
144
+
145
+ keys = attrs.dup
146
+
147
+ base = keys.each_with_index.inject(self) do |query, (key, index)|
148
+ next query unless @klass.mobility_attribute?(key)
149
+ keys[index] = backend_node(key)
150
+ if method_name == "select"
151
+ keys[index] = keys[index]
152
+ .as(::Mobility::Plugins::ActiveRecord::Query.attribute_alias(key.to_s))
153
+ end
154
+ @klass.mobility_backend_class(key).apply_scope(query, backend_node(key))
155
+ end
156
+
157
+ base.public_send(method_name, *keys)
158
+ end
159
+ end
160
+ end
161
+
112
162
  # Return backend node for attribute name.
113
163
  # @param [Symbol,String] name Name of attribute
114
164
  # @param [Symbol] locale Locale
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module Mobility
4
- VERSION = "0.8.1"
4
+ VERSION = "0.8.2"
5
5
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: mobility
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.8.1
4
+ version: 0.8.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Chris Salzberg
@@ -35,7 +35,7 @@ cert_chain:
35
35
  m46ezJjnr4uXgSNuTs+RdM6DtrKaFG2HuziSP+dzmWqZiUgj1eTACYsPwfF/Z1Z6
36
36
  2DiDkec/SK4qJBMlNYPo/PTZuh5m1jROtf/hyS0VXJmm+uWc
37
37
  -----END CERTIFICATE-----
38
- date: 2018-09-25 00:00:00.000000000 Z
38
+ date: 2018-09-28 00:00:00.000000000 Z
39
39
  dependencies:
40
40
  - !ruby/object:Gem::Dependency
41
41
  name: request_store
metadata.gz.sig CHANGED
Binary file