Module: ActiveItem::Associations
- Extended by:
- ActiveSupport::Concern
- Includes:
- ModelLoader
- Included in:
- Base
- Defined in:
- lib/active_item/associations.rb
Overview
Provides has_many and belongs_to association macros with lazy loading, preloading support, and dependent record handling (destroy, nullify, restrict).
Instance Method Summary collapse
Methods included from ModelLoader
Instance Method Details
#check_dependent_associations ⇒ Object
19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 |
# File 'lib/active_item/associations.rb', line 19 def check_dependent_associations self.class._associations.each do |name, config| next unless config[:type] == :has_many && config[:dependent] case config[:dependent] when :restrict_with_exception raise DeleteRestrictionError, name if send(name).limit(1).any? when :restrict_with_error if send(name).limit(1).any? = config[:message] || "Cannot delete #{self.class.name} because dependent #{name} exist" errors.add(:base, ) throw(:abort) end when :destroy send(name).each(&:destroy) when :delete_all send(name).each(&:delete) when :nullify foreign_key = config[:foreign_key] send(name).each { |record| record.update(foreign_key => nil) } end end end |