Module: LocalizableModel::InstanceMethods
- Defined in:
- lib/localizable_model/instance_methods.rb
Overview
LocalizableModel::InstanceMethods
This is the public API for all localizable models. See Localizable for usage examples.
Instance Method Summary collapse
-
#any_locale ⇒ Object
Returns an AnyLocalizer for the model.
-
#assign_attributes(new_attributes) ⇒ Object
assign_attributes from ActiveRecord is overridden to catch locale before any other attributes are written.
-
#locale=(locale) ⇒ Object
(also: #working_language=)
Setter for locale.
-
#localize(locale) ⇒ Object
(also: #translate)
Returns a copy of the model with a different locale.
-
#localize!(locale) ⇒ Object
(also: #translate!)
In-place variant of #localize.
-
#respond_to?(method_name, *args) ⇒ Boolean
A localized model responds to :foo, :foo= and :foo?.
Dynamic Method Handling
This class handles dynamic methods through the method_missing method
#method_missing(method_name, *args) ⇒ Object (protected)
116 117 118 119 120 121 122 123 124 125 126 127 128 |
# File 'lib/localizable_model/instance_methods.rb', line 116 def method_missing(method_name, *args) attr = method_to_attr(method_name) super unless localizer.attribute?(attr) case method_name.to_s when /\?$/ localizer.value_for?(attr) when /=$/ localizer.set(attr, args.first) else localizer.get(attr).to_s end end |
Instance Method Details
#any_locale ⇒ Object
Returns an AnyLocalizer for the model.
22 23 24 |
# File 'lib/localizable_model/instance_methods.rb', line 22 def any_locale @any_localizer ||= AnyLocalizer.new(self) end |
#assign_attributes(new_attributes) ⇒ Object
assign_attributes from ActiveRecord is overridden to catch locale before any other attributes are written. This enables the following construct:
Page.create(name: 'My Page', locale: 'en')
80 81 82 83 84 85 86 87 |
# File 'lib/localizable_model/instance_methods.rb', line 80 def assign_attributes(new_attributes) if new_attributes.respond_to?(:[]) attributes = new_attributes.stringify_keys self.locale = attributes["language"] if attributes.key?("language") self.locale = attributes["locale"] if attributes.key?("locale") end super end |
#locale=(locale) ⇒ Object Also known as: working_language=
Setter for locale
page.locale = 'no' # => 'no'
30 31 32 33 |
# File 'lib/localizable_model/instance_methods.rb', line 30 def locale=(locale) @localizer = Localizer.new(self) localizer.locale = locale end |
#localize(locale) ⇒ Object Also known as: translate
Returns a copy of the model with a different locale.
localized = page.localize('en')
localize also takes a block as an argument, which returns the result of the block.
page.localize('nb') do |p|
p.locale # => 'nb'
end
page.locale # => 'en'
51 52 53 54 |
# File 'lib/localizable_model/instance_methods.rb', line 51 def localize(locale) clone = self.clone.localize!(locale) block_given? ? (yield clone) : clone end |
#localize!(locale) ⇒ Object Also known as: translate!
In-place variant of #localize.
page.localize!('en')
This is functionally equivalent to setting locale=, but returns the model instead of the locale and is chainable.
63 64 65 66 67 |
# File 'lib/localizable_model/instance_methods.rb', line 63 def localize!(locale) @localizer = Localizer.new(self) localizer.locale = locale self end |
#respond_to?(method_name, *args) ⇒ Boolean
A localized model responds to :foo, :foo= and :foo?
91 92 93 94 |
# File 'lib/localizable_model/instance_methods.rb', line 91 def respond_to?(method_name, *args) requested_attribute, = method_name.to_s.match(/(.*?)([\?=]?)$/)[1..2] localizer.attribute?(requested_attribute.to_sym) ? true : super end |