localizable_model 0.5.2 → 0.5.3
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: c8658451c13691f4849d81bbcb28008296e3900b
|
4
|
+
data.tar.gz: 8f102bb44810aa1587da77de5f9fcf52a1c22c49
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 63e5846c982739c92ee8ccf224f3a04cf9e73579e56f58b08126c898e89a5951396e5240cc170286ca78eda7f0bb0baa0f0252a26ce13554083f7a6979292048
|
7
|
+
data.tar.gz: 5f7aad7ebf1941bb5f810ab575110dd048180653ca43e2ac03a5fb00bbe90d466226b13108efc5ede572fc2b8e912a81ffcdfe73bb3980787915a89c5f63447e
|
@@ -60,13 +60,19 @@ module LocalizableModel
|
|
60
60
|
self
|
61
61
|
end
|
62
62
|
|
63
|
+
# Returns a list of all the localized attributes
|
64
|
+
#
|
65
|
+
# page.localized_attributes # => { "name" => { "en" => "Hello" } }
|
66
|
+
#
|
67
|
+
delegate :localized_attributes, to: :localizer
|
68
|
+
|
63
69
|
# assign_attributes from ActiveRecord is overridden to catch locale before
|
64
70
|
# any other attributes are written. This enables the following construct:
|
65
71
|
#
|
66
72
|
# Page.create(name: 'My Page', locale: 'en')
|
67
73
|
#
|
68
74
|
def assign_attributes(new_attributes)
|
69
|
-
if new_attributes.
|
75
|
+
if new_attributes.respond_to?(:[])
|
70
76
|
attributes = new_attributes.stringify_keys
|
71
77
|
self.locale = attributes["language"] if attributes.key?("language")
|
72
78
|
self.locale = attributes["locale"] if attributes.key?("locale")
|
@@ -20,6 +20,13 @@ module LocalizableModel
|
|
20
20
|
locale ? true : false
|
21
21
|
end
|
22
22
|
|
23
|
+
def localized_attributes
|
24
|
+
attribute_names.each_with_object({}) do |attr, h|
|
25
|
+
h[attr] = {}
|
26
|
+
locales.each { |l| h[attr][l] = get_value(attr, l) }
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
23
30
|
def get(attribute, options = {})
|
24
31
|
get_options = { locale: locale }.merge(options)
|
25
32
|
|
@@ -35,7 +42,7 @@ module LocalizableModel
|
|
35
42
|
|
36
43
|
def set(attribute, value, options = {})
|
37
44
|
set_options = { locale: locale }.merge(options)
|
38
|
-
if value.is_a?(Hash)
|
45
|
+
if value.is_a?(Hash) || value.is_a?(ActionController::Parameters)
|
39
46
|
value.each { |loc, val| set(attribute, val, locale: loc) }
|
40
47
|
else
|
41
48
|
require_locale!(attribute, set_options[:locale])
|
@@ -54,12 +61,21 @@ module LocalizableModel
|
|
54
61
|
|
55
62
|
private
|
56
63
|
|
64
|
+
def attribute_names
|
65
|
+
@configuration.attributes.keys.map(&:to_s)
|
66
|
+
end
|
67
|
+
|
57
68
|
def find_localizations(name, locale)
|
58
69
|
@model.localizations.select do |l|
|
59
70
|
l.name == name && l.locale == locale
|
60
71
|
end
|
61
72
|
end
|
62
73
|
|
74
|
+
def get_value(attribute, locale)
|
75
|
+
localization = find_localizations(attribute, locale).try(&:first)
|
76
|
+
localization.value if localization
|
77
|
+
end
|
78
|
+
|
63
79
|
def require_locale!(attribute, locale)
|
64
80
|
return if locale
|
65
81
|
raise(ArgumentError,
|
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.5.
|
4
|
+
version: 0.5.3
|
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:
|
11
|
+
date: 2017-03-01 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: mysql2
|