From cef2abc5591270a83d37cdf355e7a5d267281691 Mon Sep 17 00:00:00 2001 From: Martin Meyerhoff Date: Sat, 24 Sep 2016 13:28:49 +0200 Subject: [PATCH] Convert blank country_iso on prices to nil When adding a new price for a product in the admin panel using the prices tab and not selecting a country for this price ("Any Country"), the browser will send an empty string as country iso. In the backend, the intention of this user action is for the price to be saved with a `country_iso` of `nil`, indicating that it is a fallback price valid for any country. This commit converts an empty string `country_iso` to `nil` before validation, allowing admin users to actually create a new price. Fixes https://github.com/solidusio/solidus/issues/1453 --- core/app/models/spree/price.rb | 4 ++++ core/spec/models/spree/price_spec.rb | 15 +++++++++++++++ 2 files changed, 19 insertions(+) diff --git a/core/app/models/spree/price.rb b/core/app/models/spree/price.rb index a1edf40e9f4..395b28a1cb6 100644 --- a/core/app/models/spree/price.rb +++ b/core/app/models/spree/price.rb @@ -58,6 +58,10 @@ def display_country end end + def country_iso=(country_iso) + self[:country_iso] = country_iso.presence + end + private def sum_of_vat_amounts diff --git a/core/spec/models/spree/price_spec.rb b/core/spec/models/spree/price_spec.rb index 2192b0529d6..eacbdcf232a 100644 --- a/core/spec/models/spree/price_spec.rb +++ b/core/spec/models/spree/price_spec.rb @@ -55,6 +55,12 @@ it { is_expected.to be_valid } end + context 'when country iso is an empty string' do + let(:country_iso) { "" } + + it { is_expected.to be_valid } + end + context 'when country iso is a country code' do let!(:country) { create(:country, iso: "DE") } let(:country_iso) { "DE" } @@ -69,6 +75,15 @@ end end + describe "country_iso=" do + let(:price) { Spree::Price.new(country_iso: "de") } + + it "assigns nil if passed nil" do + price.country_iso = nil + expect(price.country_iso).to be_nil + end + end + describe '#country' do let!(:country) { create(:country, iso: "DE") } let(:price) { create(:price, country_iso: "DE", is_default: false) }