module Para::I18n::Model

Public Instance Methods

disabled_for_locale?() click to toggle source
# File lib/para/i18n/model.rb, line 36
def disabled_for_locale?
  self.class.translates? && _disabled_for_locale
end
model_translations() click to toggle source
# File lib/para/i18n/model.rb, line 19
def model_translations
  unless respond_to?(:_translations)
    raise "The model #{ self.class.name } is not translatable. " +
          "Please run `rails g para:i18n:translate #{ self.model_name.element }` " +
          "generator to create the model's migration."
  end

  self._translations ||= {}
end
read_plain_or_store_attribute(field) click to toggle source

This method allows reading an attribute from the ActiveRecord table, whether it's a plain column of the table, or a field of a store (hash, json etc.) of the table, accessed through the ActiveRecord.store_accessor interface.

# File lib/para/i18n/model.rb, line 44
def read_plain_or_store_attribute(field)
  if plain_column?(field)
    read_attribute(field)
  elsif (store_name = find_stored_column(field))
    read_store_attribute(store_name, field)
  else
    raise ActiveRecord::UnknownAttributeError.new(self, field)
  end
end
read_translated_attribute(field, locale = ::I18n.locale) click to toggle source
# File lib/para/i18n/model.rb, line 11
def read_translated_attribute(field, locale = ::I18n.locale)
  Para::I18n::AttributeTranslation.read(locale, self, field)
end
translation_for(locale) click to toggle source
# File lib/para/i18n/model.rb, line 29
def translation_for(locale)
  case locale.to_sym
  when I18n.default_locale then default_locale_translations
  else model_translations[locale.to_s] || {}
  end.with_indifferent_access
end
write_plain_or_store_attribute(field, value) click to toggle source
# File lib/para/i18n/model.rb, line 54
def write_plain_or_store_attribute(field, value)
  if plain_column?(field)
    write_attribute(field, value)
  elsif (store_name = find_stored_column(field))
    write_store_attribute(store_name, field, value)
  else
    raise ActiveRecord::UnknownAttributeError.new(self, field)
  end
end
write_translated_attribute(field, value, locale = ::I18n.locale) click to toggle source
# File lib/para/i18n/model.rb, line 15
def write_translated_attribute(field, value, locale = ::I18n.locale)
  Para::I18n::AttributeTranslation.write(locale, self, field, value)
end

Private Instance Methods

default_locale_translations() click to toggle source
# File lib/para/i18n/model.rb, line 78
def default_locale_translations
  translated_attribute_names.each_with_object({}) do |attribute_name, hash|
    hash[attribute_name] = read_plain_or_store_attribute(attribute_name)
  end
end
find_stored_column(field) click to toggle source
# File lib/para/i18n/model.rb, line 70
def find_stored_column(field)
  store = self.class.stored_attributes.find do |name, attributes|
    attributes.include?(field.to_sym)
  end

  store.first if store
end
plain_column?(field) click to toggle source
# File lib/para/i18n/model.rb, line 66
def plain_column?(field)
  self.class.columns_hash.key?(field.to_s)
end