Module: Sequel::Plugins::AssociationDependencies::ClassMethods

Defined in:
lib/sequel/lib/sequel/plugins/association_dependencies.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#association_dependenciesObject (readonly)

A hash specifying the association dependencies for each model. The keys are symbols indicating the type of action and when it should be executed (e.g. :before_delete). Values are an array of method symbols. For before_nullify, the symbols are remove_all_association methods. For other types, the symbols are association_dataset methods, on which delete or destroy is called.


48
49
50
# File 'lib/sequel/lib/sequel/plugins/association_dependencies.rb', line 48

def association_dependencies
  @association_dependencies
end

Instance Method Details

#add_association_dependencies(hash) ⇒ Object

Add association dependencies to this model. The hash should have association name symbol keys and dependency action symbol values (e.g. :albums=>:destroy).


52
53
54
55
56
57
58
59
60
61
62
63
64
65
# File 'lib/sequel/lib/sequel/plugins/association_dependencies.rb', line 52

def add_association_dependencies(hash)
  hash.each do |association, action|
    raise(Error, "Nonexistent association: #{association}") unless r = association_reflection(association)
    raise(Error, "Invalid dependence action type: association: #{association}, dependence action: #{action}") unless DEPENDENCE_ACTIONS.include?(action)
    raise(Error, "Invalid association type: association: #{association}, type: #{r[:type]}") unless time = ASSOCIATION_MAPPING[r[:type]]
    association_dependencies[:"#{time}_#{action}"] << if action == :nullify
      raise(Error, "Can't nullify many_to_one associated objects: association: #{association}") if r[:type] == :many_to_one
      r.remove_all_method
    else
      raise(Error, "Can only nullify many_to_many associations: association: #{association}") if r[:type] == :many_to_many
      r.dataset_method
    end
  end
end

#inherited(subclass) ⇒ Object

Copy the current model object's association_dependencies into the subclass.


68
69
70
71
72
73
# File 'lib/sequel/lib/sequel/plugins/association_dependencies.rb', line 68

def inherited(subclass)
  super
  ad = association_dependencies.dup
  ad.keys.each{|k| ad[k] = ad[k].dup}
  subclass.instance_variable_set(:@association_dependencies, ad)
end