Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Zone#states and Zone#countries associations #529

Merged
merged 3 commits into from Nov 24, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions core/app/models/spree/zone.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,11 @@ class Zone < Spree::Base
has_many :zone_members, dependent: :destroy, class_name: "Spree::ZoneMember", inverse_of: :zone
has_many :tax_rates, dependent: :destroy, inverse_of: :zone

with_options through: :zone_members, source: :zoneable do
has_many :countries, source_type: "Spree::Country"
has_many :states, source_type: "Spree::State"
end

has_many :shipping_method_zones
has_many :shipping_methods, through: :shipping_method_zones

Expand Down
4 changes: 4 additions & 0 deletions core/lib/spree/testing_support/factories/zone_factory.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,9 @@
factory :zone, class: Spree::Zone do
name { generate(:random_string) }
description { generate(:random_string) }

trait :with_country do
countries { [create(:country)] }
end
end
end
25 changes: 25 additions & 0 deletions core/spec/models/spree/zone_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -302,4 +302,29 @@
end
end
end

context "state and country associations" do
let!(:country) { create(:country) }

context "has countries associated" do
let!(:zone) do
create(:zone, countries: [country])
end

it "can access associated countries" do
expect(zone.countries).to eq([country])
end
end

context "has states associated" do
let!(:state) { create(:state, country: country) }
let!(:zone) do
create(:zone, states: [state])
end

it "can access associated states" do
expect(zone.states).to eq([state])
end
end
end
end