Module: RailsTranslateModels::InstanceMethods

Defined in:
lib/rails-translate-models.rb

Class Method Summary collapse

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args) ⇒ Object



89
90
91
92
93
94
95
96
97
# 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

Class Method Details

.included(base) ⇒ Object



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# 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

Instance Method Details

#get_translated_attribute(locale, attribute) ⇒ Object



55
56
57
58
59
60
61
62
# 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

#respond_to?(name, *args) ⇒ Boolean

Returns:

  • (Boolean)


84
85
86
87
# 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) ⇒ Object



64
65
66
67
68
69
70
# 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?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'lib/rails-translate-models.rb', line 32

def translatable?
  true
end

#translated_attributesObject



72
73
74
75
76
# 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) ⇒ Object



78
79
80
81
82
# 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