Skip to content

Commit

Permalink
Failsafe to returning all countries if the checkout_zone kind is not …
Browse files Browse the repository at this point in the history
…a country type.

Also, rename Spree::Zone#country_list to zone_member_list

[Fixes #1584]
  • Loading branch information
Trung Lê authored and radar committed Jun 6, 2012
1 parent 481d12f commit 36333e8
Show file tree
Hide file tree
Showing 3 changed files with 59 additions and 7 deletions.
17 changes: 12 additions & 5 deletions core/app/helpers/spree/base_helper.rb
Expand Up @@ -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={})
Expand Down
4 changes: 2 additions & 2 deletions core/app/models/spree/zone.rb
Expand Up @@ -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'
Expand Down
45 changes: 45 additions & 0 deletions core/spec/helpers/base_helper_spec.rb
Expand Up @@ -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") }
Expand Down

0 comments on commit 36333e8

Please sign in to comment.