mobility 0.4.3 → 0.5.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.
Files changed (83) hide show
  1. checksums.yaml +4 -4
  2. checksums.yaml.gz.sig +0 -0
  3. data.tar.gz.sig +0 -0
  4. data/CHANGELOG.md +11 -0
  5. data/Gemfile.lock +27 -27
  6. data/README.md +8 -8
  7. data/lib/mobility.rb +18 -5
  8. data/lib/mobility/{wrapper.rb → accumulator.rb} +2 -4
  9. data/lib/mobility/active_record.rb +1 -0
  10. data/lib/mobility/active_record/backend_resetter.rb +1 -0
  11. data/lib/mobility/active_record/string_translation.rb +1 -0
  12. data/lib/mobility/active_record/text_translation.rb +1 -0
  13. data/lib/mobility/adapter.rb +19 -0
  14. data/lib/mobility/attributes.rb +2 -1
  15. data/lib/mobility/backend.rb +1 -1
  16. data/lib/mobility/backend/orm_delegator.rb +5 -4
  17. data/lib/mobility/backend_resetter.rb +2 -0
  18. data/lib/mobility/backends/active_record/column.rb +1 -0
  19. data/lib/mobility/backends/active_record/column/query_methods.rb +1 -0
  20. data/lib/mobility/backends/active_record/container.rb +21 -4
  21. data/lib/mobility/backends/active_record/container/json_query_methods.rb +30 -0
  22. data/lib/mobility/backends/active_record/container/{query_methods.rb → jsonb_query_methods.rb} +4 -2
  23. data/lib/mobility/backends/active_record/json.rb +36 -0
  24. data/lib/mobility/backends/active_record/json/query_methods.rb +25 -0
  25. data/lib/mobility/backends/active_record/jsonb.rb +4 -4
  26. data/lib/mobility/backends/active_record/jsonb/query_methods.rb +1 -0
  27. data/lib/mobility/backends/active_record/key_value.rb +2 -2
  28. data/lib/mobility/backends/active_record/key_value/query_methods.rb +1 -0
  29. data/lib/mobility/backends/active_record/pg_hash.rb +1 -0
  30. data/lib/mobility/backends/active_record/pg_query_methods.rb +1 -1
  31. data/lib/mobility/backends/active_record/serialized.rb +1 -0
  32. data/lib/mobility/backends/active_record/serialized/query_methods.rb +1 -0
  33. data/lib/mobility/backends/active_record/table.rb +2 -2
  34. data/lib/mobility/backends/active_record/table/query_methods.rb +1 -0
  35. data/lib/mobility/backends/column.rb +2 -0
  36. data/lib/mobility/backends/json.rb +20 -0
  37. data/lib/mobility/backends/jsonb.rb +0 -1
  38. data/lib/mobility/backends/key_value.rb +1 -0
  39. data/lib/mobility/backends/sequel/column.rb +1 -0
  40. data/lib/mobility/backends/sequel/column/query_methods.rb +1 -0
  41. data/lib/mobility/backends/sequel/container.rb +19 -3
  42. data/lib/mobility/backends/sequel/container/json_query_methods.rb +34 -0
  43. data/lib/mobility/backends/sequel/container/{query_methods.rb → jsonb_query_methods.rb} +2 -1
  44. data/lib/mobility/backends/sequel/hstore/query_methods.rb +1 -0
  45. data/lib/mobility/backends/sequel/json.rb +36 -0
  46. data/lib/mobility/backends/sequel/json/query_methods.rb +27 -0
  47. data/lib/mobility/backends/sequel/jsonb/query_methods.rb +2 -1
  48. data/lib/mobility/backends/sequel/key_value.rb +3 -3
  49. data/lib/mobility/backends/sequel/key_value/query_methods.rb +1 -0
  50. data/lib/mobility/backends/sequel/pg_hash.rb +1 -0
  51. data/lib/mobility/backends/sequel/query_methods.rb +1 -0
  52. data/lib/mobility/backends/sequel/serialized.rb +1 -0
  53. data/lib/mobility/backends/sequel/serialized/query_methods.rb +1 -0
  54. data/lib/mobility/backends/sequel/table.rb +1 -0
  55. data/lib/mobility/backends/sequel/table/query_methods.rb +1 -0
  56. data/lib/mobility/backends/serialized.rb +1 -0
  57. data/lib/mobility/backends/table.rb +1 -0
  58. data/lib/mobility/configuration.rb +3 -5
  59. data/lib/mobility/fallbacks.rb +28 -0
  60. data/lib/mobility/plugins/active_model/dirty.rb +5 -5
  61. data/lib/mobility/plugins/active_record/dirty.rb +1 -1
  62. data/lib/mobility/plugins/cache.rb +1 -0
  63. data/lib/mobility/plugins/default.rb +2 -0
  64. data/lib/mobility/plugins/dirty.rb +1 -0
  65. data/lib/mobility/plugins/fallbacks.rb +3 -1
  66. data/lib/mobility/plugins/fallthrough_accessors.rb +4 -4
  67. data/lib/mobility/plugins/locale_accessors.rb +2 -2
  68. data/lib/mobility/plugins/presence.rb +1 -0
  69. data/lib/mobility/sequel/column_changes.rb +3 -1
  70. data/lib/mobility/sequel/hash_initializer.rb +2 -0
  71. data/lib/mobility/sequel/string_translation.rb +1 -0
  72. data/lib/mobility/sequel/text_translation.rb +1 -0
  73. data/lib/mobility/translates.rb +2 -0
  74. data/lib/mobility/util.rb +3 -1
  75. data/lib/mobility/version.rb +3 -1
  76. data/lib/rails/generators/mobility/active_record_migration_compatibility.rb +1 -0
  77. data/lib/rails/generators/mobility/backend_generators/base.rb +3 -3
  78. data/lib/rails/generators/mobility/generators.rb +1 -0
  79. data/lib/rails/generators/mobility/install_generator.rb +1 -0
  80. data/lib/rails/generators/mobility/templates/initializer.rb +75 -0
  81. data/lib/rails/generators/mobility/translations_generator.rb +3 -3
  82. metadata +14 -5
  83. metadata.gz.sig +0 -0
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 43cf8100f2f537180ffae1d90e7ac2bd78ba045e
4
- data.tar.gz: 5e62d1a469b7c438779a212844737b97c6251384
3
+ metadata.gz: 517f855df0698bb34baeac4033972a5950df7333
4
+ data.tar.gz: 569bf64eb02f45abd227d75bf28e53fd8a2aa4a1
5
5
  SHA512:
6
- metadata.gz: c93092fb57649f458305fa368e72156137751e6d740ead6206b91325d5daf6b1287102096eb69c10ef238f503ea33b80f1383c1f98d94ecddda2ba728ee68be7
7
- data.tar.gz: e018ec6117e9c5029a7a1249bc01e8f276eb5102583166074205ba3e23f133b039b83da33bb51e2488f75328b86e73faa5ea860016748e1081779c4e7cc28eb4
6
+ metadata.gz: d6431a9d3884fffec244488d8f56e906f5a514eb9f74e14b9d6fff192fe43046ad3bfe6a00720bf27e383e613c38b8269fac2a58aa6996b25b483aac6450b6bd
7
+ data.tar.gz: d78ecb91bd70a1f3266a550f5fb69297e0259dd08ba7cae7091bec8146fb85d72230d9d0ebeea803118a11f96fc5ac41cf7b2041f9efdd60daed81d1dba10342
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -2,6 +2,17 @@
2
2
 
3
3
  ## 0.4
4
4
 
5
+ ### 0.5.0 (March 16, 2018)
6
+ * Support PostgreSQL json column format as Json backend and dynamically in
7
+ Container backend ([#182](https://github.com/shioyama/mobility/pull/182) and
8
+ [#184](https://github.com/shioyama/mobility/pull/184), respectively)
9
+ * Fall through to `I18n.fallbacks` when defined
10
+ ([#180](https://github.com/shioyama/mobility/pull/180))
11
+ * Improve comments in Rails initializer
12
+ ([#186](https://github.com/shioyama/mobility/pull/186))
13
+ * Use pragma comments to freeze strings everywhere
14
+ ([#177](https://github.com/shioyama/mobility/pull/177))
15
+
5
16
  ### 0.4.3 (February 18, 2018)
6
17
  * Add missing require in container backend
7
18
  ([#174](https://github.com/shioyama/mobility/pull/174))
@@ -1,33 +1,33 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mobility (0.4.2)
5
- i18n (>= 0.6.10, < 0.10)
4
+ mobility (0.5.0)
5
+ i18n (>= 0.6.10, < 1.1)
6
6
  request_store (~> 1.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
- actionpack (5.2.0.rc1)
12
- actionview (= 5.2.0.rc1)
13
- activesupport (= 5.2.0.rc1)
11
+ actionpack (5.2.0.beta2)
12
+ actionview (= 5.2.0.beta2)
13
+ activesupport (= 5.2.0.beta2)
14
14
  rack (~> 2.0)
15
15
  rack-test (>= 0.6.3)
16
16
  rails-dom-testing (~> 2.0)
17
17
  rails-html-sanitizer (~> 1.0, >= 1.0.2)
18
- actionview (5.2.0.rc1)
19
- activesupport (= 5.2.0.rc1)
18
+ actionview (5.2.0.beta2)
19
+ activesupport (= 5.2.0.beta2)
20
20
  builder (~> 3.1)
21
21
  erubi (~> 1.4)
22
22
  rails-dom-testing (~> 2.0)
23
23
  rails-html-sanitizer (~> 1.0, >= 1.0.3)
24
- activemodel (5.2.0.rc1)
25
- activesupport (= 5.2.0.rc1)
26
- activerecord (5.2.0.rc1)
27
- activemodel (= 5.2.0.rc1)
28
- activesupport (= 5.2.0.rc1)
24
+ activemodel (5.2.0.beta2)
25
+ activesupport (= 5.2.0.beta2)
26
+ activerecord (5.2.0.beta2)
27
+ activemodel (= 5.2.0.beta2)
28
+ activesupport (= 5.2.0.beta2)
29
29
  arel (>= 9.0)
30
- activesupport (5.2.0.rc1)
30
+ activesupport (5.2.0.beta2)
31
31
  concurrent-ruby (~> 1.0, >= 1.0.2)
32
32
  i18n (~> 0.7)
33
33
  minitest (~> 5.1)
@@ -35,14 +35,14 @@ GEM
35
35
  arel (9.0.0)
36
36
  benchmark-ips (2.7.2)
37
37
  builder (3.2.3)
38
- byebug (9.1.0)
38
+ byebug (10.0.0)
39
39
  coderay (1.1.2)
40
40
  concurrent-ruby (1.0.5)
41
41
  crass (1.0.3)
42
42
  database_cleaner (1.6.2)
43
43
  diff-lcs (1.3)
44
- erubi (1.7.0)
45
- ffi (1.9.21)
44
+ erubi (1.7.1)
45
+ ffi (1.9.23)
46
46
  formatador (0.2.5)
47
47
  generator_spec (0.9.4)
48
48
  activesupport (>= 3.0.0)
@@ -61,13 +61,13 @@ GEM
61
61
  guard (~> 2.1)
62
62
  guard-compat (~> 1.1)
63
63
  rspec (>= 2.99.0, < 4.0)
64
- i18n (0.9.3)
64
+ i18n (0.9.5)
65
65
  concurrent-ruby (~> 1.0)
66
66
  listen (3.1.5)
67
67
  rb-fsevent (~> 0.9, >= 0.9.4)
68
68
  rb-inotify (~> 0.9, >= 0.9.7)
69
69
  ruby_dep (~> 1.2)
70
- loofah (2.1.1)
70
+ loofah (2.2.0)
71
71
  crass (~> 1.0.2)
72
72
  nokogiri (>= 1.5.9)
73
73
  lumberjack (1.0.12)
@@ -85,25 +85,25 @@ GEM
85
85
  pry (0.11.3)
86
86
  coderay (~> 1.1.0)
87
87
  method_source (~> 0.9.0)
88
- pry-byebug (3.5.1)
89
- byebug (~> 9.1)
88
+ pry-byebug (3.6.0)
89
+ byebug (~> 10.0)
90
90
  pry (~> 0.10)
91
91
  rack (2.0.4)
92
- rack-test (0.8.2)
92
+ rack-test (0.8.3)
93
93
  rack (>= 1.0, < 3)
94
94
  rails-dom-testing (2.0.3)
95
95
  activesupport (>= 4.2.0)
96
96
  nokogiri (>= 1.6)
97
97
  rails-html-sanitizer (1.0.3)
98
98
  loofah (~> 2.0)
99
- railties (5.2.0.rc1)
100
- actionpack (= 5.2.0.rc1)
101
- activesupport (= 5.2.0.rc1)
99
+ railties (5.2.0.beta2)
100
+ actionpack (= 5.2.0.beta2)
101
+ activesupport (= 5.2.0.beta2)
102
102
  method_source
103
103
  rake (>= 0.8.7)
104
104
  thor (>= 0.18.1, < 2.0)
105
105
  rake (12.3.0)
106
- rb-fsevent (0.10.2)
106
+ rb-fsevent (0.10.3)
107
107
  rb-inotify (0.9.10)
108
108
  ffi (>= 0.5.0, < 2)
109
109
  request_store (1.4.0)
@@ -134,7 +134,7 @@ PLATFORMS
134
134
  ruby
135
135
 
136
136
  DEPENDENCIES
137
- activerecord (>= 5.2.0.beta1, < 5.3)
137
+ activerecord (= 5.2.0.beta2)
138
138
  benchmark-ips
139
139
  bundler (~> 1.12)
140
140
  database_cleaner (~> 1.5, >= 1.5.3)
@@ -144,7 +144,7 @@ DEPENDENCIES
144
144
  mysql2 (~> 0.4.9)
145
145
  pg (< 1.0)
146
146
  pry-byebug
147
- railties (>= 5.2.0.beta1, < 5.3)
147
+ railties (= 5.2.0.beta2)
148
148
  rake (~> 12, >= 12.2.1)
149
149
  rspec (~> 3.0)
150
150
  sqlite3
data/README.md CHANGED
@@ -29,7 +29,7 @@ columns](http://dejimata.com/2017/3/3/translating-with-mobility#strategy-1) and
29
29
  [model translation
30
30
  tables](http://dejimata.com/2017/3/3/translating-with-mobility#strategy-2), as
31
31
  well as database-specific storage solutions such as
32
- [jsonb](https://www.postgresql.org/docs/current/static/datatype-json.html) and
32
+ [json/jsonb](https://www.postgresql.org/docs/current/static/datatype-json.html) and
33
33
  [Hstore](https://www.postgresql.org/docs/current/static/hstore.html) (for
34
34
  PostgreSQL).
35
35
 
@@ -54,7 +54,7 @@ Installation
54
54
  Add this line to your application's Gemfile:
55
55
 
56
56
  ```ruby
57
- gem 'mobility', '~> 0.4.3'
57
+ gem 'mobility', '~> 0.5.0'
58
58
  ```
59
59
 
60
60
  Mobility is cryptographically signed. To be sure the gem you install hasn't
@@ -794,11 +794,11 @@ class](http://www.rubydoc.info/gems/mobility/Mobility/Backends/Column).
794
794
 
795
795
  ### PostgreSQL-specific Backends
796
796
 
797
- Mobility also supports jsonb and Hstore storage options, if you are using
797
+ Mobility also supports JSON and Hstore storage options, if you are using
798
798
  PostgreSQL as your database. To use this option, create column(s) on the model
799
- table for each translated attribute, and set your backend to `:jsonb` or
800
- `:hstore`. If you are using Sequel, note that you will need to enable the
801
- [pg_json](http://sequel.jeremyevans.net/rdoc-plugins/files/lib/sequel/extensions/pg_json_rb.html)
799
+ table for each translated attribute, and set your backend to `:json`, `:jsonb`
800
+ or `:hstore`. If you are using Sequel, note that you
801
+ will need to enable the [pg_json](http://sequel.jeremyevans.net/rdoc-plugins/files/lib/sequel/extensions/pg_json_rb.html)
802
802
  or
803
803
  [pg_hstore](http://sequel.jeremyevans.net/rdoc-plugins/files/lib/sequel/extensions/pg_hstore_rb.html)
804
804
  extensions with `DB.extension :pg_json` or `DB.extension :pg_hstore` (where
@@ -857,8 +857,8 @@ they conform to the same expected behaviour. These examples can be found in:
857
857
  - `spec/support/shared_examples/querying_examples.rb` (specs for
858
858
  [querying](#querying))
859
859
  - `spec/support/shared_examples/serialization_examples.rb` (specialized specs
860
- for backends which store translations as a Hash: `serialized`, `hstore` and
861
- `jsonb` backends)
860
+ for backends which store translations as a Hash: `serialized`, `hstore`,
861
+ `json` and `jsonb` backends)
862
862
 
863
863
  A minimal test can simply define a model class and use helpers defined in
864
864
  `spec/support/helpers.rb` to run these examples, by extending either
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require 'i18n'
2
3
  require 'request_store'
3
4
  require 'mobility/version'
@@ -24,15 +25,17 @@ in backends to define gem-dependent behavior.
24
25
 
25
26
  =end
26
27
  module Mobility
28
+ require "mobility/adapter"
29
+ require "mobility/accumulator"
27
30
  require "mobility/attributes"
28
31
  require "mobility/backend"
29
32
  require "mobility/backends"
30
33
  require "mobility/backend_resetter"
31
34
  require "mobility/configuration"
35
+ require "mobility/fallbacks"
32
36
  require "mobility/loaded"
33
37
  require "mobility/plugins"
34
38
  require "mobility/translates"
35
- require "mobility/wrapper"
36
39
 
37
40
  # General error for version compatibility conflicts
38
41
  class VersionNotSupportedError < ArgumentError; end
@@ -194,7 +197,7 @@ module Mobility
194
197
  # Mobility.normalize_locale("pt-BR")
195
198
  # #=> "pt_br"
196
199
  def normalize_locale(locale = Mobility.locale)
197
- "#{locale.to_s.downcase.sub("-", "_")}".freeze
200
+ "#{locale.to_s.downcase.sub("-", "_")}"
198
201
  end
199
202
  alias_method :normalized_locale, :normalize_locale
200
203
 
@@ -208,7 +211,7 @@ module Mobility
208
211
  # Mobility.normalize_locale_accessor(:bar, "pt-BR")
209
212
  # #=> "bar_pt_br"
210
213
  def normalize_locale_accessor(attribute, locale = Mobility.locale)
211
- "#{attribute}_#{normalize_locale(locale)}".freeze
214
+ "#{attribute}_#{normalize_locale(locale)}"
212
215
  end
213
216
 
214
217
  # Raises InvalidLocale exception if the locale passed in is present but not available.
@@ -243,22 +246,32 @@ EOL
243
246
 
244
247
  module InstanceMethods
245
248
  # Fetch backend for an attribute
249
+ # @deprecated Use mobility.backend_for(attribute) instead.
246
250
  # @param [String] attribute Attribute
251
+ # TODO: Remove in v1.0
247
252
  def mobility_backend_for(attribute)
248
- send(Backend.method_name(attribute))
253
+ warn %{
254
+ WARNING: mobility_backend_for is deprecated and will be removed in the next
255
+ major version of Mobility. Use <post>.mobility.backend_for(attributes)
256
+ instead.}
257
+ mobility.backend_for(attribute)
249
258
  end
250
259
 
251
260
  def initialize_dup(other)
252
261
  @mobility_backends = nil
253
262
  super
254
263
  end
264
+
265
+ def mobility
266
+ @mobility ||= Adapter.new(self)
267
+ end
255
268
  end
256
269
 
257
270
  module ClassMethods
258
271
  include Translates
259
272
 
260
273
  def mobility
261
- @mobility ||= Mobility::Wrapper.new(self)
274
+ @mobility ||= Mobility::Accumulator.new
262
275
  end
263
276
 
264
277
  def translated_attribute_names
@@ -6,14 +6,12 @@ which {Attributes} modules have been included on the model class. It is also a
6
6
  simple delegator, so any missing method will be delegated to the model class.
7
7
 
8
8
  =end
9
- class Wrapper < SimpleDelegator
9
+ class Accumulator
10
10
  # @return [Array<Attributes>]
11
11
  attr_reader :modules
12
- alias :model_class :__getobj__
13
12
 
14
13
  # @param [Class] model_class Model class
15
- def initialize(model_class)
16
- super
14
+ def initialize
17
15
  @modules = []
18
16
  end
19
17
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Mobility
2
3
  =begin
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/active_model/backend_resetter"
2
3
 
3
4
  module Mobility
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/active_record/translation"
2
3
 
3
4
  module Mobility
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/active_record/translation"
2
3
 
3
4
  module Mobility
@@ -0,0 +1,19 @@
1
+ module Mobility
2
+ =begin
3
+
4
+ Provides interface to access attributes across all backends of an instance.
5
+
6
+ =end
7
+ class Adapter
8
+ # @param [Object] model Instance of model class
9
+ def initialize(model)
10
+ @model = model
11
+ end
12
+
13
+ # Fetch backend for an attribute
14
+ # @param [String] attribute Attribute
15
+ def backend_for(attribute)
16
+ @model.send(Backend.method_name(attribute))
17
+ end
18
+ end
19
+ end
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/util"
2
3
 
3
4
  module Mobility
@@ -138,7 +139,7 @@ with other backends.
138
139
  end
139
140
 
140
141
  # Setup backend class, include modules into model class, add this
141
- # attributes module to shared {Mobility::Wrapper} and setup model with
142
+ # attributes module to shared {Mobility::Accumulator} and setup model with
142
143
  # backend setup block (see {Mobility::Backend::Setup#setup_model}).
143
144
  # @param klass [Class] Class of model
144
145
  def included(klass)
@@ -116,7 +116,7 @@ On top of this, a backend will normally:
116
116
  # @return [String] name of backend reader method
117
117
  def self.method_name(attribute)
118
118
  @backend_method_names ||= {}
119
- @backend_method_names[attribute] ||= "#{attribute}_backend".freeze
119
+ @backend_method_names[attribute] ||= "#{attribute}_backend"
120
120
  end
121
121
 
122
122
  # Defines setup hooks for backend to customize model class.
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  module Mobility
2
3
  module Backend
3
4
  =begin
@@ -16,15 +17,15 @@ Adds {#for} method to backend to return ORM-specific backend.
16
17
  # @param [Class] model_class Class of model
17
18
  # @return [Class] Class of backend to use for model
18
19
  def for(model_class)
19
- namespace = name.split('::'.freeze)
20
+ namespace = name.split('::')
20
21
  if Loaded::ActiveRecord && model_class < ::ActiveRecord::Base
21
22
  require_backend("active_record", namespace.last.underscore)
22
- const_get(namespace.insert(-2, "ActiveRecord".freeze).join("::".freeze))
23
+ const_get(namespace.insert(-2, "ActiveRecord").join("::"))
23
24
  elsif Loaded::Sequel && model_class < ::Sequel::Model
24
25
  require_backend("sequel", namespace.last.underscore)
25
- const_get(namespace.insert(-2, "Sequel".freeze).join("::".freeze))
26
+ const_get(namespace.insert(-2, "Sequel").join("::"))
26
27
  else
27
- raise ArgumentError, "#{namespace.last} backend can only be used by ActiveRecord or Sequel models".freeze
28
+ raise ArgumentError, "#{namespace.last} backend can only be used by ActiveRecord or Sequel models"
28
29
  end
29
30
  end
30
31
 
@@ -1,3 +1,5 @@
1
+ # frozen_string_literal: true
2
+
1
3
  module Mobility
2
4
  =begin
3
5
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/backends/active_record"
2
3
  require "mobility/backends/column"
3
4
 
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/backends/active_record/query_methods"
2
3
 
3
4
  module Mobility
@@ -1,3 +1,4 @@
1
+ # frozen_string_literal: true
1
2
  require "mobility/backends/active_record"
2
3
 
3
4
  module Mobility
@@ -10,7 +11,8 @@ Implements the {Mobility::Backends::Container} backend for ActiveRecord models.
10
11
  class ActiveRecord::Container
11
12
  include ActiveRecord
12
13
 
13
- require 'mobility/backends/active_record/container/query_methods'
14
+ require 'mobility/backends/active_record/container/json_query_methods'
15
+ require 'mobility/backends/active_record/container/jsonb_query_methods'
14
16
 
15
17
  # @return [Symbol] name of container column
16
18
  attr_reader :column_name
@@ -47,8 +49,14 @@ Implements the {Mobility::Backends::Container} backend for ActiveRecord models.
47
49
 
48
50
  # @!group Backend Configuration
49
51
  # @option options [Symbol] column_name (:translations) Name of column on which to store translations
52
+ # @raise [InvalidColumnType] if the type of the container column is not json or jsonb
50
53
  def self.configure(options)
51
54
  options[:column_name] ||= :translations
55
+ options[:column_name] = options[:column_name].to_sym
56
+ options[:column_type] = options[:model_class].type_for_attribute(options[:column_name].to_s).try(:type)
57
+ unless %i[json jsonb].include?(options[:column_type])
58
+ raise InvalidColumnType, "#{options[:column_name]} must be a column of type json or jsonb"
59
+ end
52
60
  end
53
61
  # @!endgroup
54
62
 
@@ -59,7 +67,9 @@ Implements the {Mobility::Backends::Container} backend for ActiveRecord models.
59
67
  end
60
68
  end
61
69
 
62
- setup do |_attributes, options|
70
+ backend_class = self
71
+
72
+ setup do |attributes, options|
63
73
  store options[:column_name], coder: Coder
64
74
 
65
75
  # Fix for duping depth-2 jsonb column in AR < 5.0
@@ -78,9 +88,14 @@ Implements the {Mobility::Backends::Container} backend for ActiveRecord models.
78
88
  include const_set(module_name, dupable)
79
89
  end
80
90
  end
81
- end
82
91
 
83
- setup_query_methods(QueryMethods)
92
+ query_methods = backend_class.const_get("#{options[:column_type].capitalize}QueryMethods")
93
+ extend(Module.new do
94
+ define_method ::Mobility.query_method do
95
+ super().extending(query_methods.new(attributes, options))
96
+ end
97
+ end)
98
+ end
84
99
 
85
100
  private
86
101
 
@@ -113,6 +128,8 @@ Implements the {Mobility::Backends::Container} backend for ActiveRecord models.
113
128
  obj
114
129
  end
115
130
  end
131
+
132
+ class InvalidColumnType < StandardError; end
116
133
  end
117
134
  end
118
135
  end