localizable_model 0.0.1 → 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.
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: f922dccacb3ed524ee9c233afff9e824b5b08533
|
4
|
+
data.tar.gz: b1dc4c6662be10b0037206349e0ae0e116a31ee2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 6e5c1418be91c5a6bdecc7aaaae5a8e17f645777cce32ae1b8a5faf58b10511dc2f18d3223850540716de6a881c34d753f8393e013a75e4f4ae5cda314aae4a7
|
7
|
+
data.tar.gz: 8b813cb237e9a622802e9576639303d87b1d72c00b3326b25e84d2108d8f8bd615c60820daaf81c18ebbdbcb2babe3b6337a1c76c6ce2a9134cd3fe63532525c
|
data/README.md
CHANGED
@@ -1,8 +1,10 @@
|
|
1
1
|
# LocalizableModel
|
2
2
|
|
3
|
+
LocalizableModel allows any ActiveRecord model to have localized attributes.
|
4
|
+
|
3
5
|
[](https://travis-ci.org/kord-as/localizable_model) [](https://codeclimate.com/github/kord-as/localizable_model) [](https://codeclimate.com/github/kord-as/localizable_model) [](https://gemnasium.com/kord-as/localizable_model)
|
4
6
|
|
5
|
-
##
|
7
|
+
## Installation
|
6
8
|
|
7
9
|
Add the `localizable_model` gem to your Gemfile:
|
8
10
|
|
@@ -16,6 +18,64 @@ Generate the migration:
|
|
16
18
|
bin/rails g localizable_model:migration
|
17
19
|
```
|
18
20
|
|
21
|
+
## Usage
|
22
|
+
|
23
|
+
You can now define localizable attributes on your model:
|
24
|
+
|
25
|
+
``` ruby
|
26
|
+
class Page < ActiveRecord::Base
|
27
|
+
localizable do
|
28
|
+
attribute :name
|
29
|
+
attribute :body
|
30
|
+
end
|
31
|
+
end
|
32
|
+
```
|
33
|
+
|
34
|
+
You can also use a dictionary. This will let you define attributes dynamically
|
35
|
+
at runtime.
|
36
|
+
|
37
|
+
``` ruby
|
38
|
+
class Page < ActiveRecord::Base
|
39
|
+
localizable do
|
40
|
+
dictionary lambda { Page.localizable_attrs }
|
41
|
+
end
|
42
|
+
|
43
|
+
class << self
|
44
|
+
def localizable_attrs
|
45
|
+
[:foo, :bar, :baz]
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
```
|
50
|
+
|
51
|
+
Usage examples:
|
52
|
+
|
53
|
+
``` ruby
|
54
|
+
page = Page.create(locale: "en", name: "Hello")
|
55
|
+
page.name? # => true
|
56
|
+
page.name.to_s # => "Hello"
|
57
|
+
```
|
58
|
+
|
59
|
+
To get a localized version of a page, call `.localize` on it:
|
60
|
+
|
61
|
+
``` ruby
|
62
|
+
page = Page.first.localize("en")
|
63
|
+
```
|
64
|
+
|
65
|
+
`.localize` can also take a block argument:
|
66
|
+
|
67
|
+
``` ruby
|
68
|
+
page.localize("nb") do |p|
|
69
|
+
p.locale # => "nb"
|
70
|
+
end
|
71
|
+
p.locale # => "en"
|
72
|
+
```
|
73
|
+
|
74
|
+
Multiple locales can be updated at the same time:
|
75
|
+
|
76
|
+
``` ruby
|
77
|
+
page.name = { en: "Hello", fr: "Bonjour" }
|
78
|
+
```
|
19
79
|
|
20
80
|
## License
|
21
81
|
|
@@ -6,6 +6,9 @@ class CreateLocalizations < ActiveRecord::Migration
|
|
6
6
|
t.string :locale
|
7
7
|
t.text :value, limit: 16_777_215
|
8
8
|
t.timestamps null: false
|
9
|
+
t.index([:localizable_id, :localizable_type, :name, :locale],
|
10
|
+
name: "index_localizations_on_locale")
|
11
|
+
t.index [:localizable_id, :localizable_type]
|
9
12
|
end
|
10
13
|
end
|
11
14
|
end
|