| Module | Sequel::Plugins::AssociationDependencies::ClassMethods |
| In: |
lib/sequel/plugins/association_dependencies.rb
|
| association_dependencies | [R] | 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. |
Add association dependencies to this model. The hash should have association name symbol keys and dependency action symbol values (e.g. :albums=>:destroy).
# File lib/sequel/plugins/association_dependencies.rb, line 52
52: def add_association_dependencies(hash)
53: hash.each do |association, action|
54: raise(Error, "Nonexistent association: #{association}") unless r = association_reflection(association)
55: type = r[:type]
56: raise(Error, "Invalid dependence action type: association: #{association}, dependence action: #{action}") unless DEPENDENCE_ACTIONS.include?(action)
57: raise(Error, "Invalid association type: association: #{association}, type: #{type}") unless time = ASSOCIATION_MAPPING[type]
58: association_dependencies["#{time}_#{action}""#{time}_#{action}"] << if action == :nullify
59: case type
60: when :one_to_many , :many_to_many
61: proc{send(r.remove_all_method)}
62: when :one_to_one
63: proc{send(r.setter_method, nil)}
64: else
65: raise(Error, "Can't nullify many_to_one associated objects: association: #{association}")
66: end
67: else
68: raise(Error, "Can only nullify many_to_many associations: association: #{association}") if type == :many_to_many
69: r.dataset_method
70: end
71: end
72: end
Copy the current model object‘s association_dependencies into the subclass.
# File lib/sequel/plugins/association_dependencies.rb, line 75
75: def inherited(subclass)
76: super
77: ad = association_dependencies.dup
78: ad.keys.each{|k| ad[k] = ad[k].dup}
79: subclass.instance_variable_set(:@association_dependencies, ad)
80: end