Module: TranslatedAttributes::InstanceMethods
- Defined in:
- lib/translated_attributes.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
104
105
106
107
108
109
110
111
112
|
# File 'lib/translated_attributes.rb', line 104
def method_missing(name, *args)
field, locale = parse_translated_attribute_method(name)
return super unless field
if name.to_s.include? '=' send("set_#{field}", args[0], locale)
else
send("get_#{field}", 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
54
55
|
# File 'lib/translated_attributes.rb', line 36
def self.included(base)
fields = base.translated_attributes_options[:fields]
fields.each do |field|
base.class_eval <<-GETTER_AND_SETTER
def get_#{field}(locale=nil)
get_translated_attribute(locale, :#{field})
end
def set_#{field}(value, locale=I18n.locale)
set_translated_attribute(locale, :#{field}, value)
end
#generic aliases (and since e.g. title=(a,b) would not be possible)
alias #{field} get_#{field}
alias #{field}= set_#{field}
GETTER_AND_SETTER
end
base.after_save :store_translated_attributes
end
|
Instance Method Details
#get_translated_attribute(locale, field) ⇒ Object
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
|
# File 'lib/translated_attributes.rb', line 57
def get_translated_attribute(locale, field)
text = if locale
translated_attributes_for(locale)[field]
else
found = translated_attributes_for(I18n.locale)[field] || translated_attributes_for(:en)[field]
if found
found
else
found = translated_attributes.detect{|locale, attributes| not attributes[field].blank?}
found ? found[1][field] : nil
end
end
if self.class.translated_attributes_options[:nil_to_blank]
text || ''
else
text
end
end
|
#respond_to?(name, *args) ⇒ Boolean
99
100
101
102
|
# File 'lib/translated_attributes.rb', line 99
def respond_to?(name, *args)
return true if parse_translated_attribute_method(name)
super
end
|
#set_translated_attribute(locale, field, value) ⇒ Object
79
80
81
82
83
84
85
|
# File 'lib/translated_attributes.rb', line 79
def set_translated_attribute(locale, field, value)
old_value = translated_attributes_for(locale)[field]
return if old_value.to_s == value.to_s
changed_attributes.merge!("#{field}_in_#{locale}" => old_value)
translated_attributes_for(locale)[field] = value
@translated_attributes_changed = true
end
|
#translated_attributes ⇒ Object
87
88
89
90
91
|
# File 'lib/translated_attributes.rb', line 87
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
93
94
95
96
97
|
# File 'lib/translated_attributes.rb', line 93
def translated_attributes= hash
@db_translations_merged = true @translated_attributes_changed = true @translated_attributes = hash.with_indifferent_access
end
|