Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP

Loading…

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
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 Owner
alup added a note

"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
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 Owner
alup added a note

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
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 Owner
alup added a note

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
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 Owner
alup added a note

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
Owner

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

@semmons99
Owner

Closed, since updates weren't complete.

@semmons99 semmons99 closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Mar 28, 2013
  1. Add Guard for specs

    Danil Pismenny authored
  2. Raise error when trying to change model based currency

    Danil Pismenny authored
This page is out of date. Refresh to see the latest.
View
2  .gitignore
@@ -50,3 +50,5 @@ tmtags
## PROJECT::SPECIFIC
.rbx
+
+/tmp
View
7 Gemfile
@@ -22,3 +22,10 @@ platform :mri do
gem 'debugger'
end
end
+
+group :development do
+ gem 'rb-inotify', '~> 0.9'
+ gem 'guard'
+ gem 'guard-rspec'
+ gem 'guard-rails'
+end
View
24 Guardfile
@@ -0,0 +1,24 @@
+# A sample Guardfile
+# More info at https://github.com/guard/guard#readme
+
+guard 'rspec' do
+ watch(%r{^spec/.+_spec\.rb$})
+ watch(%r{^lib/(.+)\.rb$}) { |m| "spec/lib/#{m[1]}_spec.rb" }
+ watch('spec/spec_helper.rb') { "spec" }
+
+ # Rails example
+ watch(%r{^app/(.+)\.rb$}) { |m| "spec/#{m[1]}_spec.rb" }
+ watch(%r{^app/(.*)(\.erb|\.haml)$}) { |m| "spec/#{m[1]}#{m[2]}_spec.rb" }
+ watch(%r{^app/controllers/(.+)_(controller)\.rb$}) { |m| ["spec/routing/#{m[1]}_routing_spec.rb", "spec/#{m[2]}s/#{m[1]}_#{m[2]}_spec.rb", "spec/acceptance/#{m[1]}_spec.rb"] }
+ watch(%r{^spec/support/(.+)\.rb$}) { "spec" }
+ watch('config/routes.rb') { "spec/routing" }
+ watch('app/controllers/application_controller.rb') { "spec/controllers" }
+
+ # Capybara features specs
+ watch(%r{^app/views/(.+)/.*\.(erb|haml)$}) { |m| "spec/features/#{m[1]}_spec.rb" }
+
+ # Turnip features and steps
+ watch(%r{^spec/acceptance/(.+)\.feature$})
+ watch(%r{^spec/acceptance/steps/(.+)_steps\.rb$}) { |m| Dir[File.join("**/#{m[1]}.feature")][0] || 'spec/acceptance' }
+end
+
View
12 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 Owner
alup added a note

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
+ raise "Can't change readonly currenc currency '#{current_currency}' to '#{money_currency}' for field '#{name}'"
@alup Owner
alup added a note

Fix typo here.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ end
+ end
instance_variable_set "@#{name}", money
end
View
24 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 Owner
alup added a note

"raise" instead of "rise"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ product = Product.new
+ expect {
+ product.price Money.new(10,'RUB')
+ }.to raise_error
+ end
+
it "respects :as argument" do
@product.discount_value.should == Money.new(150, "USD")
end
@@ -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 Owner
alup added a note

Please add an empty line here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
+ product = Product.create!(
+ :price => Money.new(10,'USD'),
+ :bonus => Money.new(10,'GBP'),
+ :discount => 10,
+ :sale_price_amount => 1234,
+ :sale_price_currency_code => 'USD')
+
+ product.sale_price.currency_as_string.should == 'USD'
+
+ product.sale_price = Money.new 456, 'CAD'
+ product.save
+ product.reload
+
+ product.sale_price.currency_as_string.should == 'CAD'
+ product.discount_value.currency_as_string.should == 'USD'
+ end
end
context "for model with currency column:" do
Something went wrong with that request. Please try again.