Module: Sequel::Plugins::AssociationDependencies

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

Overview

The AssociationDependencies plugin allows you do easily set up before and/or after destroy hooks for destroying, deleting, or nullifying associated model objects. The following association types support the following dependency actions:

  • :many_to_many - :nullify (removes all related entries in join table)

  • :many_to_one - :delete, :destroy

  • :one_to_many - :delete, :destroy, :nullify (sets foreign key to NULL for all associated objects)

This plugin works directly with the association datasets and does not use any cached association values. The :delete action will delete all associated objects from the database in a single SQL call. The :destroy action will load each associated object from the database and call the destroy method on it.

To set up an association dependency, you must provide a hash with association name symbols and dependency action values. You can provide the hash to the plugin call itself or to the add_association_dependencies method:

Business.plugin :association_dependencies, :address=>delete
# or:
Artist.plugin :association_dependencies
Artist.add_association_dependencies :albums=>:destroy, :reviews=>:delete, :tags=>:nullify

Defined Under Namespace

Modules: ClassMethods, InstanceMethods

Constant Summary collapse

ASSOCIATION_MAPPING =

Mapping of association types to when the dependency calls should be made (either :before for in before_destroy or :after for in after_destroy)

{:one_to_many=>:before, :many_to_one=>:after, :many_to_many=>:before}
DEPENDENCE_ACTIONS =

The valid dependence actions

[:delete, :destroy, :nullify]

Class Method Summary collapse

Class Method Details

.apply(model, hash = {}) ⇒ Object

Initialize the association_dependencies hash for this model.


32
33
34
# File 'lib/sequel/lib/sequel/plugins/association_dependencies.rb', line 32

def self.apply(model, hash={})
  model.instance_eval{@association_dependencies = {:before_delete=>[], :before_destroy=>[], :before_nullify=>[], :after_delete=>[], :after_destroy=>[]}}
end

.configure(model, hash = {}) ⇒ Object

Call add_association_dependencies with any dependencies given in the plugin call.


37
38
39
# File 'lib/sequel/lib/sequel/plugins/association_dependencies.rb', line 37

def self.configure(model, hash={})
  model.add_association_dependencies(hash) unless hash.empty?
end