localizable_model 0.5.5 → 0.6.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +9 -0
- data/lib/localizable_model.rb +1 -0
- data/lib/localizable_model/any_localizer.rb +46 -0
- data/lib/localizable_model/instance_methods.rb +6 -0
- data/lib/localizable_model/version.rb +1 -1
- metadata +3 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: b0befa06d12b655333ff09bd3f7f009b21806c34
|
4
|
+
data.tar.gz: 74af6b379c30a03c157aed92ff198397deedad5e
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: e8885b708fd5f7b187640b38a1aaac9afa8f3f19266dba80b34e84d53fab60cb5fdea4a62fbba62b5f446d1ab16f39357d1ee3ce33efaf625bda51f954c7f97b
|
7
|
+
data.tar.gz: e8364bc5267a73a71115f2403c1678065cb22867899eda3ed4eb5fd700ca699b1f1640b985cc785aab8858bdfc9d09da84c34ad498626547d75cff7973e88d8b
|
data/README.md
CHANGED
@@ -77,6 +77,15 @@ Multiple locales can be updated at the same time:
|
|
77
77
|
page.name = { en: "Hello", fr: "Bonjour" }
|
78
78
|
```
|
79
79
|
|
80
|
+
By chaining through `.any_locale`, you can get results from other locales if
|
81
|
+
a localization is missing.
|
82
|
+
|
83
|
+
``` ruby
|
84
|
+
page = Page.create(locale: "fr", name: "Bonjour").localize("en")
|
85
|
+
page.any_locale.name? # => true
|
86
|
+
page.any_locale.name # => "Bonjour"
|
87
|
+
```
|
88
|
+
|
80
89
|
## License
|
81
90
|
|
82
91
|
LocalizableModel is licensed under the
|
data/lib/localizable_model.rb
CHANGED
@@ -0,0 +1,46 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module LocalizableModel
|
4
|
+
class AnyLocalizer
|
5
|
+
attr_reader :record
|
6
|
+
|
7
|
+
def initialize(record)
|
8
|
+
@record = record
|
9
|
+
define_localization_methods
|
10
|
+
end
|
11
|
+
|
12
|
+
private
|
13
|
+
|
14
|
+
def define_localization_methods
|
15
|
+
record.class.localized_attributes.each do |attribute|
|
16
|
+
self.class.send(:define_method, attribute) do
|
17
|
+
localized(attribute)
|
18
|
+
end
|
19
|
+
|
20
|
+
self.class.send(:define_method, "#{attribute}?") do
|
21
|
+
localized?(attribute)
|
22
|
+
end
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
def locales
|
27
|
+
(
|
28
|
+
[record.locale, I18n.locale] + I18n.available_locales + record.locales
|
29
|
+
).compact.map(&:to_sym).uniq
|
30
|
+
end
|
31
|
+
|
32
|
+
def localized?(attribute)
|
33
|
+
!localized(attribute).blank?
|
34
|
+
end
|
35
|
+
|
36
|
+
def localized(attribute)
|
37
|
+
localized = locales.inject(nil) do |str, l|
|
38
|
+
str ||= -> {
|
39
|
+
value = record.localize(l).send(attribute)
|
40
|
+
value.blank? ? nil : value
|
41
|
+
}.call()
|
42
|
+
end
|
43
|
+
localized || ""
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: localizable_model
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.
|
4
|
+
version: 0.6.0
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Inge Jørgensen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2017-
|
11
|
+
date: 2017-08-23 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mysql2
|
@@ -106,6 +106,7 @@ files:
|
|
106
106
|
- app/models/localization.rb
|
107
107
|
- lib/localizable_model.rb
|
108
108
|
- lib/localizable_model/active_record_extension.rb
|
109
|
+
- lib/localizable_model/any_localizer.rb
|
109
110
|
- lib/localizable_model/class_methods.rb
|
110
111
|
- lib/localizable_model/configuration.rb
|
111
112
|
- lib/localizable_model/engine.rb
|