Skip to content
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
4 changes: 2 additions & 2 deletions app/models/shopkeeper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ class Shopkeeper < ApplicationRecord
has_many :shops, through: :accounts
has_many :item_tags, through: :shops
has_many :created_shops, class_name: "Shop", foreign_key: :created_by_id, inverse_of: :created_by
has_many :created_item_tags, class_name: "ItemTag", foreign_key: :created_by_id, inverse_of: :created_by
has_many :completed_item_tags, class_name: "ItemTag", foreign_key: :completed_by_id, inverse_of: :completed_by
has_many :created_item_tags, class_name: "ItemTag", foreign_key: :created_by_id, inverse_of: :created_by, dependent: :nullify
has_many :completed_item_tags, class_name: "ItemTag", foreign_key: :completed_by_id, inverse_of: :completed_by, dependent: :nullify

attribute :token, :string
attribute :client, :string
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
class ChangeShopkeeperForeignKeysToNullifyOnDelete < ActiveRecord::Migration[8.1]
def up
remove_foreign_key :item_tags, column: :completed_by_id
remove_foreign_key :item_tags, column: :created_by_id

add_foreign_key :item_tags, :shopkeepers, column: :completed_by_id, on_delete: :nullify
add_foreign_key :item_tags, :shopkeepers, column: :created_by_id, on_delete: :nullify
end

def down
remove_foreign_key :item_tags, column: :completed_by_id
remove_foreign_key :item_tags, column: :created_by_id

add_foreign_key :item_tags, :shopkeepers, column: :completed_by_id
add_foreign_key :item_tags, :shopkeepers, column: :created_by_id
end
end
128 changes: 64 additions & 64 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

15 changes: 15 additions & 0 deletions test/controllers/shopkeeper_auth/registrations_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,21 @@ class ShopkeeperAuth::RegistrationsControllerTest < ActionDispatch::IntegrationT
end
end

test "delete current shopkeeper with item_tags" do
shopkeeper.create_default_account
account = shopkeeper.accounts.first

ActsAsTenant.with_tenant(account) do
shop = account.shops.create!(name: "Test Shop", created_by: shopkeeper)
shop.item_tags.create!(queue_number: "A1", account: account, completed_by: shopkeeper)
end

assert_difference "Shopkeeper.count", -1 do
delete shopkeeper_registration_url, headers: shopkeeper.create_new_auth_token
assert_response :success
end
end

def shopkeeper
@shopkeeper ||= shopkeepers(:one)
end
Expand Down
55 changes: 55 additions & 0 deletions test/models/shopkeeper_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -213,4 +213,59 @@ class ShopkeeperTest < ActiveSupport::TestCase
shopkeeper.destroy
end
end

test "should nullify item_tags references on destroy" do
shopkeeper = shopkeepers(:one)
shopkeeper.create_default_account
other_shopkeeper = shopkeepers(:two)
other_shopkeeper.create_default_account
other_account = other_shopkeeper.accounts.first

item_tag = ActsAsTenant.with_tenant(other_account) do
shop = other_account.shops.create!(name: "Other Shop", created_by: other_shopkeeper)
shop.item_tags.create!(
queue_number: "A1",
account: other_account,
completed_by: shopkeeper
)
end

ActsAsTenant.without_tenant do
shopkeeper.destroy
end

ActsAsTenant.with_tenant(other_account) do
item_tag.reload
assert_nil item_tag.completed_by_id
assert ItemTag.exists?(item_tag.id)
end
end

test "should successfully destroy shopkeeper with item_tags in other accounts" do
shopkeeper = shopkeepers(:one)
shopkeeper.create_default_account
other_shopkeeper = shopkeepers(:two)
other_shopkeeper.create_default_account
other_account = other_shopkeeper.accounts.first

item_tag = ActsAsTenant.with_tenant(other_account) do
shop = other_account.shops.create!(name: "Other Shop", created_by: other_shopkeeper)
shop.item_tags.create!(
queue_number: "B1",
account: other_account,
created_by: shopkeeper
)
end

ActsAsTenant.without_tenant do
assert_nothing_raised do
shopkeeper.destroy!
end
end

ActsAsTenant.with_tenant(other_account) do
item_tag.reload
assert_nil item_tag.created_by_id
end
end
end