module HstoreTranslate::Translates::InstanceMethods

Attributes

enabled_fallback[R]

Public Instance Methods

disable_fallback() click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 4
def disable_fallback
  toggle_fallback(false)
end
enable_fallback() click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 8
def enable_fallback
  toggle_fallback(true)
end

Protected Instance Methods

hstore_translate_fallback_locales(locale) click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 16
def hstore_translate_fallback_locales(locale)
  return locale if enabled_fallback == false || !I18n.respond_to?(:fallbacks)
  I18n.fallbacks[locale]
end
method_missing_with_translates(method_name, *args) click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 44
def method_missing_with_translates(method_name, *args)
  translated_attr_name, locale, assigning = parse_translated_attribute_accessor(method_name)

  return method_missing_without_translates(method_name, *args) unless translated_attr_name

  if assigning
    write_hstore_translation(translated_attr_name, args.first, locale)
  else
    read_hstore_translation(translated_attr_name, locale)
  end
end
parse_translated_attribute_accessor(method_name) click to toggle source

Internal: Parse a translated convenience accessor name.

method_name - The accessor name.

Examples

parse_translated_attribute_accessor("title_en=")
# => [:title, :en, true]

parse_translated_attribute_accessor("title_fr")
# => [:title, :fr, false]

Returns the attribute name Symbol, locale Symbol, and a Boolean indicating whether or not the caller is attempting to assign a value.

# File lib/hstore_translate/translates/instance_methods.rb, line 70
def parse_translated_attribute_accessor(method_name)
  return unless /(?<attribute>[a-z_]+)_(?<locale>[a-z]{2})(?<assignment>=?)\z/ =~ method_name

  translated_attr_name = attribute.to_sym
  return unless translated_attribute_names.include?(translated_attr_name)

  locale    = locale.to_sym
  assigning = assignment.present?

  [translated_attr_name, locale, assigning]
end
read_hstore_translation(attr_name, locale = I18n.locale) click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 21
def read_hstore_translation(attr_name, locale = I18n.locale)
  translations = public_send("#{attr_name}#{SUFFIX}") || {}
  available = Array(hstore_translate_fallback_locales(locale)).detect do |available_locale|
    translations[available_locale.to_s].present?
  end

  translations[available.to_s]
end
respond_to_with_translates?(symbol, include_all = false) click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 39
def respond_to_with_translates?(symbol, include_all = false)
  return true if parse_translated_attribute_accessor(symbol)
  respond_to_without_translates?(symbol, include_all)
end
toggle_fallback(enabled) { || ... } click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 82
def toggle_fallback(enabled)
  if block_given?
    old_value = @enabled_fallback
    begin
      @enabled_fallback = enabled
      yield
    ensure
      @enabled_fallback = old_value
    end
  else
    @enabled_fallback = enabled
  end
end
write_hstore_translation(attr_name, value, locale = I18n.locale) click to toggle source
# File lib/hstore_translate/translates/instance_methods.rb, line 30
def write_hstore_translation(attr_name, value, locale = I18n.locale)
  translation_store = "#{attr_name}#{SUFFIX}"
  translations = public_send(translation_store) || {}
  public_send("#{translation_store}_will_change!") unless translations[locale.to_s] == value
  translations[locale.to_s] = value
  public_send("#{translation_store}=", translations)
  value
end