Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dependent destroy demo app models. #739

Merged
merged 1 commit into from
Jan 20, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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