forked from solidusio/solidus
-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate TaxRate, TaxCategory relation to a M-M
Implements solidusio#1836 The current relation between TaxRate and TaxCategory can lead to unnecessary tax rates if a store has multiple tax categories and if it supports countries (such as Australia) that have a standard tax rate. This means that for each TaxCategory, the store will have an extra tax rate even if the percent is the same. By transforming this relation to a M-M we can allow stores to create tax rates that have multiple tax categories.
- Loading branch information
1 parent
ed0fd82
commit c0f8394
Showing
14 changed files
with
109 additions
and
22 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
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
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
module Spree | ||
class TaxRateTaxCategory < Spree::Base | ||
belongs_to :tax_rate, class_name: Spree::TaxRate, inverse_of: :tax_rate_tax_categories | ||
belongs_to :tax_category, class_name: Spree::TaxCategory, inverse_of: :tax_rate_tax_categories | ||
end | ||
end |
48 changes: 48 additions & 0 deletions
48
core/db/migrate/20170412103617_transform_tax_rate_category_relation.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,48 @@ | ||
class TransformTaxRateCategoryRelation < ActiveRecord::Migration[5.0] | ||
class TaxRate < ActiveRecord::Base | ||
self.table_name = "spree_tax_rates" | ||
end | ||
|
||
class TaxRateTaxCategory < ActiveRecord::Base | ||
self.table_name = "spree_tax_rate_tax_categories" | ||
end | ||
|
||
def up | ||
create_table :spree_tax_rate_tax_categories do |t| | ||
t.integer :tax_category_id, index: true, null: false | ||
t.integer :tax_rate_id, index: true, null: false | ||
end | ||
|
||
add_foreign_key :spree_tax_rate_tax_categories, :spree_tax_categories, column: :tax_category_id | ||
add_foreign_key :spree_tax_rate_tax_categories, :spree_tax_rates, column: :tax_rate_id | ||
|
||
TaxRate.where.not(tax_category_id: nil).find_each do |tax_rate| | ||
TaxRateTaxCategory.create!( | ||
tax_rate_id: tax_rate.id, | ||
tax_category_id: tax_rate.tax_category_id | ||
) | ||
end | ||
|
||
remove_column :spree_tax_rates, :tax_category_id | ||
end | ||
|
||
def down | ||
add_column :spree_tax_rates, :tax_category_id, :integer, index: true | ||
add_foreign_key :spree_tax_rates, :spree_tax_categories, column: :tax_category_id | ||
|
||
TaxRate.find_each do |tax_rate| | ||
tax_category_ids = TaxRateTaxCategory.where(tax_rate_id: tax_rate.id).pluck(:tax_category_id) | ||
|
||
tax_category_ids.each_with_index do |category_id, i| | ||
if i.zero? | ||
tax_rate.update!(tax_category_id: category_id) | ||
else | ||
new_tax_rate = tax_rate.dup | ||
new_tax_rate.update!(tax_category_id: category_id) | ||
end | ||
end | ||
end | ||
|
||
drop_table :spree_tax_rate_tax_categories | ||
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
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