Skip to content
Browse files

Failsafe to returning all countries if the checkout_zone kind is not …

…a country type.

Also, rename Spree::Zone#country_list to zone_member_list

[Fixes #1584]
  • Loading branch information...
1 parent 481d12f commit 36333e85917f5f2e59b9c24e3f39336d2df56e98 @joneslee85 joneslee85 committed with radar May 27, 2012
Showing with 59 additions and 7 deletions.
  1. +12 −5 core/app/helpers/spree/base_helper.rb
  2. +2 −2 core/app/models/spree/zone.rb
  3. +45 −0 core/spec/helpers/base_helper_spec.rb
View
17 core/app/helpers/spree/base_helper.rb
@@ -122,11 +122,18 @@ def taxons_tree(root_taxon, current_taxon, max_level = 1)
end
def available_countries
- countries = Zone.find_by_name(Spree::Config[:checkout_zone]).try(:country_list) || Country.all
- countries.collect do |c|
- c.name = I18n.t(c.iso, :scope => 'countries', :default => c.name)
- c
- end.sort{ |a,b| a.name <=> b.name }
+ checkout_zone = Zone.find_by_name(Spree::Config[:checkout_zone])
+
+ if checkout_zone && checkout_zone.kind == 'country'
+ countries = checkout_zone.zone_member_list
+ else
+ countries = Country.all
+ end
+
+ countries.collect do |country|
+ country.name = I18n.t(country.iso, :scope => 'countries', :default => country.name)
+ country
+ end.sort { |a, b| a.name <=> b.name }
end
def format_price(price, options={})
View
4 core/app/models/spree/zone.rb
@@ -56,8 +56,8 @@ def self.match(address)
matches.first
end
- # convenience method for returning the countries contained within a zone
- def country_list
+ # convenience method for returning the countries or states contained within a zone
+ def zone_member_list
members.map { |zone_member|
case zone_member.zoneable_type
when 'Spree::Country'
View
45 core/spec/helpers/base_helper_spec.rb
@@ -3,6 +3,51 @@
describe Spree::BaseHelper do
include Spree::BaseHelper
+ context "available_countries" do
+ let(:country) { create(:country) }
+
+ before do
+ 3.times { create(:country) }
+ end
+
+ context "with no checkout zone defined" do
+ before do
+ Spree::Config[:checkout_zone] = nil
+ end
+
+ it "return complete list of countries" do
+ available_countries.count.should == Spree::Country.count
+ end
+ end
+
+ context "with a checkout zone defined" do
+ context "checkout zone is of type country" do
+ before do
+ @country_zone = create(:zone, :name => "CountryZone")
+ @country_zone.members.create(:zoneable => country)
+ Spree::Config[:checkout_zone] = @country_zone.name
+ end
+
+ it "return only the countries defined by the checkout zone" do
+ available_countries.should == [country]
+ end
+ end
+
+ context "checkout zone is of type state" do
+ before do
+ state_zone = create(:zone, :name => "StateZone")
+ state = create(:state, :country => country)
+ state_zone.members.create(:zoneable => state)
+ Spree::Config[:checkout_zone] = state_zone.name
+ end
+
+ it "return complete list of countries" do
+ available_countries.count.should == Spree::Country.count
+ end
+ end
+ end
+ end
+
# Regression test for #889
context "seo_url" do
let(:taxon) { stub(:permalink => "bam") }

0 comments on commit 36333e8

Please sign in to comment.
Something went wrong with that request. Please try again.