Skip to content

Commit

Permalink
Merge pull request #926 from magiclabs/delegate-shipping-rate-tax-cat…
Browse files Browse the repository at this point in the history
…egories

Use `delegate` for finding the tax category of a shipment
  • Loading branch information
jhawthorn committed Feb 26, 2016
2 parents 0970d5d + fb7afcf commit dfb1fda
Show file tree
Hide file tree
Showing 4 changed files with 10 additions and 14 deletions.
6 changes: 2 additions & 4 deletions core/app/models/spree/shipment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ class Shipment < Spree::Base
self.whitelisted_ransackable_associations = ['order']
self.whitelisted_ransackable_attributes = ['number']

delegate :tax_category, to: :selected_shipping_rate, allow_nil: true

def can_transition_from_pending_to_shipped?
!requires_shipment?
end
Expand Down Expand Up @@ -244,10 +246,6 @@ def shipping_method
selected_shipping_rate.try(:shipping_method) || shipping_rates.first.try(:shipping_method)
end

def tax_category
selected_shipping_rate.try(:tax_rate).try(:tax_category)
end

# Only one of either included_tax_total or additional_tax_total is set
# This method returns the total of the two. Saves having to check if
# tax is included or additional.
Expand Down
2 changes: 1 addition & 1 deletion core/app/models/spree/shipping_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class ShippingRate < Spree::Base
belongs_to :tax_rate, -> { with_deleted }, class_name: 'Spree::TaxRate'

delegate :order, :currency, to: :shipment
delegate :name, to: :shipping_method
delegate :name, :tax_category, to: :shipping_method
delegate :code, to: :shipping_method, prefix: true

def display_base_price
Expand Down
11 changes: 7 additions & 4 deletions core/spec/lib/spree/core/unreturned_item_charger_spec.rb
Original file line number Diff line number Diff line change
@@ -1,17 +1,21 @@
require 'spec_helper'

describe Spree::UnreturnedItemCharger do
let(:shipped_order) { create(:shipped_order, line_items_count: 1, with_cartons: false) }
let(:ship_address) { create(:address) }
let(:shipped_order) { create(:shipped_order, ship_address: ship_address, line_items_count: 1, with_cartons: false) }
let(:original_shipment) { shipped_order.shipments.first }
let(:original_stock_location) { original_shipment.stock_location }
let(:original_inventory_unit) { shipped_order.inventory_units.first }
let(:original_variant) { original_inventory_unit.variant }
let(:shipping_method) { create(:shipping_method, tax_category: original_variant.tax_category) }

let(:exchange_shipment) do
create(:shipment,
order: shipped_order,
state: 'shipped',
stock_location: original_stock_location,
created_at: 5.days.ago)
created_at: 5.days.ago,
shipping_method: shipping_method)
end
let(:exchange_inventory_unit) { exchange_shipment.inventory_units.first }
let(:return_item) do
Expand Down Expand Up @@ -43,12 +47,11 @@
end

context 'in tax zone' do
let!(:tax_zone) { Spree::Zone.find_by(name: 'GlobalZone') || FactoryGirl.create(:global_zone) }
let!(:tax_zone) { create(:zone, countries: [ship_address.country]) }
let!(:tax_rate) { create(:tax_rate, zone: tax_zone, tax_category: original_variant.tax_category) }
before { tax_zone.update_attributes!(default_tax: true) }

it "applies tax" do
exchange_shipment.shipping_rates.update_all(tax_rate_id: tax_rate.id)
exchange_order = exchange_shipment.order
exchange_order.create_tax_charge!
exchange_order.update!
Expand Down
5 changes: 0 additions & 5 deletions core/spec/models/spree/tax/taxation_integration_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,6 @@
end

it 'has a shipment with 0.52 included tax' do
pending 'But the tax is not created'
expect(shipment.included_tax_total).to eq(0.52)
end

Expand Down Expand Up @@ -182,7 +181,6 @@
end

it 'has a shipment with 2.55 included tax' do
pending 'But the tax is not created'
expect(shipment.included_tax_total).to eq(2.55)
end

Expand Down Expand Up @@ -264,7 +262,6 @@
end

it 'has a shipment with 0.52 included tax' do
pending 'But the tax is not created'
expect(shipment.included_tax_total).to eq(0.52)
end

Expand Down Expand Up @@ -304,7 +301,6 @@
end

it 'has a shipment with 2.55 included tax' do
pending 'But the tax is not created'
expect(shipment.included_tax_total).to eq(2.55)
end

Expand Down Expand Up @@ -770,7 +766,6 @@
end

it 'has a shipment with additional tax of 0.40' do
pending "This does not work because the shipping code does not use contains? for finding zones"
expect(shipment.additional_tax_total).to eq(0.40)
end

Expand Down

0 comments on commit dfb1fda

Please sign in to comment.