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: b6117f00c6fec1025aa330022a84f21cad2eece0
4
- data.tar.gz: f9902eea37e398d8ed818d0ce201197875ca73cc
3
+ metadata.gz: c8658451c13691f4849d81bbcb28008296e3900b
4
+ data.tar.gz: 8f102bb44810aa1587da77de5f9fcf52a1c22c49
5
5
  SHA512:
6
- metadata.gz: d60c8b41363ce53ebadf021d3d810b14dd4ebdbdebe5c141a2644ea0e8b35a4f375f4efd2acb2ff5177d9d4b56467625202560977dd90e937fecda88bf145481
7
- data.tar.gz: f3e8a3c0481c3f6f89b6f1f9f3ce5092b932706af78e983b90a2600881ef9eb971706313506772dd6342d9e9dc532f0a4484f6daaac75445bcb93327c6c4e2fd
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.is_a?(Hash)
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,
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module LocalizableModel
4
- VERSION = "0.5.2".freeze unless LocalizableModel.const_defined?("VERSION")
4
+ VERSION = "0.5.3".freeze unless LocalizableModel.const_defined?("VERSION")
5
5
  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.5.2
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: 2016-08-02 00:00:00.000000000 Z
11
+ date: 2017-03-01 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: mysql2