Module: PagesCore::Localizable::InstanceMethods
- Defined in:
- lib/pages_core/localizable/instance_methods.rb
Overview
Localizable::InstanceMethods
This is the public API for all localizable models. See Localizable for usage examples.
Instance Method Summary collapse
-
#assign_attributes(new_attributes) ⇒ Object
assign_attributes from ActiveRecord is overridden to catch locale before any other attributes are written.
-
#locale ⇒ Object
(also: #working_language)
Getter for locale.
-
#locale=(locale) ⇒ Object
(also: #working_language=)
Setter for locale.
-
#locale? ⇒ Boolean
Returns true if this page has a locale set.
-
#locales ⇒ Object
Returns all locales saved for this page.
-
#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)
111 112 113 114 115 116 117 118 119 120 121 122 123 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 111 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
#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')
75 76 77 78 79 80 81 82 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 75 def assign_attributes(new_attributes) if new_attributes.is_a?(Hash) 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 ⇒ Object Also known as: working_language
Getter for locale
page.locale # => 'en'
21 22 23 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 21 def locale localizer.locale end |
#locale=(locale) ⇒ Object Also known as: working_language=
Setter for locale
page.locale = 'no' # => 'no'
29 30 31 32 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 29 def locale=(locale) @localizer = Localizer.new(self) localizer.locale = locale end |
#locale? ⇒ Boolean
Returns true if this page has a locale set
36 37 38 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 36 def locale? localizer.locale? end |
#locales ⇒ Object
Returns all locales saved for this page.
13 14 15 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 13 def locales localizer.locales 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'
52 53 54 55 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 52 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.
64 65 66 67 68 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 64 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?
86 87 88 89 |
# File 'lib/pages_core/localizable/instance_methods.rb', line 86 def respond_to?(method_name, *args) requested_attribute, _ = method_name.to_s.match(/(.*?)([\?=]?)$/)[1..2] localizer.attribute?(requested_attribute.to_sym) ? true : super end |