module Mobility::Plugins::LocaleAccessors
Defines methods for a set of locales to access translated attributes in those locales directly with a method call, using a suffix including the locale:
article.title_pt_br
If no locales are passed as an option to the initializer, Mobility.available_locales
(i.e. I18n.available_locales
, or Rails-set available locales for a Rails application) will be used by default.
Private Instance Methods
define_locale_reader(name, locale)
click to toggle source
# File lib/mobility/plugins/locale_accessors.rb, line 39 def define_locale_reader(name, locale) warning_message = "locale passed as option to locale accessor will be ignored" normalized_locale = Mobility.normalize_locale(locale) module_eval <<-EOM, __FILE__, __LINE__ + 1 def #{name}_#{normalized_locale}(options = {}) return super() if options.delete(:super) warn "#{warning_message}" if options[:locale] #{name}(**options, locale: #{locale.inspect}) end EOM module_eval <<-EOM, __FILE__, __LINE__ + 1 def #{name}_#{normalized_locale}?(options = {}) return super() if options.delete(:super) warn "#{warning_message}" if options[:locale] #{name}?(**options, locale: #{locale.inspect}) end EOM end
define_locale_writer(name, locale)
click to toggle source
# File lib/mobility/plugins/locale_accessors.rb, line 60 def define_locale_writer(name, locale) warning_message = "locale passed as option to locale accessor will be ignored" normalized_locale = Mobility.normalize_locale(locale) module_eval <<-EOM, __FILE__, __LINE__ + 1 def #{name}_#{normalized_locale}=(value, options = {}) return super(value) if options.delete(:super) warn "#{warning_message}" if options[:locale] public_send(:#{name}=, value, **options, locale: #{locale.inspect}) end EOM end