mobility 0.5.1 → 0.6.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.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +2 -2
- data.tar.gz.sig +0 -0
- data/CHANGELOG.md +41 -1
- data/Gemfile.lock +3 -58
- data/README.md +22 -21
- data/lib/mobility.rb +3 -2
- data/lib/mobility/accumulator.rb +1 -2
- data/lib/mobility/active_model/backend_resetter.rb +1 -1
- data/lib/mobility/active_record.rb +12 -9
- data/lib/mobility/active_record/backend_resetter.rb +6 -7
- data/lib/mobility/active_record/uniqueness_validator.rb +12 -2
- data/lib/mobility/adapter.rb +1 -0
- data/lib/mobility/attributes.rb +3 -13
- data/lib/mobility/backends/active_record/column.rb +1 -0
- data/lib/mobility/backends/active_record/column/query_methods.rb +25 -20
- data/lib/mobility/backends/active_record/container/json_query_methods.rb +22 -16
- data/lib/mobility/backends/active_record/container/jsonb_query_methods.rb +19 -19
- data/lib/mobility/backends/active_record/hstore.rb +14 -12
- data/lib/mobility/backends/active_record/hstore/query_methods.rb +14 -5
- data/lib/mobility/backends/active_record/json.rb +21 -19
- data/lib/mobility/backends/active_record/json/query_methods.rb +16 -11
- data/lib/mobility/backends/active_record/jsonb.rb +21 -19
- data/lib/mobility/backends/active_record/jsonb/query_methods.rb +14 -5
- data/lib/mobility/backends/active_record/key_value.rb +9 -9
- data/lib/mobility/backends/active_record/key_value/query_methods.rb +53 -46
- data/lib/mobility/backends/active_record/pg_hash.rb +29 -25
- data/lib/mobility/backends/active_record/pg_query_methods.rb +76 -40
- data/lib/mobility/backends/active_record/query_methods.rb +17 -10
- data/lib/mobility/backends/active_record/serialized.rb +4 -2
- data/lib/mobility/backends/active_record/serialized/query_methods.rb +18 -15
- data/lib/mobility/backends/active_record/table.rb +21 -12
- data/lib/mobility/backends/active_record/table/query_methods.rb +82 -83
- data/lib/mobility/backends/hash_valued.rb +19 -0
- data/lib/mobility/backends/hstore.rb +3 -1
- data/lib/mobility/backends/json.rb +3 -1
- data/lib/mobility/backends/jsonb.rb +3 -1
- data/lib/mobility/backends/key_value.rb +32 -15
- data/lib/mobility/backends/sequel/column/query_methods.rb +16 -12
- data/lib/mobility/backends/sequel/container/json_query_methods.rb +25 -18
- data/lib/mobility/backends/sequel/container/jsonb_query_methods.rb +25 -18
- data/lib/mobility/backends/sequel/hstore.rb +14 -12
- data/lib/mobility/backends/sequel/hstore/query_methods.rb +18 -11
- data/lib/mobility/backends/sequel/json.rb +21 -19
- data/lib/mobility/backends/sequel/json/query_methods.rb +18 -11
- data/lib/mobility/backends/sequel/jsonb.rb +21 -19
- data/lib/mobility/backends/sequel/jsonb/query_methods.rb +18 -11
- data/lib/mobility/backends/sequel/key_value.rb +10 -11
- data/lib/mobility/backends/sequel/key_value/query_methods.rb +39 -34
- data/lib/mobility/backends/sequel/pg_hash.rb +37 -25
- data/lib/mobility/backends/sequel/pg_query_methods.rb +45 -20
- data/lib/mobility/backends/sequel/query_methods.rb +5 -0
- data/lib/mobility/backends/sequel/serialized.rb +18 -13
- data/lib/mobility/backends/sequel/serialized/query_methods.rb +10 -7
- data/lib/mobility/backends/sequel/table.rb +1 -1
- data/lib/mobility/backends/sequel/table/query_methods.rb +40 -35
- data/lib/mobility/plugins/cache/translation_cacher.rb +15 -15
- data/lib/mobility/plugins/default.rb +0 -7
- data/lib/mobility/plugins/fallbacks.rb +4 -0
- data/lib/mobility/sequel.rb +11 -5
- data/lib/mobility/sequel/backend_resetter.rb +6 -7
- data/lib/mobility/sequel/column_changes.rb +4 -4
- data/lib/mobility/version.rb +1 -1
- data/lib/rails/generators/mobility/backend_generators/base.rb +4 -0
- data/lib/rails/generators/mobility/backend_generators/table_backend.rb +0 -12
- data/lib/rails/generators/mobility/templates/column_translations.rb +2 -2
- data/lib/rails/generators/mobility/templates/create_string_translations.rb +5 -5
- data/lib/rails/generators/mobility/templates/create_text_translations.rb +5 -5
- data/lib/rails/generators/mobility/templates/initializer.rb +8 -0
- data/lib/rails/generators/mobility/templates/table_migration.rb +2 -3
- data/lib/rails/generators/mobility/templates/table_translations.rb +3 -4
- data/lib/rails/generators/mobility/translations_generator.rb +6 -5
- metadata +2 -3
- metadata.gz.sig +0 -0
- data/lib/mobility/backend/stringify_locale.rb +0 -18
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: aa18719235ac4c4f4dc7a50391d1aaa98e9963a0
|
4
|
+
data.tar.gz: 2aea9e420788a09176509fff75f1eb397656d6cd
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: d59832bfc7f2e3b0672b5f8d690ec167731e49225cd2d135f1325fd4ac934a22bf040c3ddaaed9a56cb7e6d9ac6b050f0fb7458f6f7f8e456a6391a9337ad6ec
|
7
|
+
data.tar.gz: 8a0dde22222134bad52a65e47a83e4cc6829d3a5273aef7be06ca25491c01bdc3e53bd609079c65d45c16328506b1f4a88fc9ab43612ac8272f3d4485fc073a7
|
checksums.yaml.gz.sig
CHANGED
@@ -1,2 +1,2 @@
|
|
1
|
-
|
2
|
-
|
1
|
+
�=^��&(�2x�P�f�^�n�_BE�i�ԟ;42?���ƴ�Ǿާ��Q���v����~l�YwW�w@��!
|
2
|
+
��ƕyi���ϟ�
|
data.tar.gz.sig
CHANGED
Binary file
|
data/CHANGELOG.md
CHANGED
@@ -1,6 +1,44 @@
|
|
1
1
|
# Mobility Changelog
|
2
2
|
|
3
|
-
## 0.
|
3
|
+
## 0.6
|
4
|
+
|
5
|
+
### 0.6.1 (April 26, 2018)
|
6
|
+
|
7
|
+
* Add column_prefix/column_suffix options to hash backends
|
8
|
+
([#200](https://github.com/shioyama/mobility/pull/199) and
|
9
|
+
[#201](https://github.com/shioyama/mobility/pull/201))
|
10
|
+
* Require specifying type for KeyValue backends
|
11
|
+
([#200](https://github.com/shioyama/mobility/pull/200))
|
12
|
+
* Remove table backend index on foreign key alone
|
13
|
+
([#198](https://github.com/shioyama/mobility/pull/198))
|
14
|
+
* Test/cleanup index name truncation in backend generators
|
15
|
+
([#197](https://github.com/shioyama/mobility/pull/197))
|
16
|
+
* Improve translations generators
|
17
|
+
([#196](https://github.com/shioyama/mobility/pull/196))
|
18
|
+
* Enforce null: false constraint on columns consistently
|
19
|
+
([#205](https://github.com/shioyama/mobility/pull/205))
|
20
|
+
* Add extension to find translations in a locale for Table backend
|
21
|
+
([#202](https://github.com/shioyama/mobility/pull/202))
|
22
|
+
* Ignore non-arel nodes in joins_values
|
23
|
+
([#206](https://github.com/shioyama/mobility/pull/206))
|
24
|
+
* Collapse duplicates in array-valued query hash
|
25
|
+
([#207](https://github.com/shioyama/mobility/pull/207))
|
26
|
+
* Remove unneeded anonymous module in backend resetters
|
27
|
+
([#213](https://github.com/shioyama/mobility/pull/213))
|
28
|
+
* Make constants private
|
29
|
+
([#214](https://github.com/shioyama/mobility/pull/214))
|
30
|
+
* Use IN when querying on array values with PG backends
|
31
|
+
([#209](https://github.com/shioyama/mobility/pull/209))
|
32
|
+
* Remove some deprecated methods
|
33
|
+
([#215](https://github.com/shioyama/mobility/pull/215))
|
34
|
+
* Explicitly implement matches/has_locale methods everywhere
|
35
|
+
([#194](https://github.com/shioyama/mobility/pull/194))
|
36
|
+
* Refactor Mobility::Backends::AR::QueryMethods using MobilityWhereChain module
|
37
|
+
([#193](https://github.com/shioyama/mobility/pull/193))
|
38
|
+
* Add missing documentation
|
39
|
+
([#192](https://github.com/shioyama/mobility/pull/192))
|
40
|
+
|
41
|
+
## 0.5
|
4
42
|
|
5
43
|
### 0.5.1 (March 21, 2018)
|
6
44
|
* Fix issues with Dirty plugin in ActiveRecord 5.2.0.rc2
|
@@ -17,6 +55,8 @@
|
|
17
55
|
* Use pragma comments to freeze strings everywhere
|
18
56
|
([#177](https://github.com/shioyama/mobility/pull/177))
|
19
57
|
|
58
|
+
## 0.4
|
59
|
+
|
20
60
|
### 0.4.3 (February 18, 2018)
|
21
61
|
* Add missing require in container backend
|
22
62
|
([#174](https://github.com/shioyama/mobility/pull/174))
|
data/Gemfile.lock
CHANGED
@@ -1,52 +1,21 @@
|
|
1
1
|
PATH
|
2
2
|
remote: .
|
3
3
|
specs:
|
4
|
-
mobility (0.
|
4
|
+
mobility (0.6.0)
|
5
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.rc2)
|
12
|
-
actionview (= 5.2.0.rc2)
|
13
|
-
activesupport (= 5.2.0.rc2)
|
14
|
-
rack (~> 2.0)
|
15
|
-
rack-test (>= 0.6.3)
|
16
|
-
rails-dom-testing (~> 2.0)
|
17
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.2)
|
18
|
-
actionview (5.2.0.rc2)
|
19
|
-
activesupport (= 5.2.0.rc2)
|
20
|
-
builder (~> 3.1)
|
21
|
-
erubi (~> 1.4)
|
22
|
-
rails-dom-testing (~> 2.0)
|
23
|
-
rails-html-sanitizer (~> 1.0, >= 1.0.3)
|
24
|
-
activemodel (5.2.0.rc2)
|
25
|
-
activesupport (= 5.2.0.rc2)
|
26
|
-
activerecord (5.2.0.rc2)
|
27
|
-
activemodel (= 5.2.0.rc2)
|
28
|
-
activesupport (= 5.2.0.rc2)
|
29
|
-
arel (>= 9.0)
|
30
|
-
activesupport (5.2.0.rc2)
|
31
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
32
|
-
i18n (>= 0.7, < 2)
|
33
|
-
minitest (~> 5.1)
|
34
|
-
tzinfo (~> 1.1)
|
35
|
-
arel (9.0.0)
|
36
11
|
benchmark-ips (2.7.2)
|
37
|
-
builder (3.2.3)
|
38
12
|
byebug (10.0.0)
|
39
13
|
coderay (1.1.2)
|
40
14
|
concurrent-ruby (1.0.5)
|
41
|
-
crass (1.0.3)
|
42
15
|
database_cleaner (1.6.2)
|
43
16
|
diff-lcs (1.3)
|
44
|
-
erubi (1.7.1)
|
45
17
|
ffi (1.9.23)
|
46
18
|
formatador (0.2.5)
|
47
|
-
generator_spec (0.9.4)
|
48
|
-
activesupport (>= 3.0.0)
|
49
|
-
railties (>= 3.0.0)
|
50
19
|
guard (2.14.2)
|
51
20
|
formatador (>= 0.2.4)
|
52
21
|
listen (>= 2.7, < 4.0)
|
@@ -67,17 +36,10 @@ GEM
|
|
67
36
|
rb-fsevent (~> 0.9, >= 0.9.4)
|
68
37
|
rb-inotify (~> 0.9, >= 0.9.7)
|
69
38
|
ruby_dep (~> 1.2)
|
70
|
-
loofah (2.2.1)
|
71
|
-
crass (~> 1.0.2)
|
72
|
-
nokogiri (>= 1.5.9)
|
73
39
|
lumberjack (1.0.12)
|
74
40
|
method_source (0.9.0)
|
75
|
-
mini_portile2 (2.3.0)
|
76
|
-
minitest (5.11.3)
|
77
41
|
mysql2 (0.4.10)
|
78
42
|
nenv (0.3.0)
|
79
|
-
nokogiri (1.8.2)
|
80
|
-
mini_portile2 (~> 2.3.0)
|
81
43
|
notiffany (0.1.1)
|
82
44
|
nenv (~> 0.1)
|
83
45
|
shellany (~> 0.0)
|
@@ -89,19 +51,6 @@ GEM
|
|
89
51
|
byebug (~> 10.0)
|
90
52
|
pry (~> 0.10)
|
91
53
|
rack (2.0.4)
|
92
|
-
rack-test (0.8.3)
|
93
|
-
rack (>= 1.0, < 3)
|
94
|
-
rails-dom-testing (2.0.3)
|
95
|
-
activesupport (>= 4.2.0)
|
96
|
-
nokogiri (>= 1.6)
|
97
|
-
rails-html-sanitizer (1.0.3)
|
98
|
-
loofah (~> 2.0)
|
99
|
-
railties (5.2.0.rc2)
|
100
|
-
actionpack (= 5.2.0.rc2)
|
101
|
-
activesupport (= 5.2.0.rc2)
|
102
|
-
method_source
|
103
|
-
rake (>= 0.8.7)
|
104
|
-
thor (>= 0.18.1, < 2.0)
|
105
54
|
rake (12.3.0)
|
106
55
|
rb-fsevent (0.10.3)
|
107
56
|
rb-inotify (0.9.10)
|
@@ -122,31 +71,27 @@ GEM
|
|
122
71
|
rspec-support (~> 3.7.0)
|
123
72
|
rspec-support (3.7.1)
|
124
73
|
ruby_dep (1.5.0)
|
74
|
+
sequel (5.7.1)
|
125
75
|
shellany (0.0.1)
|
126
76
|
sqlite3 (1.3.13)
|
127
77
|
thor (0.20.0)
|
128
|
-
thread_safe (0.3.6)
|
129
|
-
tzinfo (1.2.5)
|
130
|
-
thread_safe (~> 0.1)
|
131
78
|
yard (0.9.12)
|
132
79
|
|
133
80
|
PLATFORMS
|
134
81
|
ruby
|
135
82
|
|
136
83
|
DEPENDENCIES
|
137
|
-
activerecord (>= 5.2.0.rc2, < 5.3)
|
138
84
|
benchmark-ips
|
139
85
|
bundler (~> 1.12)
|
140
86
|
database_cleaner (~> 1.5, >= 1.5.3)
|
141
|
-
generator_spec (~> 0.9.4)
|
142
87
|
guard-rspec
|
143
88
|
mobility!
|
144
89
|
mysql2 (~> 0.4.9)
|
145
90
|
pg (< 1.0)
|
146
91
|
pry-byebug
|
147
|
-
railties (>= 5.2.0.rc2, < 5.3)
|
148
92
|
rake (~> 12, >= 12.2.1)
|
149
93
|
rspec (~> 3.0)
|
94
|
+
sequel (>= 5.0.0, < 6.0.0)
|
150
95
|
sqlite3
|
151
96
|
yard (~> 0.9.0)
|
152
97
|
|
data/README.md
CHANGED
@@ -54,7 +54,7 @@ Installation
|
|
54
54
|
Add this line to your application's Gemfile:
|
55
55
|
|
56
56
|
```ruby
|
57
|
-
gem 'mobility', '~> 0.
|
57
|
+
gem 'mobility', '~> 0.6.0'
|
58
58
|
```
|
59
59
|
|
60
60
|
Mobility is cryptographically signed. To be sure the gem you install hasn't
|
@@ -107,8 +107,12 @@ possibilities [below](#backends)).
|
|
107
107
|
|
108
108
|
You will likely also want to set default values for the various translation
|
109
109
|
options described below. You can set these defaults by assigning values to keys
|
110
|
-
on the `config.default_options` hash,
|
111
|
-
|
110
|
+
on the `config.default_options` hash. Below, we turn on the Dirty plugin by
|
111
|
+
default, so it will be enabled for all models.
|
112
|
+
|
113
|
+
You can also set defaults for backend-specific options. Below, we set the
|
114
|
+
default `type` option for the KeyValue backend to `:string` (this is
|
115
|
+
unnecessary and will be ignored if you are using a different backend).
|
112
116
|
|
113
117
|
```diff
|
114
118
|
Mobility.configure do |config|
|
@@ -116,11 +120,12 @@ the `dirty` option to `true`):
|
|
116
120
|
config.accessor_method = :translates
|
117
121
|
config.query_method = :i18n
|
118
122
|
+ config.default_options[:dirty] = true
|
123
|
+
+ config.default_options[:type] = :string
|
119
124
|
end
|
120
125
|
```
|
121
126
|
|
122
|
-
|
123
|
-
described in the [API
|
127
|
+
We will assume the configuration above in the examples that follow. Other
|
128
|
+
configuration options are described in the [API
|
124
129
|
docs](http://www.rubydoc.info/gems/mobility/Mobility/Configuration).
|
125
130
|
|
126
131
|
See [Getting Started](#quickstart) to get started translating your models.
|
@@ -136,8 +141,7 @@ You can extend `Mobility` just like in ActiveRecord, or you can use the
|
|
136
141
|
```ruby
|
137
142
|
class Word < ::Sequel::Model
|
138
143
|
plugin :mobility
|
139
|
-
translates :name,
|
140
|
-
translates :meaning, type: :text
|
144
|
+
translates :name, :meaning
|
141
145
|
end
|
142
146
|
```
|
143
147
|
|
@@ -161,15 +165,12 @@ of options, like this:
|
|
161
165
|
```ruby
|
162
166
|
class Word < ApplicationRecord
|
163
167
|
extend Mobility
|
164
|
-
translates :name,
|
165
|
-
translates :meaning, type: :text
|
168
|
+
translates :name, :meaning
|
166
169
|
end
|
167
170
|
```
|
168
171
|
|
169
|
-
You now have translated attributes `name`
|
170
|
-
|
171
|
-
would any other attribute:
|
172
|
-
|
172
|
+
You now have translated attributes `name` and `meaning` on the model `Word`.
|
173
|
+
You can set their values like you would any other attribute:
|
173
174
|
|
174
175
|
```ruby
|
175
176
|
word = Word.new
|
@@ -262,7 +263,7 @@ option when defining translated attributes on the model class:
|
|
262
263
|
```ruby
|
263
264
|
class Word < ApplicationRecord
|
264
265
|
extend Mobility
|
265
|
-
translates :name,
|
266
|
+
translates :name, locale_accessors: [:en, :ja]
|
266
267
|
end
|
267
268
|
```
|
268
269
|
|
@@ -302,7 +303,7 @@ For example, if we define `Word` this way:
|
|
302
303
|
```ruby
|
303
304
|
class Word < ApplicationRecord
|
304
305
|
extend Mobility
|
305
|
-
translates :name,
|
306
|
+
translates :name, fallthrough_accessors: true
|
306
307
|
end
|
307
308
|
```
|
308
309
|
|
@@ -368,7 +369,7 @@ reading and writing from the backend instance this way.
|
|
368
369
|
|
369
370
|
The `write` methods do not call underlying backend's methods to persist the change.
|
370
371
|
This is up to the user (e.g. with ActiveRecord you should call `save` write
|
371
|
-
the changes to the database.
|
372
|
+
the changes to the database).
|
372
373
|
|
373
374
|
### Setting the Locale
|
374
375
|
|
@@ -416,8 +417,8 @@ translated attributes on a class:
|
|
416
417
|
```ruby
|
417
418
|
class Word < ApplicationRecord
|
418
419
|
extend Mobility
|
419
|
-
translates :name,
|
420
|
-
translates :meaning,
|
420
|
+
translates :name, fallbacks: { de: :ja, fr: :ja }, locale_accessors: true
|
421
|
+
translates :meaning, fallbacks: { de: :ja, fr: :ja }, locale_accessors: true
|
421
422
|
end
|
422
423
|
```
|
423
424
|
|
@@ -511,7 +512,7 @@ Another option is to assign a default value, which will be used if the result of
|
|
511
512
|
```ruby
|
512
513
|
class Word < ApplicationRecord
|
513
514
|
extend Mobility
|
514
|
-
translates :name,
|
515
|
+
translates :name, default: 'foo'
|
515
516
|
end
|
516
517
|
|
517
518
|
Mobility.locale = :ja
|
@@ -559,7 +560,7 @@ dirty tracking is not specific to AR and works for non-persisted models as well)
|
|
559
560
|
```ruby
|
560
561
|
class Post < ApplicationRecord
|
561
562
|
extend Mobility
|
562
|
-
translates :title,
|
563
|
+
translates :title, dirty: true
|
563
564
|
end
|
564
565
|
```
|
565
566
|
|
@@ -644,7 +645,7 @@ false` when defining an attribute, like this:
|
|
644
645
|
```ruby
|
645
646
|
class Word < ApplicationRecord
|
646
647
|
extend Mobility
|
647
|
-
translates :name,
|
648
|
+
translates :name, cache: false
|
648
649
|
end
|
649
650
|
```
|
650
651
|
|
data/lib/mobility.rb
CHANGED
@@ -143,7 +143,7 @@ module Mobility
|
|
143
143
|
# @!group Configuration Methods
|
144
144
|
# @return [Mobility::Configuration] Mobility configuration
|
145
145
|
def config
|
146
|
-
@configuration ||=
|
146
|
+
@configuration ||= Configuration.new
|
147
147
|
end
|
148
148
|
|
149
149
|
# (see Mobility::Configuration#accessor_method)
|
@@ -271,7 +271,7 @@ instead.}
|
|
271
271
|
include Translates
|
272
272
|
|
273
273
|
def mobility
|
274
|
-
@mobility ||=
|
274
|
+
@mobility ||= Accumulator.new
|
275
275
|
end
|
276
276
|
|
277
277
|
def translated_attribute_names
|
@@ -283,6 +283,7 @@ instead.}
|
|
283
283
|
super
|
284
284
|
end
|
285
285
|
end
|
286
|
+
private_constant :ClassMethods, :InstanceMethods
|
286
287
|
|
287
288
|
class BackendRequired < ArgumentError; end
|
288
289
|
class InvalidLocale < I18n::InvalidLocale; end
|
data/lib/mobility/accumulator.rb
CHANGED
@@ -2,8 +2,7 @@ module Mobility
|
|
2
2
|
=begin
|
3
3
|
|
4
4
|
Class to access Mobility across backends. In particular, keeps a record of
|
5
|
-
which {Attributes} modules have been included on the model class.
|
6
|
-
simple delegator, so any missing method will be delegated to the model class.
|
5
|
+
which {Attributes} modules have been included on the model class.
|
7
6
|
|
8
7
|
=end
|
9
8
|
class Accumulator
|
@@ -9,7 +9,7 @@ Backend resetter for ActiveModel models. Adds hook to reset backend when
|
|
9
9
|
class BackendResetter < Mobility::BackendResetter
|
10
10
|
|
11
11
|
# (see Mobility::BackendResetter#initialize)
|
12
|
-
def initialize(attribute_names)
|
12
|
+
def initialize(attribute_names, &block)
|
13
13
|
super
|
14
14
|
|
15
15
|
model_reset_method = @model_reset_method
|
@@ -8,6 +8,17 @@ Module loading ActiveRecord-specific classes for Mobility models.
|
|
8
8
|
module ActiveRecord
|
9
9
|
require "mobility/active_record/uniqueness_validator"
|
10
10
|
|
11
|
+
def self.included(model_class)
|
12
|
+
model_class.class_eval do
|
13
|
+
extend QueryMethod.new(Mobility.query_method)
|
14
|
+
unless const_defined?(:UniquenessValidator)
|
15
|
+
const_set(:UniquenessValidator,
|
16
|
+
Class.new(::Mobility::ActiveRecord::UniquenessValidator))
|
17
|
+
end
|
18
|
+
delegate :translated_attribute_names, to: :class
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
11
22
|
class QueryMethod < Module
|
12
23
|
def initialize(query_method)
|
13
24
|
module_eval <<-EOM, __FILE__, __LINE__ + 1
|
@@ -17,14 +28,6 @@ Module loading ActiveRecord-specific classes for Mobility models.
|
|
17
28
|
EOM
|
18
29
|
end
|
19
30
|
end
|
20
|
-
|
21
|
-
def self.included(model_class)
|
22
|
-
model_class.extend QueryMethod.new(Mobility.query_method)
|
23
|
-
unless model_class.const_defined?(:UniquenessValidator)
|
24
|
-
model_class.const_set(:UniquenessValidator,
|
25
|
-
Class.new(::Mobility::ActiveRecord::UniquenessValidator))
|
26
|
-
end
|
27
|
-
model_class.delegate :translated_attribute_names, to: :class
|
28
|
-
end
|
31
|
+
private_constant :QueryMethod
|
29
32
|
end
|
30
33
|
end
|
@@ -11,16 +11,15 @@ Backend resetter for ActiveRecord models. Adds hook on +reload+ event to
|
|
11
11
|
=end
|
12
12
|
class BackendResetter < Mobility::ActiveModel::BackendResetter
|
13
13
|
|
14
|
-
#
|
15
|
-
def
|
14
|
+
# (see Mobility::BackendResetter#initialize)
|
15
|
+
def initialize(attribute_names, &block)
|
16
|
+
super
|
17
|
+
|
16
18
|
model_reset_method = @model_reset_method
|
17
19
|
|
18
|
-
|
19
|
-
|
20
|
-
super(*args).tap { instance_eval(&model_reset_method) }
|
21
|
-
end
|
20
|
+
define_method :reload do |*args|
|
21
|
+
super(*args).tap { instance_eval(&model_reset_method) }
|
22
22
|
end
|
23
|
-
model_class.include mod
|
24
23
|
end
|
25
24
|
end
|
26
25
|
end
|
@@ -3,10 +3,20 @@ module Mobility
|
|
3
3
|
=begin
|
4
4
|
|
5
5
|
A backend-agnostic uniqueness validator for ActiveRecord translated attributes.
|
6
|
+
To use the validator, you must +extend Mobility+ before calling +validates+
|
7
|
+
(see example below).
|
6
8
|
|
7
9
|
@note This validator does not support case sensitivity, since doing so would
|
8
10
|
significantly complicate implementation.
|
9
11
|
|
12
|
+
@example Validating uniqueness on translated model
|
13
|
+
class Post < ActiveRecord::Base
|
14
|
+
extend Mobility
|
15
|
+
translates :title
|
16
|
+
|
17
|
+
# This must come *after* extending Mobility.
|
18
|
+
validates :title, uniqueness: true
|
19
|
+
end
|
10
20
|
=end
|
11
21
|
class UniquenessValidator < ::ActiveRecord::Validations::UniquenessValidator
|
12
22
|
# @param [ActiveRecord::Base] record Translated model
|
@@ -15,7 +25,7 @@ A backend-agnostic uniqueness validator for ActiveRecord translated attributes.
|
|
15
25
|
def validate_each(record, attribute, value)
|
16
26
|
klass = record.class
|
17
27
|
|
18
|
-
if ((
|
28
|
+
if (([*options[:scope]] + [attribute]).map(&:to_s) & klass.translated_attribute_names).present?
|
19
29
|
warn %{
|
20
30
|
WARNING: The Mobility uniqueness validator for translated attributes does not
|
21
31
|
support case-insensitive validation. This option will be ignored for: #{attribute}
|
@@ -40,7 +50,7 @@ support case-insensitive validation. This option will be ignored for: #{attribut
|
|
40
50
|
private
|
41
51
|
|
42
52
|
def mobility_scope_relation(record, relation)
|
43
|
-
|
53
|
+
[*options[:scope]].inject(relation) do |scoped_relation, scope_item|
|
44
54
|
scoped_relation.where(scope_item => record.send(scope_item))
|
45
55
|
end
|
46
56
|
end
|