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
109
110
111
112
113
114
115
116
117
|
# File 'lib/translated_attributes.rb', line 109
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
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
|
# File 'lib/translated_attributes.rb', line 33
def self.included(base)
fields = base.translated_attributes_options[:fields]
fields.each do |field|
base.class_eval " def get_\#{field}(locale=nil)\n get_translated_attribute(locale, :\#{field})\n end\n\n def set_\#{field}(value, locale=I18n.locale)\n set_translated_attribute(locale, :\#{field}, value)\n end\n\n #generic aliases (and since e.g. title=(a,b) would not be possible)\n alias \#{field} get_\#{field}\n alias \#{field}= set_\#{field}\n GETTER_AND_SETTER\n end\n\n base.after_save :store_translated_attributes\nend\n"
|
Instance Method Details
#attributes=(new_attributes, *args) ⇒ Object
54
55
56
57
58
59
60
|
# File 'lib/translated_attributes.rb', line 54
def attributes=(new_attributes, *args)
unless ( translations_hash = new_attributes.select{|k,v| is_translated_attribute_hash?(k) } ).blank?
@translated_attributes = translations_hash.with_indifferent_access
new_attributes.delete_if{|k,v| is_translated_attribute_hash?(k) }
end
super
end
|
#get_translated_attribute(locale, field) ⇒ Object
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
|
# File 'lib/translated_attributes.rb', line 62
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
104
105
106
107
|
# File 'lib/translated_attributes.rb', line 104
def respond_to?(name, *args)
return true if parse_translated_attribute_method(name)
super
end
|
#set_translated_attribute(locale, field, value) ⇒ Object
84
85
86
87
88
89
90
|
# File 'lib/translated_attributes.rb', line 84
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
92
93
94
95
96
|
# File 'lib/translated_attributes.rb', line 92
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
98
99
100
101
102
|
# File 'lib/translated_attributes.rb', line 98
def translated_attributes= hash
@db_translations_merged = false
@translated_attributes_changed = true
@translated_attributes = hash.with_indifferent_access
end
|