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

ensure no inventory unit present before destroy any variant #147

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
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
10 changes: 10 additions & 0 deletions app/models/spree/variant_decorator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,21 @@
has_many :assemblies, through: :assemblies_variants, class_name: "Spree::Variant", dependent: :destroy
has_many :parts, through: :parts_variants, class_name: "Spree::Variant", dependent: :destroy

before_destroy :ensure_no_inventory_units

def assemblies_for(products)
assemblies.where(id: products)
end

def part?
assemblies.exists?
end

private
def ensure_no_inventory_units
if inventory_units.any?
errors.add(:base, Spree.t(:cannot_destroy_if_attached_to_inventory_units))
return false
end
end
end
1 change: 1 addition & 0 deletions config/locales/en.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ en:
spree:
available_parts: Available parts
can_be_part: Can be part
cannot_destroy_if_attached_to_inventory_units: Cannot delete variants once they are attached to inventory unit.
individual_sale: Individual sale
no_variants: No variants
parts: Parts
Expand Down