diff --git a/core/app/helpers/spree/base_helper.rb b/core/app/helpers/spree/base_helper.rb index 6a0f4b42816..bab090595b0 100644 --- a/core/app/helpers/spree/base_helper.rb +++ b/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={}) diff --git a/core/app/models/spree/zone.rb b/core/app/models/spree/zone.rb index ad5eeaef5da..251e866a68a 100644 --- a/core/app/models/spree/zone.rb +++ b/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' diff --git a/core/spec/helpers/base_helper_spec.rb b/core/spec/helpers/base_helper_spec.rb index e2179822a4f..3f1342bb475 100644 --- a/core/spec/helpers/base_helper_spec.rb +++ b/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") }