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
74
75
76
77
78
79
80
81
82
83
84
85
86
|
# File 'lib/multilang-hstore/active_record_extensions.rb', line 10
def multilang(*args)
options = {
:required => false,
:length => nil,
:accessible => false,
:format => nil
}.merge(args.)
options.assert_valid_keys([:required, :length, :accessible, :format, :nil])
define_translation_base! unless included_modules.include?(InstanceMethods)
args.each do |attribute|
define_method attribute do
multilang_translation_keeper(attribute).value
end
define_method "#{attribute}=" do |value|
multilang_translation_keeper(attribute).update(value)
end
define_method "#{attribute}_before_type_cast" do
multilang_translation_keeper(attribute).translations
end
if options[:accessible]
matr = multilang_accessible_translations + [attribute.to_sym]
class_variable_set(:@@multilang_accessible_translations, matr)
end
module_eval do
serialize "#{attribute}", ActiveRecord::Coders::Hstore
end
I18n.available_locales.each do |locale|
define_method "#{attribute}_#{locale}" do
multilang_translation_keeper(attribute)[locale]
end
define_method "#{attribute}_#{locale}=" do |value|
multilang_translation_keeper(attribute)[locale] = value
end
if options[:accessible]
matr = multilang_accessible_translations + ["#{attribute}_#{locale}".to_sym]
class_variable_set(:@@multilang_accessible_translations, matr)
end
if options[:required]
module_eval do
validates_presence_of "#{attribute}_#{locale}"
end
end
if options[:length]
module_eval do
validates_length_of "#{attribute}_#{locale}", :maximum => options[:length]
end
end
if options[:format]
module_eval do
validates_format_of "#{attribute}_#{locale}", :with => options[:format]
end
end
end
end
end
|