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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 17671afe71bebccadf1ed44e9739a29370b8a262
4
- data.tar.gz: 2535f6374e8f747d5c89c417c7c5dd26eb039c2f
3
+ metadata.gz: b0befa06d12b655333ff09bd3f7f009b21806c34
4
+ data.tar.gz: 74af6b379c30a03c157aed92ff198397deedad5e
5
5
  SHA512:
6
- metadata.gz: 1b852c3e37f9403fb2add58ec2d79f0741a538fce3cbade925847e5fc1f07d3deeaf4f5411a34a91d727099b5cf0c4e4f33b3730fbb05bf9db051343b79c3fb5
7
- data.tar.gz: ab44351ae8d0f5e9767000b30b654161af31b3c5972d20164001a61ee040f508da6da844dbcd48aa584b386109be71203f76a9b1ee171bd527cde73e5335b5e0
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
@@ -1,6 +1,7 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  load "localizable_model/active_record_extension.rb"
4
+ load "localizable_model/any_localizer.rb"
4
5
  load "localizable_model/class_methods.rb"
5
6
  load "localizable_model/configuration.rb"
6
7
  load "localizable_model/engine.rb"
@@ -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
@@ -17,6 +17,12 @@ module LocalizableModel
17
17
  #
18
18
  delegate :locale, to: :localizer
19
19
 
20
+ # Returns an AnyLocalizer for the model.
21
+ #
22
+ def any_locale
23
+ AnyLocalizer.new(self)
24
+ end
25
+
20
26
  # Setter for locale
21
27
  #
22
28
  # page.locale = 'no' # => 'no'
@@ -1,5 +1,5 @@
1
1
  # encoding: utf-8
2
2
 
3
3
  module LocalizableModel
4
- VERSION = "0.5.5".freeze unless LocalizableModel.const_defined?("VERSION")
4
+ VERSION = "0.6.0".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.5
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-06-07 00:00:00.000000000 Z
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