Skip to content

Commit

Permalink
Merge pull request #1169 from magiclabs/validate-price-currency
Browse files Browse the repository at this point in the history
Validate currency on Spree::Price
  • Loading branch information
mamhoff committed May 20, 2016
2 parents 15c5f4c + 7274cd7 commit 821c362
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 0 deletions.
2 changes: 2 additions & 0 deletions core/app/models/spree/price.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ class Price < Spree::Base
less_than_or_equal_to: MAXIMUM_AMOUNT
}

validates :currency, inclusion: { in: ::Money::Currency.all.map(&:iso_code), message: :invalid_code }

scope :currently_valid, -> { where(is_default: true) }
scope :with_default_attributes, -> { where(Spree::Config.default_pricing_options.desired_attributes) }
after_save :set_default_price
Expand Down
4 changes: 4 additions & 0 deletions core/config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,10 @@ en:
attributes:
currency:
must_match_order_currency: "Must match order currency"
spree/price:
attributes:
currency:
invalid_code: "is not a valid currency code"
spree/promotion:
attributes:
apply_automatically:
Expand Down
24 changes: 24 additions & 0 deletions core/spec/models/spree/price_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,4 +39,28 @@
it { is_expected.to be_valid }
end
end

describe "#currency" do
let(:variant) { stub_model Spree::Variant }
subject { Spree::Price.new variant: variant, amount: 10, currency: currency }

describe "validation" do
context "with an invalid currency" do
let(:currency) { "XYZ" }

it { is_expected.to be_invalid }

it "has an understandable error message" do
subject.valid?
expect(subject.errors.messages[:currency].first).to eq("is not a valid currency code")
end
end

context "with a valid currency" do
let(:currency) { "USD" }

it { is_expected.to be_valid }
end
end
end
end

0 comments on commit 821c362

Please sign in to comment.