Skip to content

Commit

Permalink
Merge pull request #6552 from Matt-Yorkley/adjustments-order-association
Browse files Browse the repository at this point in the history
[Adjustments] Associate all adjustments with an order
  • Loading branch information
mkllnk authored Jan 18, 2021
2 parents 473ac82 + 1eb08ba commit 334e270
Show file tree
Hide file tree
Showing 6 changed files with 48 additions and 1 deletion.
2 changes: 2 additions & 0 deletions app/models/spree/adjustment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ class Adjustment < ActiveRecord::Base
belongs_to :adjustable, polymorphic: true
belongs_to :source, polymorphic: true
belongs_to :originator, polymorphic: true
belongs_to :order, class_name: "Spree::Order"

belongs_to :tax_rate, -> { where spree_adjustments: { originator_type: 'Spree::TaxRate' } },
foreign_key: 'originator_id'

Expand Down
1 change: 1 addition & 0 deletions app/models/spree/tax_rate.rb
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ def adjust(order)
amount: amount,
source: order,
originator: self,
order: order,
state: "closed",
label: label
)
Expand Down
26 changes: 26 additions & 0 deletions db/migrate/20201219120055_add_order_to_adjustments.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
class AddOrderToAdjustments < ActiveRecord::Migration
def up
class Spree::Adjustment < ActiveRecord::Base
belongs_to :adjustable, polymorphic: true
belongs_to :order, class_name: "Spree::Order"
end
class Spree::LineItem < ActiveRecord::Base
belongs_to :order, class_name: "Spree::Order"
end

add_column :spree_adjustments, :order_id, :integer

# Ensure migration can use the new column
Spree::Adjustment.reset_column_information

# Migrate adjustments on orders
Spree::Adjustment.where(order_id: nil, adjustable_type: "Spree::Order").find_each do |adjustment|
adjustment.update_column(:order_id, adjustment.adjustable_id)
end

# Migrate adjustments on line_items
Spree::Adjustment.where(order_id: nil, adjustable_type: "Spree::LineItem").includes(:adjustable).find_each do |adjustment|
adjustment.update_column(:order_id, adjustment.adjustable.order_id)
end
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20201113163227) do
ActiveRecord::Schema.define(version: 20201219120055) do

# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
Expand Down Expand Up @@ -393,6 +393,7 @@
t.string "adjustable_type", limit: 255
t.decimal "included_tax", precision: 10, scale: 2, default: 0.0, null: false
t.string "state", limit: 255
t.integer "order_id"
end

add_index "spree_adjustments", ["adjustable_id"], name: "index_adjustments_on_order_id", using: :btree
Expand Down
12 changes: 12 additions & 0 deletions lib/spree/core/calculated_adjustments.rb
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ def create_adjustment(label, target, calculable, mandatory = false, state = "clo
amount: amount,
source: old_calculable,
originator: self,
order: order_object_for(target),
label: label,
mandatory: mandatory,
state: state
Expand Down Expand Up @@ -74,6 +75,17 @@ def self.spree_calculators
Rails.application.config.spree.calculators
end
private_class_method :spree_calculators

private

def order_object_for(target)
# Temporary method for adjustments transition.
if target.is_a? Spree::Order
target
elsif target.respond_to?(:order)
target.order
end
end
end
end
end
Expand Down
5 changes: 5 additions & 0 deletions spec/lib/spree/core/calculated_adjustments_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@
tax_rate.create_adjustment("foo", target, order)
end

it "should be associated with the order" do
tax_rate.create_adjustment("foo", target, order)
expect(target.adjustments.first.order_id).to eq order.id
end

it "should have the correct originator and an amount derived from the calculator and supplied calculable" do
adjustment = tax_rate.create_adjustment("foo", target, order)
expect(adjustment).not_to be_nil
Expand Down

0 comments on commit 334e270

Please sign in to comment.