-
-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Remove code that relates to Spree::ShippingRate#tax_rate
Since now we have the persisted Spree::ShippingRate#taxes, we do not need to store one of the tax rates anymore on the shipping rate. The data migration contains three lines of code from tax_rate.rb so that the migration still runs in the future.
- Loading branch information
Showing
5 changed files
with
42 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
40 changes: 40 additions & 0 deletions
40
core/db/migrate/20160225152313_remove_tax_rate_from_shipping_rate.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
class RemoveTaxRateFromShippingRate < ActiveRecord::Migration | ||
class Spree::ShippingRate < Spree::Base; end | ||
class Spree::TaxRate < Spree::Base | ||
has_one :calculator, class_name: "Spree::Calculator", as: :calculable, inverse_of: :calculable, dependent: :destroy, autosave: true | ||
def compute_amount(item) | ||
calculator.compute(item) | ||
end | ||
end | ||
|
||
def up | ||
say_with_time "Pre-calculating taxes for existing shipping rates" do | ||
Spree::ShippingRate.find_each do |shipping_rate| | ||
tax_rate_id = shipping_rate.tax_rate_id | ||
if tax_rate_id | ||
tax_rate = Spree::TaxRate.unscoped.find_by(shipping_rate.tax_rate_id) | ||
shipping_rate.taxes.create( | ||
tax_rate: tax_rate, | ||
amount: tax_rate.compute_amount(shipping_rate) | ||
) | ||
end | ||
end | ||
end | ||
|
||
remove_column :spree_shipping_rates, :tax_rate_id | ||
end | ||
|
||
def down | ||
add_reference :spree_shipping_rates, :tax_rate, index: true, foreign_key: true | ||
say_with_time "Attaching a tax rate to shipping rates" do | ||
Spree::ShippingRate.find_each do |shipping_rate| | ||
shipping_taxes = Spree::ShippingRateTax.where(shipping_rate_id: shipping_rate.id) | ||
# We can only use one tax rate, so let's take the biggest. | ||
selected_tax = shipping_taxes.sort_by(&:amount).last | ||
if selected_tax | ||
shipping_rate.update(tax_rate_id: tax_rate_id) | ||
end | ||
end | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters