Skip to content

Commit

Permalink
Dependent destroy demo app models. (#739)
Browse files Browse the repository at this point in the history
This fixes #738, where foreign key constrains were violated trying out
the deletion in the demo app.
  • Loading branch information
nickcharlton authored and BenMorganIO committed Jan 20, 2017
1 parent 17cd60e commit e2a9282
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 2 deletions.
2 changes: 1 addition & 1 deletion spec/example_app/app/models/customer.rb
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
class Customer < ActiveRecord::Base
has_many :orders
has_many :orders, dependent: :destroy

validates :name, presence: true
validates :email, presence: true
Expand Down
2 changes: 1 addition & 1 deletion spec/example_app/app/models/order.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ class Order < ActiveRecord::Base
belongs_to :customer

validates :customer, presence: true
has_many :line_items
has_many :line_items, dependent: :destroy

validates :address_line_one, presence: true
validates :address_line_two, presence: true
Expand Down
9 changes: 9 additions & 0 deletions spec/models/customer_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,15 @@
it { should validate_presence_of(:name) }
it { should validate_presence_of(:email) }

it "deletes associated orders when deleted itself" do
customer = create(:customer)
create(:order, customer: customer)

puts customer.destroy

expect(Order.all).to be_empty
end

describe "#lifetime_value" do
it "returns 0 for no orders" do
customer = Customer.new
Expand Down
11 changes: 11 additions & 0 deletions spec/models/order_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,17 @@
it { should validate_presence_of(:address_zip) }
end

it "deletes associated line_items when deleted itself" do
product = create(:product)
customer = create(:customer)
order = create(:order, customer: customer)
create(:line_item, product: product, order: order)

order.destroy

expect(LineItem.all).to be_empty
end

describe "#total_price" do
it "returns 0 when there are no line items" do
order = build(:order)
Expand Down

0 comments on commit e2a9282

Please sign in to comment.