Skip to content

Commit 91916c1

Browse files
committed
Merge pull request #1062 from magiclabs/remove-buggy-shipping-rate-migration
Simplify buggy tax rate migration
2 parents 415e3b2 + 25d5109 commit 91916c1

File tree

1 file changed

+0
-31
lines changed

1 file changed

+0
-31
lines changed
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,9 @@
11
class RemoveTaxRateFromShippingRate < ActiveRecord::Migration
2-
class Spree::ShippingRate < Spree::Base; end
3-
class Spree::TaxRate < Spree::Base
4-
has_one :calculator, class_name: "Spree::Calculator", as: :calculable, inverse_of: :calculable, dependent: :destroy, autosave: true
5-
def compute_amount(item)
6-
calculator.compute(item)
7-
end
8-
end
9-
102
def up
11-
say_with_time "Pre-calculating taxes for existing shipping rates" do
12-
Spree::ShippingRate.find_each do |shipping_rate|
13-
tax_rate_id = shipping_rate.tax_rate_id
14-
if tax_rate_id
15-
tax_rate = Spree::TaxRate.unscoped.find_by(shipping_rate.tax_rate_id)
16-
shipping_rate.taxes.create(
17-
tax_rate: tax_rate,
18-
amount: tax_rate.compute_amount(shipping_rate)
19-
)
20-
end
21-
end
22-
end
23-
243
remove_column :spree_shipping_rates, :tax_rate_id
254
end
265

276
def down
287
add_reference :spree_shipping_rates, :tax_rate, index: true, foreign_key: true
29-
say_with_time "Attaching a tax rate to shipping rates" do
30-
Spree::ShippingRate.find_each do |shipping_rate|
31-
shipping_taxes = Spree::ShippingRateTax.where(shipping_rate_id: shipping_rate.id)
32-
# We can only use one tax rate, so let's take the biggest.
33-
selected_tax = shipping_taxes.sort_by(&:amount).last
34-
if selected_tax
35-
shipping_rate.update(tax_rate_id: tax_rate_id)
36-
end
37-
end
38-
end
398
end
409
end

0 commit comments

Comments
 (0)