module RailsTranslateModels::InstanceMethods

Public Class Methods

included(base) click to toggle source
# File lib/rails-translate-models.rb, line 36
    def self.included(base)
      attributes = base.has_translations_options

      attributes.each do |attribute|
        base.class_eval <<-GETTER_AND_SETTER
         def get_#{attribute}(locale=nil)
           get_translated_attribute(locale, :#{attribute})
         end

         def set_#{attribute}(value, locale=I18n.locale)
           set_translated_attribute(locale, :#{attribute}, value)
         end

         alias #{attribute} get_#{attribute}
         alias #{attribute}= set_#{attribute}
       GETTER_AND_SETTER
      end
    end

Public Instance Methods

get_translated_attribute(locale, attribute) click to toggle source
# File lib/rails-translate-models.rb, line 55
def get_translated_attribute(locale, attribute)
  translated_value = if locale
    translated_attributes_for(locale)[attribute]
  else
    # find translated attribute, first try current locale, then default_locale
    text = translated_attributes_for(I18n.locale)[attribute] || translated_attributes_for(I18n.default_locale)[attribute]
  end
end
method_missing(name, *args) click to toggle source
Calls superclass method
# File lib/rails-translate-models.rb, line 89
def method_missing(name, *args)
  attribute, locale = parse_translated_attribute_method(name)
  return super unless attribute
  if name.to_s.include? '='
    send("set_#{attribute}", args[0], locale)
  else
    send("get_#{attribute}", locale)
  end
end
respond_to?(name, *args) click to toggle source
Calls superclass method
# File lib/rails-translate-models.rb, line 84
def respond_to?(name, *args)
  return true if parse_translated_attribute_method(name)
  super
end
set_translated_attribute(locale, attribute, value) click to toggle source
# File lib/rails-translate-models.rb, line 64
def set_translated_attribute(locale, attribute, value)
  old_value = translated_attributes_for(locale)[attribute]
  return if old_value.to_s == value.to_s
  changed_attributes.merge!("#{attribute}_in_#{locale}" => old_value)
  translated_attributes_for(locale)[attribute] = value
  @translated_attributes_changed = true
end
translatable?() click to toggle source
# File lib/rails-translate-models.rb, line 32
def translatable?
  true
end
translated_attributes() click to toggle source
# File lib/rails-translate-models.rb, line 72
def translated_attributes
  return @translated_attributes if @translated_attributes
  merge_db_translations_with_instance_variable
  @translated_attributes ||= {}.with_indifferent_access
end
translated_attributes=(hash) click to toggle source
# File lib/rails-translate-models.rb, line 78
def translated_attributes= hash
  @db_translations_merged = true
  @translated_attributes_changed = true
  @translated_attributes = hash.with_indifferent_access
end

Protected Instance Methods

store_translated_attributes() click to toggle source
# File lib/rails-translate-models.rb, line 101
def store_translated_attributes
  return true unless @translated_attributes_changed
  @translated_attributes.each do |locale, attributes|
    unless attributes.blank?
      translation = translations.find_or_initialize_by_language_code(locale.to_s)
      translation.attributes = translation.attributes.merge(attributes)
      translation.save!
    end
  end
  @translated_attributes_changed = false
  true
end

Private Instance Methods

is_translated_attribute?(method_name) click to toggle source
# File lib/rails-translate-models.rb, line 139
def is_translated_attribute?(method_name)
  attributes = self.class.has_translations_options
  attributes.include? method_name.sub('=','').to_sym
end
merge_db_translations_with_instance_variable() click to toggle source
# File lib/rails-translate-models.rb, line 116
def merge_db_translations_with_instance_variable
  return if new_record? or @db_translations_merged
  @db_translations_merged = true

  translations.each do |t|
    has_translations_options.each do |attribute|
      translated_attributes_for(t.language_code)[attribute] = eval("t.#{attribute.to_s}")
    end
  end
end
parse_translated_attribute_method(name) click to toggle source
# File lib/rails-translate-models.rb, line 132
def parse_translated_attribute_method(name)
  return false if name.to_s !~ /^([a-zA-Z_]+)_in_([a-z]{2})[=]?$/
  attribute = $1; locale = $2
  return false unless is_translated_attribute?(attribute)
  return attribute, locale
end
translated_attributes_for(locale) click to toggle source
# File lib/rails-translate-models.rb, line 127
def translated_attributes_for(locale)
  translated_attributes[locale] ||= {}.with_indifferent_access
  translated_attributes[locale]
end