mobility 0.1.20 → 0.2.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- checksums.yaml.gz.sig +0 -0
- data.tar.gz.sig +2 -0
- data/CHANGELOG.md +17 -0
- data/CONTRIBUTING.md +55 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +2 -56
- data/README.md +64 -15
- data/Rakefile +17 -17
- data/lib/mobility.rb +43 -40
- data/lib/mobility/active_model.rb +0 -1
- data/lib/mobility/active_model/backend_resetter.rb +1 -1
- data/lib/mobility/active_record.rb +8 -15
- data/lib/mobility/active_record/backend_resetter.rb +2 -0
- data/lib/mobility/active_record/model_translation.rb +1 -1
- data/lib/mobility/active_record/string_translation.rb +2 -0
- data/lib/mobility/active_record/text_translation.rb +2 -0
- data/lib/mobility/attributes.rb +74 -71
- data/lib/mobility/backend.rb +64 -25
- data/lib/mobility/backend/orm_delegator.rb +17 -7
- data/lib/mobility/backend/stringify_locale.rb +18 -0
- data/lib/mobility/backend_resetter.rb +8 -5
- data/lib/mobility/backends.rb +4 -0
- data/lib/mobility/backends/active_record.rb +20 -0
- data/lib/mobility/{backend → backends}/active_record/column.rb +25 -13
- data/lib/mobility/{backend → backends}/active_record/column/query_methods.rb +5 -3
- data/lib/mobility/backends/active_record/hstore.rb +29 -0
- data/lib/mobility/{backend → backends}/active_record/hstore/query_methods.rb +2 -2
- data/lib/mobility/{backend → backends}/active_record/jsonb.rb +6 -6
- data/lib/mobility/{backend → backends}/active_record/jsonb/query_methods.rb +4 -2
- data/lib/mobility/{backend → backends}/active_record/key_value.rb +10 -44
- data/lib/mobility/{backend → backends}/active_record/key_value/query_methods.rb +4 -2
- data/lib/mobility/{backend/active_record/hash_valued.rb → backends/active_record/pg_hash.rb} +13 -21
- data/lib/mobility/{backend → backends}/active_record/query_methods.rb +2 -2
- data/lib/mobility/{backend → backends}/active_record/serialized.rb +12 -28
- data/lib/mobility/{backend → backends}/active_record/serialized/query_methods.rb +5 -8
- data/lib/mobility/{backend → backends}/active_record/table.rb +11 -60
- data/lib/mobility/{backend → backends}/active_record/table/query_methods.rb +5 -3
- data/lib/mobility/{backend → backends}/column.rb +8 -4
- data/lib/mobility/backends/hash_valued.rb +29 -0
- data/lib/mobility/{backend → backends}/hstore.rb +4 -4
- data/lib/mobility/{backend → backends}/jsonb.rb +4 -4
- data/lib/mobility/backends/key_value.rb +111 -0
- data/lib/mobility/{backend → backends}/null.rb +4 -4
- data/lib/mobility/backends/sequel.rb +20 -0
- data/lib/mobility/backends/sequel/column.rb +52 -0
- data/lib/mobility/{backend → backends}/sequel/column/query_methods.rb +5 -3
- data/lib/mobility/backends/sequel/hstore.rb +29 -0
- data/lib/mobility/{backend → backends}/sequel/hstore/query_methods.rb +4 -3
- data/lib/mobility/{backend → backends}/sequel/jsonb.rb +6 -6
- data/lib/mobility/{backend → backends}/sequel/jsonb/query_methods.rb +4 -3
- data/lib/mobility/{backend → backends}/sequel/key_value.rb +28 -39
- data/lib/mobility/{backend → backends}/sequel/key_value/query_methods.rb +4 -5
- data/lib/mobility/backends/sequel/pg_hash.rb +46 -0
- data/lib/mobility/{backend → backends}/sequel/postgres_query_methods.rb +1 -2
- data/lib/mobility/{backend → backends}/sequel/query_methods.rb +6 -4
- data/lib/mobility/{backend → backends}/sequel/serialized.rb +17 -38
- data/lib/mobility/backends/sequel/serialized/query_methods.rb +17 -0
- data/lib/mobility/{backend → backends}/sequel/table.rb +29 -60
- data/lib/mobility/{backend → backends}/sequel/table/query_methods.rb +5 -3
- data/lib/mobility/{backend → backends}/serialized.rb +27 -5
- data/lib/mobility/{backend → backends}/table.rb +69 -29
- data/lib/mobility/configuration.rb +40 -0
- data/lib/mobility/{orm.rb → loaded.rb} +0 -0
- data/lib/mobility/plugins.rb +35 -0
- data/lib/mobility/plugins/active_model.rb +6 -0
- data/lib/mobility/plugins/active_model/dirty.rb +81 -0
- data/lib/mobility/plugins/active_record.rb +6 -0
- data/lib/mobility/plugins/active_record/dirty.rb +59 -0
- data/lib/mobility/plugins/cache.rb +54 -0
- data/lib/mobility/plugins/cache/translation_cacher.rb +40 -0
- data/lib/mobility/plugins/default.rb +73 -0
- data/lib/mobility/plugins/dirty.rb +61 -0
- data/lib/mobility/{backend → plugins}/fallbacks.rb +36 -31
- data/lib/mobility/plugins/fallthrough_accessors.rb +66 -0
- data/lib/mobility/plugins/locale_accessors.rb +84 -0
- data/lib/mobility/{backend → plugins}/presence.rb +15 -6
- data/lib/mobility/plugins/sequel.rb +6 -0
- data/lib/mobility/plugins/sequel/dirty.rb +59 -0
- data/lib/mobility/sequel.rb +5 -14
- data/lib/mobility/sequel/backend_resetter.rb +4 -6
- data/lib/mobility/sequel/column_changes.rb +4 -4
- data/lib/mobility/sequel/model_translation.rb +1 -1
- data/lib/mobility/sequel/string_translation.rb +2 -0
- data/lib/mobility/sequel/text_translation.rb +2 -0
- data/lib/mobility/translates.rb +1 -5
- data/lib/mobility/util.rb +126 -0
- data/lib/mobility/version.rb +1 -1
- data/lib/mobility/wrapper.rb +1 -1
- data/lib/rails/generators/mobility/translations_generator.rb +7 -3
- metadata +85 -55
- metadata.gz.sig +0 -0
- data/lib/mobility/backend/active_model.rb +0 -7
- data/lib/mobility/backend/active_model/dirty.rb +0 -95
- data/lib/mobility/backend/active_record.rb +0 -29
- data/lib/mobility/backend/active_record/dirty.rb +0 -54
- data/lib/mobility/backend/active_record/hstore.rb +0 -29
- data/lib/mobility/backend/cache.rb +0 -117
- data/lib/mobility/backend/dirty.rb +0 -38
- data/lib/mobility/backend/key_value.rb +0 -85
- data/lib/mobility/backend/sequel.rb +0 -29
- data/lib/mobility/backend/sequel/column.rb +0 -39
- data/lib/mobility/backend/sequel/dirty.rb +0 -57
- data/lib/mobility/backend/sequel/hash_valued.rb +0 -51
- data/lib/mobility/backend/sequel/hstore.rb +0 -29
- data/lib/mobility/backend/sequel/serialized/query_methods.rb +0 -20
- data/lib/mobility/core_ext/object.rb +0 -30
- data/lib/mobility/core_ext/string.rb +0 -16
- data/lib/mobility/fallthrough_accessors.rb +0 -57
- data/lib/mobility/locale_accessors.rb +0 -55
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 65312461ae32cbb2024100f6a3676ac28949a35b
|
4
|
+
data.tar.gz: 7cebc1fb0666104ab1514beb5966ee711057a178
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6a9962e0d3ab3fede8e8de43604435bdbfe2362a56dfaeada04a18cdf1c018060ca3e2f95e96f86eb7fc4cf4e22ed8ff5b7be713f72fe69ad4721619d248cb65
|
7
|
+
data.tar.gz: 294e4a19db85fd3b7645455167f974d6995334a12582a07a3501bbecfb7e6129a0832bb7368036d49357c807c3c798976bcbab2ce4e4d8f88ac9c2109d60e9f1
|
checksums.yaml.gz.sig
ADDED
Binary file
|
data.tar.gz.sig
ADDED
data/CHANGELOG.md
CHANGED
@@ -2,6 +2,23 @@
|
|
2
2
|
|
3
3
|
## 0.1
|
4
4
|
|
5
|
+
### 0.2.0 (August 13, 2017)
|
6
|
+
|
7
|
+
See overview of the changes in [this blog
|
8
|
+
post](http://dejimata.com/2017/8/13/mobility-0-2-now-with-plugins).
|
9
|
+
|
10
|
+
* Mobility.default_options ([#50](https://github.com/shioyama/mobility/pull/50))
|
11
|
+
* Re-organized options under Plugins namespace ([#62](https://github.com/shioyama/mobility/pull/64))
|
12
|
+
* Backends are now Enumerable ([#71](https://github.com/shioyama/mobility/pull/71))
|
13
|
+
* Replace `autoload` by `require` ([#65](https://github.com/shioyama/mobility/pull/65))
|
14
|
+
* Remove mobility/core_ext and replace with `Mobility::Util` ([#60](https://github.com/shioyama/mobility/pull/60))
|
15
|
+
* New "default" plugin which sets a default value or proc for an attribute: ([#49](https://github.com/shioyama/mobility/pull/49))
|
16
|
+
* Add `super` option ([#62](https://github.com/shioyama/mobility/pull/62))
|
17
|
+
* Rename default associations for KeyValue and Table backends ([#59](https://github.com/shioyama/mobility/pull/59) and [#66](https://github.com/shioyama/mobility/pull/66))
|
18
|
+
* Refactor cache code ([#57](https://github.com/shioyama/mobility/pull/58))
|
19
|
+
* Gem is now signed ([#73](https://github.com/shioyama/mobility/pull/73))
|
20
|
+
* Minimum Ruby version: 2.2.7
|
21
|
+
|
5
22
|
### 0.1.20 (July 23, 2017)
|
6
23
|
* Fix location of Rails generators to work with plugins
|
7
24
|
([#56](https://github.com/shioyama/mobility/pull/56))
|
data/CONTRIBUTING.md
ADDED
@@ -0,0 +1,55 @@
|
|
1
|
+
# Contributing to Mobility
|
2
|
+
|
3
|
+
Thanks for your interest in contributing to Mobility! Contributions are welcomed and encouraged. Bug reports, feature requests, and refactoring are all a great help, but please follow the instructions below to ensure things go as smoothly as possible.
|
4
|
+
|
5
|
+
## Bugs
|
6
|
+
|
7
|
+
Notice a bug or something that seems not to be working correctly? Great, that's valuable information. First off, make sure you go through the [Github issues](https://github.com/shioyama/mobility/issues?utf8=%E2%9C%93&q=is%3Aissue) to see if what you're experiencing has already been reported.
|
8
|
+
|
9
|
+
If not, please post a new issue explaining how the issue happens, and steps to reproduce it. Also include what backend you are using, what ORM (ActiveRecord, Sequel, etc.), what Ruby version, and if relevant what platform, etc.
|
10
|
+
|
11
|
+
## Feature Requests
|
12
|
+
|
13
|
+
Have an idea for a new feature? Great! Please sketch out what you are thinking of and create an issue describing it in as much detail as possible. Note that Mobility aims to be as simple as possible, so complex features will probably not be added, but extensions and integrations with other gems may be created outside of the Mobility gem itself.
|
14
|
+
|
15
|
+
## Questions
|
16
|
+
|
17
|
+
If you are having issues understanding how to apply Mobility to your particular use case, or any other questions about the gem, please post a question to [Stack Overflow](http://stackoverflow.com) tagged with "mobility". If you don't get an answer, post an issue to the repository with a link to the question and someone will try to help you asap.
|
18
|
+
|
19
|
+
## Features
|
20
|
+
|
21
|
+
If you've actually built a new feature for Mobility, go ahead and make a pull request and we will consider it. In general you will need to have tests for whatever feature you are proposing.
|
22
|
+
|
23
|
+
To test that your feature does not break existing specs, run the specs with:
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
bundle exec rspec
|
27
|
+
```
|
28
|
+
|
29
|
+
This will run specs which are not dependent on any ORM (pure Ruby specs only). To test against ActiveRecord, you will need to set the `ORM` environment variable, like this:
|
30
|
+
|
31
|
+
```ruby
|
32
|
+
ORM=active_record bundle exec rspec
|
33
|
+
```
|
34
|
+
|
35
|
+
This will run AR specs with an sqlite3 in-memory database. If you want to run specs against a specific database, you will need to specify which database to use with the `DB` env (either `mysql` or `postgres`), and first create and migrate the database:
|
36
|
+
|
37
|
+
```ruby
|
38
|
+
ORM=active_record DB=postgres bundle exec rspec
|
39
|
+
```
|
40
|
+
|
41
|
+
... will run the specs against Mobility running with AR 5.1 with postgres as the database.
|
42
|
+
|
43
|
+
For more info, see the [Testing Backends](https://github.com/shioyama/mobility#testing-backends) section of the README.
|
44
|
+
|
45
|
+
Once you've ensured that existing specs do not break, please try to write at least one spec covering the new feature. If you have questions about how to do this, first post the PR and we can help you in the PR comments section.
|
46
|
+
|
47
|
+
Note that when you submit the pull request, Travis CI will run the [test suite](https://travis-ci.org/mobility/mobility) against your branch and will highlight any failures. Unless there is a good reason for it, we do not generally accept pull requests that take Mobility from green to red.
|
48
|
+
|
49
|
+
## Other Resources
|
50
|
+
|
51
|
+
Be sure to check out these resources for more detailed info on how Mobility works:
|
52
|
+
|
53
|
+
- [API docs](http://www.rubydoc.info/gems/mobility)
|
54
|
+
- [Wiki](https://github.com/shioyama/mobility/wiki)
|
55
|
+
- [Translating with Mobility](http://dejimata.com/2017/3/3/translating-with-mobility)
|
data/Gemfile
CHANGED
data/Gemfile.lock
CHANGED
@@ -8,43 +8,12 @@ PATH
|
|
8
8
|
GEM
|
9
9
|
remote: https://rubygems.org/
|
10
10
|
specs:
|
11
|
-
actionpack (5.1.2)
|
12
|
-
actionview (= 5.1.2)
|
13
|
-
activesupport (= 5.1.2)
|
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.1.2)
|
19
|
-
activesupport (= 5.1.2)
|
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.1.2)
|
25
|
-
activesupport (= 5.1.2)
|
26
|
-
activerecord (5.1.2)
|
27
|
-
activemodel (= 5.1.2)
|
28
|
-
activesupport (= 5.1.2)
|
29
|
-
arel (~> 8.0)
|
30
|
-
activesupport (5.1.2)
|
31
|
-
concurrent-ruby (~> 1.0, >= 1.0.2)
|
32
|
-
i18n (~> 0.7)
|
33
|
-
minitest (~> 5.1)
|
34
|
-
tzinfo (~> 1.1)
|
35
|
-
arel (8.0.0)
|
36
|
-
builder (3.2.3)
|
37
11
|
byebug (9.0.6)
|
38
12
|
coderay (1.1.1)
|
39
|
-
concurrent-ruby (1.0.5)
|
40
13
|
database_cleaner (1.6.1)
|
41
14
|
diff-lcs (1.3)
|
42
|
-
erubi (1.6.1)
|
43
15
|
ffi (1.9.18)
|
44
16
|
formatador (0.2.5)
|
45
|
-
generator_spec (0.9.4)
|
46
|
-
activesupport (>= 3.0.0)
|
47
|
-
railties (>= 3.0.0)
|
48
17
|
guard (2.14.1)
|
49
18
|
formatador (>= 0.2.4)
|
50
19
|
listen (>= 2.7, < 4.0)
|
@@ -64,16 +33,10 @@ GEM
|
|
64
33
|
rb-fsevent (~> 0.9, >= 0.9.4)
|
65
34
|
rb-inotify (~> 0.9, >= 0.9.7)
|
66
35
|
ruby_dep (~> 1.2)
|
67
|
-
loofah (2.0.3)
|
68
|
-
nokogiri (>= 1.5.9)
|
69
36
|
lumberjack (1.0.12)
|
70
37
|
method_source (0.8.2)
|
71
|
-
mini_portile2 (2.2.0)
|
72
|
-
minitest (5.10.3)
|
73
38
|
mysql2 (0.3.21)
|
74
39
|
nenv (0.3.0)
|
75
|
-
nokogiri (1.8.0)
|
76
|
-
mini_portile2 (~> 2.2.0)
|
77
40
|
notiffany (0.1.1)
|
78
41
|
nenv (~> 0.1)
|
79
42
|
shellany (~> 0.0)
|
@@ -85,20 +48,6 @@ GEM
|
|
85
48
|
pry-byebug (3.4.2)
|
86
49
|
byebug (~> 9.0)
|
87
50
|
pry (~> 0.10)
|
88
|
-
rack (2.0.3)
|
89
|
-
rack-test (0.6.3)
|
90
|
-
rack (>= 1.0)
|
91
|
-
rails-dom-testing (2.0.3)
|
92
|
-
activesupport (>= 4.2.0)
|
93
|
-
nokogiri (>= 1.6)
|
94
|
-
rails-html-sanitizer (1.0.3)
|
95
|
-
loofah (~> 2.0)
|
96
|
-
railties (5.1.2)
|
97
|
-
actionpack (= 5.1.2)
|
98
|
-
activesupport (= 5.1.2)
|
99
|
-
method_source
|
100
|
-
rake (>= 0.8.7)
|
101
|
-
thor (>= 0.18.1, < 2.0)
|
102
51
|
rake (10.5.0)
|
103
52
|
rb-fsevent (0.10.2)
|
104
53
|
rb-inotify (0.9.10)
|
@@ -118,23 +67,19 @@ GEM
|
|
118
67
|
rspec-support (~> 3.6.0)
|
119
68
|
rspec-support (3.6.0)
|
120
69
|
ruby_dep (1.5.0)
|
70
|
+
sequel (4.48.0)
|
121
71
|
shellany (0.0.1)
|
122
72
|
slop (3.6.0)
|
123
73
|
sqlite3 (1.3.13)
|
124
74
|
thor (0.19.4)
|
125
|
-
thread_safe (0.3.6)
|
126
|
-
tzinfo (1.2.3)
|
127
|
-
thread_safe (~> 0.1)
|
128
75
|
yard (0.9.9)
|
129
76
|
|
130
77
|
PLATFORMS
|
131
78
|
ruby
|
132
79
|
|
133
80
|
DEPENDENCIES
|
134
|
-
activerecord (>= 5.1, < 5.2)
|
135
81
|
bundler (~> 1.12)
|
136
82
|
database_cleaner (~> 1.5, >= 1.5.3)
|
137
|
-
generator_spec (~> 0.9.4)
|
138
83
|
guard-rspec
|
139
84
|
mobility!
|
140
85
|
mysql2 (~> 0.3.10)
|
@@ -142,6 +87,7 @@ DEPENDENCIES
|
|
142
87
|
pry-byebug
|
143
88
|
rake (~> 10.0)
|
144
89
|
rspec (~> 3.0)
|
90
|
+
sequel (>= 4.46.0)
|
145
91
|
sqlite3
|
146
92
|
yard (~> 0.9.0)
|
147
93
|
|
data/README.md
CHANGED
@@ -47,11 +47,20 @@ Installation
|
|
47
47
|
Add this line to your application's Gemfile:
|
48
48
|
|
49
49
|
```ruby
|
50
|
-
gem 'mobility', '~> 0.
|
50
|
+
gem 'mobility', '~> 0.2.0'
|
51
51
|
```
|
52
52
|
|
53
|
-
|
54
|
-
|
53
|
+
Mobility is cryptographically signed. To be sure the gem you install hasn't
|
54
|
+
been tampered with, first my public key (if you haven’t already) as a trusted
|
55
|
+
certificate:
|
56
|
+
|
57
|
+
```
|
58
|
+
gem cert --add <(curl -Ls https://raw.github.com/shioyama/mobility/master/certs/shioyama.pem)
|
59
|
+
gem install shioyama -P MediumSecurity
|
60
|
+
```
|
61
|
+
|
62
|
+
The MediumSecurity trust profile will verify signed gems, but allow the
|
63
|
+
installation of unsigned dependencies.
|
55
64
|
|
56
65
|
### ActiveRecord (Rails)
|
57
66
|
|
@@ -62,6 +71,9 @@ Requirements:
|
|
62
71
|
ActiveRecord/Rails 4.2, but there are some tests still failing. To see exactly
|
63
72
|
what might not work, check pending specs in Rails 4.2 Travis builds.)
|
64
73
|
|
74
|
+
To translate attributes on a model, extend `Mobility`, then call `translates`
|
75
|
+
passing in one or more attributes as well as a hash of options (see below).
|
76
|
+
|
65
77
|
If using Mobility in a Rails project, you can run the generator to create an
|
66
78
|
initializer and a migration to create shared translation tables for the
|
67
79
|
default `KeyValue` backend:
|
@@ -95,7 +107,7 @@ See [Getting Started](#quickstart) to get started translating your models.
|
|
95
107
|
Requirements:
|
96
108
|
- Sequel >= 4.0
|
97
109
|
|
98
|
-
You can
|
110
|
+
You can extend `Mobility` just like in ActiveRecord, or you can use the
|
99
111
|
`mobility` plugin, which does the same thing:
|
100
112
|
|
101
113
|
```ruby
|
@@ -125,7 +137,7 @@ of options, like this:
|
|
125
137
|
|
126
138
|
```ruby
|
127
139
|
class Word < ApplicationRecord
|
128
|
-
|
140
|
+
extend Mobility
|
129
141
|
translates :name, type: :string
|
130
142
|
translates :meaning, type: :text
|
131
143
|
end
|
@@ -226,7 +238,7 @@ option when defining translated attributes on the model class:
|
|
226
238
|
|
227
239
|
```ruby
|
228
240
|
class Word < ApplicationRecord
|
229
|
-
|
241
|
+
extend Mobility
|
230
242
|
translates :name, type: :string, locale_accessors: [:en, :ja]
|
231
243
|
end
|
232
244
|
```
|
@@ -266,7 +278,7 @@ For example, if we define `Word` this way:
|
|
266
278
|
|
267
279
|
```ruby
|
268
280
|
class Word < ApplicationRecord
|
269
|
-
|
281
|
+
extend Mobility
|
270
282
|
translates :name, type: :string, fallthrough_accessors: true
|
271
283
|
end
|
272
284
|
```
|
@@ -326,6 +338,10 @@ word.name_backend.read(:en)
|
|
326
338
|
Internally, all methods for accessing translated attributes ultimately end up
|
327
339
|
reading and writing from the backend instance this way.
|
328
340
|
|
341
|
+
The `write` methods do not call underlying backend's methods to persist the change.
|
342
|
+
This is up to the user (e.g. with ActiveRecord you should call `save` write
|
343
|
+
the changes to the database.
|
344
|
+
|
329
345
|
### Setting the Locale
|
330
346
|
|
331
347
|
It may not always be desirable to use `I18n.locale` to set the locale for
|
@@ -371,7 +387,7 @@ translated attributes on a class:
|
|
371
387
|
|
372
388
|
```ruby
|
373
389
|
class Word < ApplicationRecord
|
374
|
-
|
390
|
+
extend Mobility
|
375
391
|
translates :name, type: :string, fallbacks: { de: :ja, fr: :ja }
|
376
392
|
translates :meaning, type: :text, fallbacks: { de: :ja, fr: :ja }
|
377
393
|
end
|
@@ -437,6 +453,39 @@ fallbacks](http://www.rubydoc.info/gems/mobility/Mobility/Backend/Fallbacks)
|
|
437
453
|
and [this article on I18n
|
438
454
|
fallbacks](https://github.com/svenfuchs/i18n/wiki/Fallbacks).
|
439
455
|
|
456
|
+
### <a name="default"></a>Default values
|
457
|
+
|
458
|
+
Another option is to assign a default value, which will be used if the result of a fetch would otherwise be `nil`:
|
459
|
+
|
460
|
+
```ruby
|
461
|
+
class Word < ApplicationRecord
|
462
|
+
extend Mobility
|
463
|
+
translates :name, type: :string, default: 'foo'
|
464
|
+
end
|
465
|
+
|
466
|
+
Mobility.locale = :ja
|
467
|
+
word = Word.create(name: "モビリティ")
|
468
|
+
word.name
|
469
|
+
#=> "モビリティ"
|
470
|
+
Mobility.locale = :de
|
471
|
+
word.name
|
472
|
+
#=> "foo"
|
473
|
+
```
|
474
|
+
|
475
|
+
You can override the default by passing a `default` option to the attribute reader:
|
476
|
+
|
477
|
+
```ruby
|
478
|
+
word.name
|
479
|
+
#=> 'foo'
|
480
|
+
word.name(default: nil)
|
481
|
+
#=> nil
|
482
|
+
word.name(default: 'bar')
|
483
|
+
#=> 'bar'
|
484
|
+
```
|
485
|
+
|
486
|
+
The default can also be a `Proc`, which will be passed the model and attribute
|
487
|
+
name as keyword arguments. See the [API docs][docs] for details.
|
488
|
+
|
440
489
|
### <a name="dirty"></a>Dirty Tracking
|
441
490
|
|
442
491
|
Dirty tracking (tracking of changed attributes) can be enabled for models which
|
@@ -456,7 +505,7 @@ dirty tracking is not specific to AR and works for non-persisted models as well)
|
|
456
505
|
|
457
506
|
```ruby
|
458
507
|
class Post < ApplicationRecord
|
459
|
-
|
508
|
+
extend Mobility
|
460
509
|
translates :title, type: :string, dirty: true
|
461
510
|
end
|
462
511
|
```
|
@@ -530,7 +579,7 @@ false` when defining an attribute, like this:
|
|
530
579
|
|
531
580
|
```ruby
|
532
581
|
class Word < ApplicationRecord
|
533
|
-
|
582
|
+
extend Mobility
|
534
583
|
translates :name, type: :string, cache: false
|
535
584
|
end
|
536
585
|
```
|
@@ -554,7 +603,7 @@ So assuming a model:
|
|
554
603
|
|
555
604
|
```ruby
|
556
605
|
class Post < ApplicationRecord
|
557
|
-
|
606
|
+
extend Mobility
|
558
607
|
translates :title, type: :string
|
559
608
|
translates :content, type: :text
|
560
609
|
end
|
@@ -598,7 +647,7 @@ default scope on your model:
|
|
598
647
|
|
599
648
|
```ruby
|
600
649
|
class Post < ApplicationRecord
|
601
|
-
|
650
|
+
extend Mobility
|
602
651
|
translates :title, type: :string
|
603
652
|
translates :content, type: :text
|
604
653
|
default_scope { i18n }
|
@@ -633,7 +682,7 @@ This would set the `name` attribute to use the `Table` backend (see below).
|
|
633
682
|
The `type` option (`type: :string` or `type: :text`) is missing here because
|
634
683
|
this is an option specific to the KeyValue backend (specifying which shared
|
635
684
|
table to store translations on). Backends have their own specific options; see
|
636
|
-
the [Wiki][wiki] and [API documentation][
|
685
|
+
the [Wiki][wiki] and [API documentation][docs] for which options are available
|
637
686
|
for each.
|
638
687
|
|
639
688
|
Everything else described above (fallbacks, dirty tracking, locale accessors,
|
@@ -712,7 +761,7 @@ class MyBackend
|
|
712
761
|
end
|
713
762
|
|
714
763
|
class MyClass
|
715
|
-
|
764
|
+
extend Mobility
|
716
765
|
translates :foo, backend: MyBackend
|
717
766
|
end
|
718
767
|
```
|
@@ -745,7 +794,7 @@ describe MyBackend do
|
|
745
794
|
|
746
795
|
before do
|
747
796
|
stub_const 'MyPost', Class.new(ActiveRecord::Base)
|
748
|
-
MyPost.
|
797
|
+
MyPost.extend Mobility
|
749
798
|
MyPost.translates :title, :content, backend: MyBackend
|
750
799
|
end
|
751
800
|
|
data/Rakefile
CHANGED
@@ -6,49 +6,49 @@ RSpec::Core::RakeTask.new(:spec)
|
|
6
6
|
|
7
7
|
task :default => :spec
|
8
8
|
|
9
|
-
task :
|
9
|
+
task :setup do
|
10
10
|
%w(lib spec).each do |path|
|
11
11
|
$LOAD_PATH.unshift(File.expand_path("../#{path}", __FILE__))
|
12
12
|
end
|
13
|
+
require "database"
|
14
|
+
exit if config["database"] == ":memory:"
|
13
15
|
end
|
14
16
|
|
15
17
|
namespace :db do
|
16
18
|
desc "Create the database"
|
17
|
-
task :
|
18
|
-
require "database"
|
19
|
-
driver = Mobility::Test::Database.driver
|
20
|
-
config = Mobility::Test::Database.config[driver]
|
21
|
-
exit if config["database"] == ":memory:"
|
19
|
+
task create: :setup do
|
22
20
|
commands = {
|
23
|
-
"mysql" => "mysql -u #{config['username']} -e 'create database #{config["database"]};' >/dev/null",
|
21
|
+
"mysql" => "mysql -u #{config['username']} -e 'create database #{config["database"]} default character set #{config["encoding"]} default collate #{config["collation"]};' >/dev/null",
|
24
22
|
"postgres" => "psql -c 'create database #{config['database']};' -U #{config['username']} >/dev/null"
|
25
23
|
}
|
26
24
|
%x{#{commands[driver] || true}}
|
25
|
+
$?.success? ? puts("Database successfully created.") : puts("There was an error creating the database.")
|
27
26
|
end
|
28
27
|
|
29
28
|
desc "Drop the database"
|
30
|
-
task :
|
31
|
-
require "database"
|
32
|
-
driver = Mobility::Test::Database.driver
|
33
|
-
config = Mobility::Test::Database.config[driver]
|
34
|
-
exit if config["database"] == ":memory:"
|
29
|
+
task drop: :setup do
|
35
30
|
commands = {
|
36
31
|
"mysql" => "mysql -u #{config['username']} -e 'drop database #{config["database"]};' >/dev/null",
|
37
32
|
"postgres" => "psql -c 'drop database #{config['database']};' -U #{config['username']} >/dev/null"
|
38
33
|
}
|
39
34
|
%x{#{commands[driver] || true}}
|
35
|
+
$?.success? ? puts("Database successfully dropped.") : puts("There was an error dropping the database.")
|
40
36
|
end
|
41
37
|
|
42
38
|
desc "Set up the database schema"
|
43
|
-
task :
|
44
|
-
require "database"
|
45
|
-
driver = Mobility::Test::Database.driver
|
46
|
-
config = Mobility::Test::Database.config[driver]
|
47
|
-
exit if config["database"] == ":memory:"
|
39
|
+
task up: :setup do
|
48
40
|
require "spec_helper"
|
49
41
|
Mobility::Test::Schema.up
|
50
42
|
end
|
51
43
|
|
52
44
|
desc "Drop and recreate the database schema"
|
53
45
|
task :reset => [:drop, :create]
|
46
|
+
|
47
|
+
def config
|
48
|
+
Mobility::Test::Database.config[driver]
|
49
|
+
end
|
50
|
+
|
51
|
+
def driver
|
52
|
+
Mobility::Test::Database.driver
|
53
|
+
end
|
54
54
|
end
|