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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
|
# File 'lib/money-rails/active_record/monetizable.rb', line 22
def monetize(*fields)
options = fields.
fields.each do |field|
subunit_name = field.to_s
if options[:field_currency] || options[:target_name] || options[:model_currency]
ActiveSupport::Deprecation.warn("You are using the old " \
"argument keys of the monetize command! Instead use :as, " \
":with_currency or :with_model_currency")
end
name = options[:as] || options[:target_name] || nil
if name
name = name.to_s
if name == subunit_name
raise ArgumentError, "monetizable attribute name cannot be the same as options[:as] parameter"
end
elsif subunit_name =~ /#{MoneyRails::Configuration.amount_column[:postfix]}$/
name = subunit_name.sub(/#{MoneyRails::Configuration.amount_column[:postfix]}$/, "")
else
raise ArgumentError, "Unable to infer the name of the monetizable attribute for '#{subunit_name}'. " \
"Expected amount column postfix is '#{MoneyRails::Configuration.amount_column[:postfix]}'. " \
"Use :as option to explicitly specify the name or change the amount column postfix in the initializer."
end
instance_currency_name = options[:with_model_currency] ||
options[:model_currency] ||
MoneyRails::Configuration.currency_column[:column_name]
if !instance_currency_name && MoneyRails::Configuration.currency_column[:postfix].present?
instance_currency_name = "#{name}#{MoneyRails::Configuration.currency_column[:postfix]}"
end
instance_currency_name = instance_currency_name && instance_currency_name.to_s
field_currency_name = options[:with_currency] ||
options[:field_currency] || nil
track_monetized_attribute name, subunit_name
if (validation_enabled = MoneyRails.include_validations && !options[:disable_validation])
if (subunit_numericality = options.fetch(:subunit_numericality, true))
validates subunit_name, {
allow_nil: options[:allow_nil],
numericality: subunit_numericality
}
end
if (numericality = options.fetch(:numericality, true))
validates name.to_sym, {
allow_nil: options[:allow_nil],
'money_rails/active_model/money' => numericality
}
end
end
define_method name do |*args|
read_monetized name, subunit_name, options, *args
end
define_method "#{name}=" do |value|
write_monetized name, subunit_name, value, validation_enabled, instance_currency_name, options
end
if validation_enabled
define_method "#{subunit_name}=" do |value|
instance_variable_set "@#{name}_money_before_type_cast", nil
write_attribute(subunit_name, value)
end
end
define_method "currency_for_#{name}" do
currency_for name, instance_currency_name, field_currency_name
end
attr_reader "#{name}_money_before_type_cast"
after_save do
instance_variable_set "@#{name}_money_before_type_cast", nil
end
end
end
|