6
7
8
9
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
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
|
# File 'lib/transilator/active_record_extensions.rb', line 6
def transilator(*args)
args.each do |attribute|
define_method attribute do
locale = I18n.locale.to_s
attribute_value_hash = (self[attribute] || {})
value = (attribute_value_hash)[locale] || ""
return value unless value.empty?
fallbacks_for_locale = Transilator.config.get_fallbacks_for_locale(locale)
return value if fallbacks_for_locale.empty?
fallback_locale_to_use = fallbacks_for_locale.find {|f| (attribute_value_hash)[f] }
return "" unless fallback_locale_to_use
attribute_value_hash[fallback_locale_to_use] || ""
end
define_method "#{attribute}=" do |value|
if value.is_a?(Hash)
write_attribute attribute, value
elsif self[attribute].present?
write_attribute attribute, self[attribute].merge({I18n.locale => value })
value
else
write_attribute attribute, {I18n.locale => value }
value
end
end
define_method "#{attribute}_before_type_cast" do
self[attribute] || {}
end
module_eval do
serialize "#{attribute}", ActiveRecord::Coders::Hstore
end if defined?(ActiveRecord::Coders::Hstore)
I18n.available_locales.map(&:to_s).each do |locale|
define_method "#{attribute}_#{locale}" do
(self[attribute] || {})[locale] || ""
end
define_method "#{attribute}_#{locale}=" do |value|
if self[attribute].present?
write_attribute attribute, self[attribute].merge({locale => value })
else
write_attribute attribute, {locale => value }
end
end
end
end
end
|