10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
|
# File 'lib/has_foreign_language.rb', line 10
def has_foreign_language(*args)
args.each do |field|
define_method(field.to_s) do
if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0
result = self.send("#{field}_#{I18n.locale}".to_sym)
result.blank? ? super() : self.send("#{field}_#{I18n.locale}".to_sym) else
super()
end
end
define_method("#{field}=") do |val|
if I18n.locale != I18n.default_locale && self.class.columns.select {|c| c.name == "#{field}_#{I18n.locale}"}.length > 0
self["#{field}_#{I18n.locale}".to_sym] = val
else
self[field.to_sym] = val
end
end
define_method("#{field}_#{I18n.default_locale}") do
self.send(field.to_sym)
end
define_method("#{field}_#{I18n.default_locale}=") do |val|
self[field.to_sym] = val
end
end
end
|