mobility 1.0.0.beta2 → 1.0.3

Sign up to get free protection for your applications and to get access to all the features.
Files changed (52) 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 +49 -4
  5. data/Gemfile +5 -16
  6. data/Gemfile.lock +20 -1
  7. data/README.md +23 -28
  8. data/lib/mobility.rb +62 -8
  9. data/lib/mobility/backends/active_record.rb +1 -1
  10. data/lib/mobility/backends/active_record/column.rb +1 -1
  11. data/lib/mobility/backends/active_record/container.rb +4 -4
  12. data/lib/mobility/backends/active_record/hstore.rb +3 -3
  13. data/lib/mobility/backends/active_record/json.rb +3 -3
  14. data/lib/mobility/backends/active_record/jsonb.rb +3 -3
  15. data/lib/mobility/backends/active_record/key_value.rb +27 -11
  16. data/lib/mobility/backends/active_record/table.rb +11 -6
  17. data/lib/mobility/backends/sequel.rb +32 -0
  18. data/lib/mobility/backends/sequel/container.rb +5 -3
  19. data/lib/mobility/backends/sequel/key_value.rb +79 -12
  20. data/lib/mobility/backends/sequel/pg_hash.rb +6 -6
  21. data/lib/mobility/backends/sequel/table.rb +18 -8
  22. data/lib/mobility/backends/table.rb +11 -6
  23. data/lib/mobility/plugin.rb +2 -2
  24. data/lib/mobility/plugins/active_record.rb +3 -0
  25. data/lib/mobility/plugins/active_record/backend.rb +2 -0
  26. data/lib/mobility/plugins/active_record/query.rb +7 -7
  27. data/lib/mobility/plugins/active_record/uniqueness_validation.rb +4 -0
  28. data/lib/mobility/plugins/arel.rb +125 -0
  29. data/lib/mobility/plugins/arel/nodes.rb +15 -0
  30. data/lib/mobility/plugins/arel/nodes/pg_ops.rb +134 -0
  31. data/lib/mobility/plugins/sequel/dirty.rb +1 -1
  32. data/lib/mobility/version.rb +2 -2
  33. data/lib/rails/generators/mobility/templates/create_string_translations.rb +0 -1
  34. data/lib/rails/generators/mobility/templates/create_text_translations.rb +0 -1
  35. data/lib/rails/generators/mobility/templates/initializer.rb +9 -1
  36. metadata +14 -20
  37. metadata.gz.sig +0 -0
  38. data/lib/mobility/active_record/model_translation.rb +0 -14
  39. data/lib/mobility/active_record/string_translation.rb +0 -10
  40. data/lib/mobility/active_record/text_translation.rb +0 -10
  41. data/lib/mobility/active_record/translation.rb +0 -14
  42. data/lib/mobility/arel.rb +0 -49
  43. data/lib/mobility/arel/nodes.rb +0 -13
  44. data/lib/mobility/arel/nodes/pg_ops.rb +0 -132
  45. data/lib/mobility/arel/visitor.rb +0 -61
  46. data/lib/mobility/sequel/column_changes.rb +0 -28
  47. data/lib/mobility/sequel/hash_initializer.rb +0 -21
  48. data/lib/mobility/sequel/model_translation.rb +0 -20
  49. data/lib/mobility/sequel/sql.rb +0 -16
  50. data/lib/mobility/sequel/string_translation.rb +0 -10
  51. data/lib/mobility/sequel/text_translation.rb +0 -10
  52. data/lib/mobility/sequel/translation.rb +0 -53
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 2346e4ef46985b520c5feea3577a6644f213e1fe89e3dfd5960f3347c72a1134
4
- data.tar.gz: df38726f94dca7139a3eb9cf838b27dfede5378c34290a5298e7dcde01bf3c90
3
+ metadata.gz: 21ca551cdc17159c12fc493282038fba6e5274bd0ff56f5cd9d95a5bb1080cff
4
+ data.tar.gz: 611ff563e7e3139c5db31ff1337a40082a9ed5e04349a87e748754cf32779817
5
5
  SHA512:
6
- metadata.gz: 236ad0894645f98bf2a21c6848b4ba439da84b24862a56f6fca878889a6cd9a79108711923961d2f9ee75b6a6ee8482fcefff213b5433736fa507332db0cc253
7
- data.tar.gz: a3e78f97d08d26e18ac8187b961097e686c3fc5650f45ae84fb4aa1aad1ccd0970410f2b9e887d8618ed6c1a09e559b1fe3fd0428df591de011961a0c2da1379
6
+ metadata.gz: 88a9c728d973e58532241802e2f2b99fc1b6d95b5d41d41ff87b9818378294b6795f8d8461600047aa4b3d4024e7e557a4e7ef0738fc99474a8dc741c91ee459
7
+ data.tar.gz: 34a4ab6a5db631eb4e4fcddf519825e25e8efbec533ad1b9b92e4fdae7085149f29d25978a8c121b71ed701ed18db6de410ff2ddd157896364f379b215d98262
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,17 +1,62 @@
1
1
  # Mobility Changelog
2
2
 
3
+ ### Unreleased
4
+ - Remove `Mobility::Plugins::Attributes#each`
5
+ ([#475](https://github.com/shioyama/mobility/pull/475))
6
+ - Add public method `Mobility::Plugins::ActiveRecord::Query.build_query`
7
+ ([#471](https://github.com/shioyama/mobility/pull/471))
8
+ - Allow setting locale per node in query block
9
+ ([#479](https://github.com/shioyama/mobility/pull/479))
10
+ - Add `Mobility.validate_locale!`
11
+ ([#479](https://github.com/shioyama/mobility/pull/479))
12
+ - Call after_destroy in setup block to delete associated translations
13
+ ([#487](https://github.com/shioyama/mobility/pull/487))
14
+ - Add key, value and belongs_to options to key-value backend
15
+ ([#488](https://github.com/shioyama/mobility/pull/488)) thanks
16
+ [sedubois](https://github.com/sedubois)!
17
+
3
18
  ## 1.0
4
19
 
5
- 1.0 is a rewrite of many internals of the gem. Please see the [wiki page on v1.0](https://github.com/shioyama/mobility/wiki/Introduction-to-Mobility-v1.0) for more details on how to upgrade.
20
+ 1.0 is a rewrite of many internals of the gem. Please see the [wiki page on
21
+ v1.0](https://github.com/shioyama/mobility/wiki/Introduction-to-Mobility-v1.0)
22
+ for more details on how to upgrade.
23
+
24
+ ### 1.0.3
25
+ - Fix `Mobility.default_backend`
26
+ ([#497](https://github.com/shioyama/mobility/pull/497))
27
+
28
+ ### 1.0.2
29
+
30
+ ### 1.0.1
31
+
32
+ - Make `Mobility::Plugins::ActiveRecord::Query::VirtualRow` and
33
+ `Mobility::Plugins::ActiveRecord::Query::QueryExtension`
34
+ ([#471](https://github.com/shioyama/mobility/pull/471)) public
35
+ - Fix typo in initializer template
36
+ ([#474](https://github.com/shioyama/mobility/pull/474))
37
+
38
+ ### 1.0.0
39
+
40
+ - Fix setting a locale from Rails config
41
+ ([#468](https://github.com/shioyama/mobility/pull/468)) thanks
42
+ [sergey-alekseev](https://github.com/sergey-alekseev)!
43
+ - Fixes validations with validates_uniqueness_of
44
+ ([#470](https://github.com/shioyama/mobility/pull/470)) thanks
45
+ [artplan1](https://github.com/artplan1)!
46
+
47
+ ### 1.0.0.rc1 (pre-release)
48
+
49
+ - Remove `Mobility::ActiveRecord`, `Mobility::Sequel` and `Mobility::Arel`, and
50
+ general cleanup ([#464](https://github.com/shioyama/mobility/pull/464))
6
51
 
7
- ## 1.0.0.beta2 (pre-release)
52
+ ### 1.0.0.beta2 (pre-release)
8
53
 
9
54
  - Refactor attributes & backend plugins and make `mobility_attributes` public
10
55
  ([#462](https://github.com/shioyama/mobility/pull/462))
11
56
  - Make attribute_methods plugin depend on attributes
12
57
  ([#461](https://github.com/shioyama/mobility/pull/461))
13
58
 
14
- ## 1.0.0.beta1 (pre-release)
59
+ ### 1.0.0.beta1 (pre-release)
15
60
 
16
61
  - Remove `Mobility::Backend#apply_plugin`
17
62
  ([#454](https://github.com/shioyama/mobility/pull/454))
@@ -22,7 +67,7 @@
22
67
  - Fix Ruby 2.7 deprecation warnings
23
68
  ([#460](https://github.com/shioyama/mobility/pull/460))
24
69
 
25
- ## 1.0.0.alpha (pre-release)
70
+ ### 1.0.0.alpha (pre-release)
26
71
 
27
72
  - Default fallbacks plugin to `true` when enabled
28
73
  ([#447](https://github.com/shioyama/mobility/pull/447))
data/Gemfile CHANGED
@@ -8,20 +8,11 @@ orm, orm_version = ENV['ORM'], ENV['ORM_VERSION']
8
8
  group :development, :test do
9
9
  case orm
10
10
  when 'active_record'
11
- orm_version ||= '6.0'
11
+ orm_version ||= '6.1'
12
12
  case orm_version
13
- when '4.2'
14
- gem 'activerecord', '>= 4.2.6', '< 5.0'
15
- when '5.0'
16
- gem 'activerecord', '>= 5.0', '< 5.1'
17
- when '5.1'
18
- gem 'activerecord', '>= 5.1', '< 5.2'
19
- when '5.2'
20
- gem 'activerecord', '>= 5.2.0', '< 5.3'
21
- gem 'railties', '>= 5.2.0.rc2', '< 5.3'
22
- when '6.0'
23
- gem 'activerecord', '>= 6.0.0', '< 6.1'
24
- when '6.1'
13
+ when '4.2', '5.0', '5.1', '5.2', '6.0', '6.1'
14
+ gem 'activerecord', "~> #{orm_version}.0"
15
+ when '6.2'
25
16
  git 'https://github.com/rails/rails.git' do
26
17
  gem 'activerecord'
27
18
  gem 'activesupport'
@@ -32,10 +23,8 @@ group :development, :test do
32
23
  when 'sequel'
33
24
  orm_version ||= '5'
34
25
  case orm_version
35
- when '4'
36
- gem 'sequel', '>= 4.46.0', '< 5.0'
37
26
  when '5'
38
- gem 'sequel', '>= 5.0.0', '< 6.0.0'
27
+ gem 'sequel', "~> #{orm_version}.0"
39
28
  else
40
29
  raise ArgumentError, 'Invalid Sequel version'
41
30
  end
@@ -1,13 +1,24 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- mobility (1.0.0.beta2)
4
+ mobility (1.0.1)
5
5
  i18n (>= 0.6.10, < 2)
6
6
  request_store (~> 1.0)
7
7
 
8
8
  GEM
9
9
  remote: https://rubygems.org/
10
10
  specs:
11
+ activemodel (6.0.3.4)
12
+ activesupport (= 6.0.3.4)
13
+ activerecord (6.0.3.4)
14
+ activemodel (= 6.0.3.4)
15
+ activesupport (= 6.0.3.4)
16
+ activesupport (6.0.3.4)
17
+ concurrent-ruby (~> 1.0, >= 1.0.2)
18
+ i18n (>= 0.7, < 2)
19
+ minitest (~> 5.1)
20
+ tzinfo (~> 1.1)
21
+ zeitwerk (~> 2.2, >= 2.2.2)
11
22
  benchmark-ips (2.8.3)
12
23
  byebug (11.1.3)
13
24
  coderay (1.1.3)
@@ -37,10 +48,12 @@ GEM
37
48
  rb-inotify (~> 0.9, >= 0.9.10)
38
49
  lumberjack (1.2.8)
39
50
  method_source (1.0.0)
51
+ minitest (5.14.3)
40
52
  nenv (0.3.0)
41
53
  notiffany (0.1.3)
42
54
  nenv (~> 0.1)
43
55
  shellany (~> 0.0)
56
+ pg (1.2.3)
44
57
  pry (0.13.1)
45
58
  coderay (~> 1.1)
46
59
  method_source (~> 1.0)
@@ -69,16 +82,22 @@ GEM
69
82
  rspec-support (3.10.0)
70
83
  shellany (0.0.1)
71
84
  thor (1.0.1)
85
+ thread_safe (0.3.6)
86
+ tzinfo (1.2.9)
87
+ thread_safe (~> 0.1)
72
88
  yard (0.9.25)
89
+ zeitwerk (2.4.2)
73
90
 
74
91
  PLATFORMS
75
92
  ruby
76
93
 
77
94
  DEPENDENCIES
95
+ activerecord (~> 6.0.0)
78
96
  benchmark-ips
79
97
  database_cleaner (~> 1.5, >= 1.5.3)
80
98
  guard-rspec
81
99
  mobility!
100
+ pg
82
101
  pry-byebug
83
102
  rake (~> 12, >= 12.2.1)
84
103
  rspec (~> 3.0)
data/README.md CHANGED
@@ -12,11 +12,9 @@ Mobility
12
12
  [docs]: http://www.rubydoc.info/gems/mobility
13
13
  [wiki]: https://github.com/shioyama/mobility/wiki
14
14
 
15
- **This is the readme for the [`master`](https://github.com/shioyama/mobility)
16
- branch, which corresponds to v1.0.0.beta, a pre-release version of Mobility.
17
- If you are using an earlier version (0.8.x or earlier), you probably want the
18
- readme on the [0-8-stable
19
- branch](https://github.com/shioyama/mobility/tree/0-8-stable).**
15
+ **This is the readme for version 1.0 of Mobility. If you are using an earlier
16
+ version (0.8.x or earlier), you probably want the readme on the [0-8
17
+ branch](https://github.com/shioyama/mobility/tree/0-8).**
20
18
 
21
19
  Mobility is a gem for storing and retrieving translations as attributes on a
22
20
  class. These translations could be the content of blog posts, captions on
@@ -58,12 +56,9 @@ To use the latest pre-version of Mobility 1.0, add this line to your
58
56
  application's Gemfile:
59
57
 
60
58
  ```ruby
61
- gem 'mobility', '~> 1.0.0.beta2'
59
+ gem 'mobility', '~> 1.0.3'
62
60
  ```
63
61
 
64
- For the latest stable version of Mobility, see the readme on the
65
- [0-8-stable](https://github.com/shioyama/mobility/tree/0-8-stable) branch.
66
-
67
62
  ### ActiveRecord (Rails)
68
63
 
69
64
  Requirements:
@@ -91,10 +86,10 @@ The generator will create an initializer file `config/initializers/mobility.rb`
91
86
  which looks something like this:
92
87
 
93
88
  ```ruby
94
- Mobility.configure do |config|
95
- # PLUGINS
89
+ Mobility.configure do
96
90
 
97
- config.plugins do
91
+ # PLUGINS
92
+ plugins do
98
93
  backend :key_value
99
94
 
100
95
  active_record
@@ -107,15 +102,15 @@ Mobility.configure do |config|
107
102
  end
108
103
  ```
109
104
 
110
- Each method call inside the block passed to `config.plugins` declares a plugin,
111
- along with an optional default. To use a different default backend, you can
105
+ Each method call inside the block passed to `plugins` declares a plugin, along
106
+ with an optional default. To use a different default backend, you can
112
107
  change the default passed to the `backend` plugin, like this:
113
108
 
114
109
  ```diff
115
- Mobility.configure do |config|
116
- # PLUGINS
110
+ Mobility.configure do
117
111
 
118
- config.plugins do
112
+ # PLUGINS
113
+ plugins do
119
114
  - backend :key_value
120
115
  + backend :table
121
116
  ```
@@ -126,10 +121,10 @@ You can also set defaults for backend-specific options. Below, we set the
126
121
  default `type` option for the KeyValue backend to `:string`.
127
122
 
128
123
  ```diff
129
- Mobility.configure do |config|
130
- # PLUGINS
124
+ Mobility.configure do
131
125
 
132
- config.plugins do
126
+ # PLUGINS
127
+ plugins do
133
128
  - backend :key_value
134
129
  + backend :key_value, type: :string
135
130
  end
@@ -150,7 +145,7 @@ Requirements:
150
145
  When configuring Mobility, ensure that you include the `sequel` plugin:
151
146
 
152
147
  ```diff
153
- config.plugins do
148
+ plugins do
154
149
  backend :key_value
155
150
 
156
151
  - active_record
@@ -297,7 +292,7 @@ accessors" in Mobility, and can be enabled by including the `locale_accessors`
297
292
  plugin, with a default set of accessors:
298
293
 
299
294
  ```diff
300
- config.plugins do
295
+ plugins do
301
296
  # ...
302
297
  + locale_accessors [:en, :ja]
303
298
  ```
@@ -345,7 +340,7 @@ defined for a given locale.)
345
340
  Ensure the plugin is enabled:
346
341
 
347
342
  ```diff
348
- config.plugins do
343
+ plugins do
349
344
  # ...
350
345
  + fallthrough_accessors
351
346
  ```
@@ -470,7 +465,7 @@ Mobility offers basic support for translation fallbacks. First, enable the
470
465
  `fallbacks` plugin:
471
466
 
472
467
  ```diff
473
- config.plugins do
468
+ plugins do
474
469
  # ...
475
470
  + fallbacks
476
471
  + locale_accessors
@@ -579,7 +574,7 @@ fallbacks](https://github.com/svenfuchs/i18n/wiki/Fallbacks).
579
574
  Another option is to assign a default value, using the `default` plugin:
580
575
 
581
576
  ```diff
582
- config.plugins do
577
+ plugins do
583
578
  # ...
584
579
  + default 'foo'
585
580
  ```
@@ -632,7 +627,7 @@ have enabled an ORM plugin (either `active_record` or `sequel`), since the
632
627
  dirty plugin will depend on one of these being enabled.
633
628
 
634
629
  ```diff
635
- config.plugins do
630
+ plugins do
636
631
  # ...
637
632
  active_record
638
633
  + dirty
@@ -722,7 +717,7 @@ can be quickly retrieved again. The cache plugin is included in the default
722
717
  configuration created by the install generator:
723
718
 
724
719
  ```diff
725
- config.plugins do
720
+ plugins do
726
721
  # ...
727
722
  + cache
728
723
  ```
@@ -752,7 +747,7 @@ this feature, include the `query` plugin, and ensure you also have an ORM
752
747
  plugin enabled (`active_record` or `sequel`):
753
748
 
754
749
  ```diff
755
- config.plugins do
750
+ plugins do
756
751
  # ...
757
752
  active_record
758
753
  + query
@@ -6,17 +6,71 @@ require 'mobility/version'
6
6
  =begin
7
7
 
8
8
  Mobility is a gem for storing and retrieving localized data through attributes
9
- on a class. The {Mobility} module includes all necessary methods and modules to
10
- support defining backend accessors on a class.
9
+ on a class.
11
10
 
12
- To enable Mobility on a class, simply include or extend the {Mobility} module,
13
- and define any attribute accessors using {Translates#translates}.
11
+ There are two ways to translate attributes on a class, both of which are
12
+ variations on the same basic mechanism. The first and most common way is to
13
+ extend the `Mobility` module, which adds a class method +translates+.
14
+ Translated attributes can then be defined like this:
14
15
 
15
- class MyClass
16
+ class Post
16
17
  extend Mobility
17
18
  translates :title, backend: :key_value
18
19
  end
19
20
 
21
+ Behind the scenes, +translates+ simply creates an instance of
22
+ +Mobility.translations_class+, passes it whatever arguments are passed to
23
+ +translates+, and includes the instance (which is a module) into the class.
24
+
25
+ So the above example is equivalent to:
26
+
27
+ class Post
28
+ Mobility.translations_class.new(:title, backend: :key_value)
29
+ end
30
+
31
+ `Mobility.translations_class` is a subclass of `Mobility::Translations` created
32
+ when `Mobility.configure` is called to configure Mobility. In fact, when you
33
+ call `Mobility.configure`, it is the subclass of `Mobility::Translations` which
34
+ is passed to the block as `config`. Plugins and plugin configuration is all
35
+ applied to the same `Mobility.translations_class`.
36
+
37
+ There is another way to use Mobility, which is to create your own subclass or
38
+ subclasses of +Mobility::Translations+ and include them explicitly, without
39
+ using +translates+.
40
+
41
+ For example:
42
+
43
+ class Translations < Mobility::Translations
44
+ plugins do
45
+ backend :key_value
46
+ # ...
47
+ end
48
+ end
49
+
50
+ class Post
51
+ include Translations.new(:title)
52
+ end
53
+
54
+ This usage might be handy if, for example, you want to have more complex
55
+ configuration, where some models use some plugins while others do not. Since
56
+ `Mobility::Translations` is a class like any other, you can subclass it and
57
+ define plugins specifically on the subclass which are not present on its
58
+ parent:
59
+
60
+ class TranslationsWithFallbacks < Translations
61
+ plugins do
62
+ fallbacks
63
+ end
64
+ end
65
+
66
+ class Comment
67
+ include TranslationsWithFallbacks.new(:author)
68
+ end
69
+
70
+ In this case, +Comment+ uses +TranslationsWithFallbacks+ and thus has the
71
+ fallbacks plugin, whereas +Post+ uses +Translations+ which does not have that
72
+ plugin enabled.
73
+
20
74
  =end
21
75
  module Mobility
22
76
  # A generic exception used by Mobility.
@@ -50,9 +104,10 @@ module Mobility
50
104
  model_class.extend self
51
105
  end
52
106
 
107
+ # Alias to default backend defined on *translations_class+.
53
108
  # @return [Symbol,Class]
54
109
  def default_backend
55
- translations_class.defaults[:backend]
110
+ translations_class.defaults[:backend]&.first
56
111
  end
57
112
 
58
113
  # Configure Mobility
@@ -65,7 +120,6 @@ module Mobility
65
120
  yield translations_class
66
121
  end
67
122
  end
68
- # @!endgroup
69
123
 
70
124
  def translates_with(pluggable)
71
125
  raise ArgumentError, "translations class must be a subclass of Module." unless Module === pluggable
@@ -164,7 +218,7 @@ module Mobility
164
218
  # methods (in LocaleAccessors) than is really necessary.
165
219
  def available_locales
166
220
  if defined?(Rails) && Rails.application
167
- Rails.application.config.i18n.available_locales || I18n.available_locales
221
+ Rails.application.config.i18n.available_locales&.map(&:to_sym) || I18n.available_locales
168
222
  else
169
223
  I18n.available_locales
170
224
  end
@@ -1,6 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
  require "mobility/backend"
3
- require "mobility/arel"
3
+ require "mobility/plugins/arel"
4
4
 
5
5
  module Mobility
6
6
  module Backends
@@ -57,7 +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
+ .extend(Plugins::Arel::MobilityExpressions)
61
61
  end
62
62
 
63
63
  private