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
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
|
# File 'lib/money-rails/active_record/monetizable.rb', line 11
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
instance_currency_name = options[:with_model_currency] ||
options[:model_currency] ||
MoneyRails::Configuration.currency_column[:column_name]
instance_currency_name = instance_currency_name &&
instance_currency_name.to_s
field_currency_name = options[:with_currency] ||
options[:field_currency] || nil
name = options[:as] || options[:target_name] || nil
if name
name = name.to_s
elsif subunit_name =~ /#{MoneyRails::Configuration.amount_column[:postfix]}$/
name = subunit_name.sub(/#{MoneyRails::Configuration.amount_column[:postfix]}$/, "")
else
name = [subunit_name, "money"].join("_")
end
@monetized_attributes ||= {}.with_indifferent_access
if @monetized_attributes[name].present?
raise ArgumentError, "#{self} already has a monetized attribute called '#{name}'"
end
@monetized_attributes[name] = subunit_name
class << self
def monetized_attributes
@monetized_attributes || superclass.monetized_attributes
end
end unless respond_to? :monetized_attributes
if validation_enabled = MoneyRails.include_validations && !options[:disable_validation]
subunit_validation_options =
unless options.has_key? :subunit_numericality
true
else
options[:subunit_numericality]
end
money_validation_options =
unless options.has_key? :numericality
true
else
options[:numericality]
end
validates subunit_name, {
:allow_nil => options[:allow_nil],
:numericality => subunit_validation_options
}
validates name.to_sym, {
:allow_nil => options[:allow_nil],
'money_rails/active_model/money' => money_validation_options
}
end
define_method name do |*args|
amount = public_send(subunit_name, *args)
attr_currency = public_send("currency_for_#{name}")
memoized = instance_variable_get("@#{name}")
return memoized if memoized && memoized.cents == amount &&
memoized.currency == attr_currency
amount = Money.new(amount, attr_currency) unless amount.blank?
instance_variable_set "@#{name}", amount
end
define_method "#{name}=" do |value|
instance_variable_set "@#{name}_money_before_type_cast", value
if options[:allow_nil] && value.blank?
money = nil
else
if value.is_a?(Money)
money = value
else
begin
money = value.to_money(public_send("currency_for_#{name}"))
rescue NoMethodError
return nil
rescue ArgumentError
raise if MoneyRails.raise_error_on_money_parsing
return nil
rescue Money::Currency::UnknownCurrency
raise if MoneyRails.raise_error_on_money_parsing
return nil
end
end
end
if !validation_enabled
public_send("#{subunit_name}=", money.try(:cents))
elsif self.class.respond_to?(:attribute_aliases) &&
self.class.attribute_aliases.key?(subunit_name)
original_name = self.class.attribute_aliases[subunit_name.to_s]
write_attribute(original_name, money.try(:cents))
else
write_attribute(subunit_name, money.try(:cents))
end
money_currency = money.try(:currency)
if instance_currency_name.present? &&
respond_to?("#{instance_currency_name}=")
public_send("#{instance_currency_name}=", money_currency.try(:iso_code))
else
current_currency = public_send("currency_for_#{name}")
if money_currency && current_currency != money_currency.id
raise "Can't change readonly currency '#{current_currency}' to '#{money_currency}' for field '#{name}'"
end
end
instance_variable_set "@#{name}", money
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
instance_currency_name_with_postfix = "#{name}#{MoneyRails::Configuration.currency_column[:postfix]}"
if instance_currency_name.present? &&
respond_to?(instance_currency_name) &&
Money::Currency.find(public_send(instance_currency_name))
Money::Currency.find(public_send(instance_currency_name))
elsif field_currency_name
Money::Currency.find(field_currency_name)
elsif respond_to?(instance_currency_name_with_postfix) &&
Money::Currency.find(public_send(instance_currency_name_with_postfix))
Money::Currency.find(public_send(instance_currency_name_with_postfix))
elsif self.class.respond_to?(:currency)
self.class.currency
else
Money.default_currency
end
end
attr_reader "#{name}_money_before_type_cast"
after_save do
instance_variable_set "@#{name}_money_before_type_cast", nil
end
end
end
|