Raise error when trying to changle readonly model based currency #95

Closed
wants to merge 2 commits into
from

3 participants

@dapi

No description provided.

@alup alup commented on the diff Jun 12, 2013
spec/active_record/monetizable_spec.rb
@@ -44,6 +44,13 @@ class Sub < Product; end
@product.price_cents.should == 215
end
+ it "should rise error if can't change currency" do
@alup
RubyMoney member
alup added a line comment Jun 12, 2013

"raise" instead of "rise"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@alup alup commented on the diff Jun 12, 2013
spec/active_record/monetizable_spec.rb
@@ -326,6 +333,23 @@ class Sub < Product; end
:sale_price_currency_code => 'CAD')
product.sale_price.currency_as_string.should == 'CAD'
end
+ it 'can change currency of custom column' do
@alup
RubyMoney member
alup added a line comment Jun 12, 2013

Please add an empty line here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@alup alup commented on the diff Jun 12, 2013
lib/money-rails/active_record/monetizable.rb
@@ -100,7 +100,17 @@ def monetized_attributes
end
send("#{subunit_name}=", money.try(:cents))
- send("#{instance_currency_name}=", money.try(:currency).try(:iso_code)) if self.respond_to?("#{instance_currency_name}=")
+
+ money_currency = money.try(:currency)
+
+ if self.respond_to?("#{instance_currency_name}=")
+ send("#{instance_currency_name}=", money_currency.try(:iso_code))
+ else
+ current_currency = send("currency_for_#{name}")
+ if money_currency && send("currency_for_#{name}") != money_currency
+ raise "Can't change readonly currenc currency '#{current_currency}' to '#{money_currency}' for field '#{name}'"
@alup
RubyMoney member
alup added a line comment Jun 12, 2013

Fix typo here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@alup alup commented on the diff Jun 12, 2013
lib/money-rails/active_record/monetizable.rb
@@ -100,7 +100,17 @@ def monetized_attributes
end
send("#{subunit_name}=", money.try(:cents))
- send("#{instance_currency_name}=", money.try(:currency).try(:iso_code)) if self.respond_to?("#{instance_currency_name}=")
+
+ money_currency = money.try(:currency)
+
+ if self.respond_to?("#{instance_currency_name}=")
+ send("#{instance_currency_name}=", money_currency.try(:iso_code))
+ else
+ current_currency = send("currency_for_#{name}")
+ if money_currency && send("currency_for_#{name}") != money_currency
@alup
RubyMoney member
alup added a line comment Jun 12, 2013

Use current_currency variable instead of send("currency_for_#{name}")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
@alup
RubyMoney member

Please clean your commits (remove commit about Guardfile and .gitignore - these are not related to this patch).

@semmons99
RubyMoney member

Closed, since updates weren't complete.

@semmons99 semmons99 closed this Nov 28, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment