mobility 1.0.0.alpha → 1.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (76) 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 +54 -1
  5. data/Gemfile +5 -16
  6. data/Gemfile.lock +30 -82
  7. data/README.md +24 -29
  8. data/lib/mobility.rb +67 -9
  9. data/lib/mobility/backend.rb +8 -10
  10. data/lib/mobility/backends.rb +1 -1
  11. data/lib/mobility/backends/active_record.rb +1 -1
  12. data/lib/mobility/backends/active_record/column.rb +1 -1
  13. data/lib/mobility/backends/active_record/container.rb +6 -9
  14. data/lib/mobility/backends/active_record/hstore.rb +4 -4
  15. data/lib/mobility/backends/active_record/json.rb +3 -3
  16. data/lib/mobility/backends/active_record/jsonb.rb +3 -3
  17. data/lib/mobility/backends/active_record/key_value.rb +27 -11
  18. data/lib/mobility/backends/active_record/serialized.rb +4 -0
  19. data/lib/mobility/backends/active_record/table.rb +12 -7
  20. data/lib/mobility/backends/container.rb +10 -2
  21. data/lib/mobility/backends/hash_valued.rb +4 -0
  22. data/lib/mobility/backends/jsonb.rb +1 -1
  23. data/lib/mobility/backends/key_value.rb +12 -15
  24. data/lib/mobility/backends/sequel.rb +34 -2
  25. data/lib/mobility/backends/sequel/container.rb +8 -8
  26. data/lib/mobility/backends/sequel/hstore.rb +1 -1
  27. data/lib/mobility/backends/sequel/json.rb +1 -0
  28. data/lib/mobility/backends/sequel/key_value.rb +79 -12
  29. data/lib/mobility/backends/sequel/pg_hash.rb +6 -6
  30. data/lib/mobility/backends/sequel/serialized.rb +4 -0
  31. data/lib/mobility/backends/sequel/table.rb +18 -8
  32. data/lib/mobility/backends/table.rb +29 -29
  33. data/lib/mobility/pluggable.rb +21 -1
  34. data/lib/mobility/plugin.rb +2 -2
  35. data/lib/mobility/plugins.rb +2 -0
  36. data/lib/mobility/plugins/active_model/dirty.rb +11 -5
  37. data/lib/mobility/plugins/active_record.rb +3 -0
  38. data/lib/mobility/plugins/active_record/backend.rb +2 -0
  39. data/lib/mobility/plugins/active_record/query.rb +7 -7
  40. data/lib/mobility/plugins/active_record/uniqueness_validation.rb +5 -1
  41. data/lib/mobility/plugins/arel.rb +125 -0
  42. data/lib/mobility/plugins/arel/nodes.rb +15 -0
  43. data/lib/mobility/plugins/arel/nodes/pg_ops.rb +134 -0
  44. data/lib/mobility/plugins/attribute_methods.rb +1 -0
  45. data/lib/mobility/plugins/attributes.rb +17 -15
  46. data/lib/mobility/plugins/backend.rb +45 -22
  47. data/lib/mobility/plugins/cache.rb +12 -5
  48. data/lib/mobility/plugins/default.rb +1 -1
  49. data/lib/mobility/plugins/fallbacks.rb +4 -4
  50. data/lib/mobility/plugins/fallthrough_accessors.rb +5 -6
  51. data/lib/mobility/plugins/locale_accessors.rb +2 -5
  52. data/lib/mobility/plugins/presence.rb +1 -1
  53. data/lib/mobility/plugins/reader.rb +2 -2
  54. data/lib/mobility/plugins/sequel/dirty.rb +2 -2
  55. data/lib/mobility/plugins/writer.rb +1 -1
  56. data/lib/mobility/version.rb +2 -2
  57. data/lib/rails/generators/mobility/templates/create_string_translations.rb +0 -1
  58. data/lib/rails/generators/mobility/templates/create_text_translations.rb +0 -1
  59. data/lib/rails/generators/mobility/templates/initializer.rb +11 -3
  60. metadata +14 -20
  61. metadata.gz.sig +0 -0
  62. data/lib/mobility/active_record/model_translation.rb +0 -14
  63. data/lib/mobility/active_record/string_translation.rb +0 -10
  64. data/lib/mobility/active_record/text_translation.rb +0 -10
  65. data/lib/mobility/active_record/translation.rb +0 -14
  66. data/lib/mobility/arel.rb +0 -49
  67. data/lib/mobility/arel/nodes.rb +0 -13
  68. data/lib/mobility/arel/nodes/pg_ops.rb +0 -132
  69. data/lib/mobility/arel/visitor.rb +0 -61
  70. data/lib/mobility/sequel/column_changes.rb +0 -28
  71. data/lib/mobility/sequel/hash_initializer.rb +0 -21
  72. data/lib/mobility/sequel/model_translation.rb +0 -20
  73. data/lib/mobility/sequel/sql.rb +0 -16
  74. data/lib/mobility/sequel/string_translation.rb +0 -10
  75. data/lib/mobility/sequel/text_translation.rb +0 -10
  76. data/lib/mobility/sequel/translation.rb +0 -53
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: d2cfc984b6de27771cbdbe1e7cba1864134d7008a3c07a75a4f890ca0007a956
4
- data.tar.gz: b7ea3bc4122a9f536a58bd00dc18b8c47151168e39dc8b315bdc5ade77e6385b
3
+ metadata.gz: cf9b852c3efdabe4bf4bd64e29a036f93b02169614965e37621f3f8b54315c7a
4
+ data.tar.gz: 3f4b71f87943fabcb6eae21c1960ffb20cb555cbb487c16a213ec4a5bb4f4824
5
5
  SHA512:
6
- metadata.gz: 745a3462d4ca4b9fe9a392a2c38e95a3a59626c8d560a7eaa5eb3650320c02a2b88797fe16bb5a203a3f0e42a401f632fcf620a5dcc0cc6e1f579dcb132a6a8e
7
- data.tar.gz: 54031f4838a210915a964b954289e435cd4280c663b24c5516b6050318c9a0551b9912d36c968a71c17ac9199d30f0edea40cd4441046f38d4085f5195c73606
6
+ metadata.gz: 49c5b97f0f2c4d1e260fa49b663334aca25fd8c0bbe5aa958a104d6cbbba31a6a6ea4403bb930f315258f2b07ef533fae0891f9fb189645ed2f1be56ff6e86ff
7
+ data.tar.gz: d132e09bc34036fdf7fe8cda20a7c337e714415b1999fdfd9319148e8584d3567a7f1fc494842bacbaebdb0881fc9b46b49a86b7de6a6ff8cc791f4feaf5192c
Binary file
data.tar.gz.sig CHANGED
Binary file
@@ -1,6 +1,59 @@
1
1
  # Mobility Changelog
2
2
 
3
- ## 1.0.0.alpha (unreleased)
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
+
9
+ ## 1.0
10
+
11
+ 1.0 is a rewrite of many internals of the gem. Please see the [wiki page on
12
+ v1.0](https://github.com/shioyama/mobility/wiki/Introduction-to-Mobility-v1.0)
13
+ for more details on how to upgrade.
14
+
15
+ ### 1.0.1
16
+
17
+ - Make `Mobility::Plugins::ActiveRecord::Query::VirtualRow` and
18
+ `Mobility::Plugins::ActiveRecord::Query::QueryExtension`
19
+ ([#471](https://github.com/shioyama/mobility/pull/471)) public
20
+ - Fix typo in initializer template
21
+ ([#474](https://github.com/shioyama/mobility/pull/474))
22
+
23
+ ### 1.0.0
24
+
25
+ - Fix setting a locale from Rails config
26
+ ([#468](https://github.com/shioyama/mobility/pull/468)) thanks
27
+ [sergey-alekseev](https://github.com/sergey-alekseev)!
28
+ - Fixes validations with validates_uniqueness_of
29
+ ([#470](https://github.com/shioyama/mobility/pull/470)) thanks
30
+ [artplan1](https://github.com/artplan1)!
31
+
32
+ ### 1.0.0.rc1 (pre-release)
33
+
34
+ - Remove `Mobility::ActiveRecord`, `Mobility::Sequel` and `Mobility::Arel`, and
35
+ general cleanup ([#464](https://github.com/shioyama/mobility/pull/464))
36
+
37
+ ### 1.0.0.beta2 (pre-release)
38
+
39
+ - Refactor attributes & backend plugins and make `mobility_attributes` public
40
+ ([#462](https://github.com/shioyama/mobility/pull/462))
41
+ - Make attribute_methods plugin depend on attributes
42
+ ([#461](https://github.com/shioyama/mobility/pull/461))
43
+
44
+ ### 1.0.0.beta1 (pre-release)
45
+
46
+ - Remove `Mobility::Backend#apply_plugin`
47
+ ([#454](https://github.com/shioyama/mobility/pull/454))
48
+ - Instance exec configure block if it takes no arguments
49
+ ([#456](https://github.com/shioyama/mobility/pull/456))
50
+ - Raise an exception if invalid options are passed to Translations initializer
51
+ ([#457](https://github.com/shioyama/mobility/pull/457/files))
52
+ - Fix Ruby 2.7 deprecation warnings
53
+ ([#460](https://github.com/shioyama/mobility/pull/460))
54
+
55
+ ### 1.0.0.alpha (pre-release)
56
+
4
57
  - Default fallbacks plugin to `true` when enabled
5
58
  ([#447](https://github.com/shioyama/mobility/pull/447))
6
59
  - Remove `Mobility::Backend.method_name`
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,64 +1,32 @@
1
- GIT
2
- remote: https://github.com/rails/rails.git
3
- revision: 0a608bd987fde4f9bc5bcf4bcebdb181d199cf4f
4
- specs:
5
- actionpack (6.1.0.alpha)
6
- actionview (= 6.1.0.alpha)
7
- activesupport (= 6.1.0.alpha)
8
- rack (~> 2.0, >= 2.0.9)
9
- rack-test (>= 0.6.3)
10
- rails-dom-testing (~> 2.0)
11
- rails-html-sanitizer (~> 1.0, >= 1.2.0)
12
- actionview (6.1.0.alpha)
13
- activesupport (= 6.1.0.alpha)
14
- builder (~> 3.1)
15
- erubi (~> 1.4)
16
- rails-dom-testing (~> 2.0)
17
- rails-html-sanitizer (~> 1.1, >= 1.2.0)
18
- activemodel (6.1.0.alpha)
19
- activesupport (= 6.1.0.alpha)
20
- activerecord (6.1.0.alpha)
21
- activemodel (= 6.1.0.alpha)
22
- activesupport (= 6.1.0.alpha)
23
- activesupport (6.1.0.alpha)
24
- concurrent-ruby (~> 1.0, >= 1.0.2)
25
- i18n (>= 1.6, < 2)
26
- minitest (>= 5.1)
27
- tzinfo (~> 2.0)
28
- zeitwerk (~> 2.3)
29
- railties (6.1.0.alpha)
30
- actionpack (= 6.1.0.alpha)
31
- activesupport (= 6.1.0.alpha)
32
- method_source
33
- rake (>= 0.8.7)
34
- thor (~> 1.0)
35
-
36
1
  PATH
37
2
  remote: .
38
3
  specs:
39
- mobility (1.0.0.alpha)
4
+ mobility (1.0.0)
40
5
  i18n (>= 0.6.10, < 2)
41
6
  request_store (~> 1.0)
42
7
 
43
8
  GEM
44
9
  remote: https://rubygems.org/
45
10
  specs:
46
- actionmailer (0.6.1)
47
- actionpack (>= 0.9.5)
48
- benchmark-ips (2.8.2)
49
- builder (3.2.4)
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)
22
+ benchmark-ips (2.8.3)
50
23
  byebug (11.1.3)
51
24
  coderay (1.1.3)
52
25
  concurrent-ruby (1.1.7)
53
- crass (1.0.6)
54
26
  database_cleaner (1.8.5)
55
27
  diff-lcs (1.4.4)
56
- erubi (1.9.0)
57
28
  ffi (1.13.1)
58
29
  formatador (0.2.5)
59
- generator_spec (0.9.4)
60
- activesupport (>= 3.0.0)
61
- railties (>= 3.0.0)
62
30
  guard (2.16.2)
63
31
  formatador (>= 0.2.4)
64
32
  listen (>= 2.7, < 4.0)
@@ -78,16 +46,10 @@ GEM
78
46
  listen (3.2.1)
79
47
  rb-fsevent (~> 0.10, >= 0.10.3)
80
48
  rb-inotify (~> 0.9, >= 0.9.10)
81
- loofah (2.7.0)
82
- crass (~> 1.0.2)
83
- nokogiri (>= 1.5.9)
84
- lumberjack (1.2.7)
49
+ lumberjack (1.2.8)
85
50
  method_source (1.0.0)
86
- mini_portile2 (2.4.0)
87
51
  minitest (5.14.2)
88
52
  nenv (0.3.0)
89
- nokogiri (1.10.10)
90
- mini_portile2 (~> 2.4.0)
91
53
  notiffany (0.1.3)
92
54
  nenv (~> 0.1)
93
55
  shellany (~> 0.0)
@@ -99,58 +61,44 @@ GEM
99
61
  byebug (~> 11.0)
100
62
  pry (~> 0.13.0)
101
63
  rack (2.2.3)
102
- rack-test (1.1.0)
103
- rack (>= 1.0, < 3)
104
- rails (0.9.5)
105
- actionmailer (>= 0.6.1)
106
- actionpack (>= 1.4.0)
107
- activerecord (>= 1.6.0)
108
- rake (>= 0.4.15)
109
- rails-dom-testing (2.0.3)
110
- activesupport (>= 4.2.0)
111
- nokogiri (>= 1.6)
112
- rails-html-sanitizer (1.3.0)
113
- loofah (~> 2.3)
114
64
  rake (12.3.3)
115
65
  rb-fsevent (0.10.4)
116
66
  rb-inotify (0.10.1)
117
67
  ffi (~> 1.0)
118
68
  request_store (1.5.0)
119
69
  rack (>= 1.4)
120
- rspec (3.9.0)
121
- rspec-core (~> 3.9.0)
122
- rspec-expectations (~> 3.9.0)
123
- rspec-mocks (~> 3.9.0)
124
- rspec-core (3.9.2)
125
- rspec-support (~> 3.9.3)
126
- rspec-expectations (3.9.2)
70
+ rspec (3.10.0)
71
+ rspec-core (~> 3.10.0)
72
+ rspec-expectations (~> 3.10.0)
73
+ rspec-mocks (~> 3.10.0)
74
+ rspec-core (3.10.0)
75
+ rspec-support (~> 3.10.0)
76
+ rspec-expectations (3.10.0)
127
77
  diff-lcs (>= 1.2.0, < 2.0)
128
- rspec-support (~> 3.9.0)
129
- rspec-mocks (3.9.1)
78
+ rspec-support (~> 3.10.0)
79
+ rspec-mocks (3.10.0)
130
80
  diff-lcs (>= 1.2.0, < 2.0)
131
- rspec-support (~> 3.9.0)
132
- rspec-support (3.9.3)
81
+ rspec-support (~> 3.10.0)
82
+ rspec-support (3.10.0)
133
83
  shellany (0.0.1)
134
84
  thor (1.0.1)
135
- tzinfo (2.0.2)
136
- concurrent-ruby (~> 1.0)
85
+ thread_safe (0.3.6)
86
+ tzinfo (1.2.9)
87
+ thread_safe (~> 0.1)
137
88
  yard (0.9.25)
138
- zeitwerk (2.4.0)
89
+ zeitwerk (2.4.2)
139
90
 
140
91
  PLATFORMS
141
92
  ruby
142
93
 
143
94
  DEPENDENCIES
144
- activerecord!
145
- activesupport!
95
+ activerecord (~> 6.0.0)
146
96
  benchmark-ips
147
97
  database_cleaner (~> 1.5, >= 1.5.3)
148
- generator_spec (~> 0.9.4)
149
98
  guard-rspec
150
99
  mobility!
151
100
  pg
152
101
  pry-byebug
153
- rails
154
102
  rake (~> 12, >= 12.2.1)
155
103
  rspec (~> 3.0)
156
104
  yard (~> 0.9.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.alpha, 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
@@ -54,16 +52,13 @@ section of the wiki.
54
52
  Installation
55
53
  ------------
56
54
 
57
- To use the latest (unreleased) version of Mobility, add this line to your
55
+ 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', git: 'https://github.com/shioyama/mobility.git'
59
+ gem 'mobility', '~> 1.0.1'
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,6 +104,7 @@ 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
110
  translations_class.defaults[:backend]
@@ -57,11 +112,14 @@ module Mobility
57
112
 
58
113
  # Configure Mobility
59
114
  # @yield [Mobility::Translations]
60
- def configure
115
+ def configure(&block)
61
116
  translates_with(Class.new(Translations)) unless @translations_class
62
- yield translations_class
117
+ if block.arity == 0
118
+ translations_class.instance_exec(&block)
119
+ else
120
+ yield translations_class
121
+ end
63
122
  end
64
- # @!endgroup
65
123
 
66
124
  def translates_with(pluggable)
67
125
  raise ArgumentError, "translations class must be a subclass of Module." unless Module === pluggable
@@ -160,7 +218,7 @@ module Mobility
160
218
  # methods (in LocaleAccessors) than is really necessary.
161
219
  def available_locales
162
220
  if defined?(Rails) && Rails.application
163
- Rails.application.config.i18n.available_locales || I18n.available_locales
221
+ Rails.application.config.i18n.available_locales&.map(&:to_sym) || I18n.available_locales
164
222
  else
165
223
  I18n.available_locales
166
224
  end