Class: DataMapper::Associations::ManyToMany::Collection

Inherits:
OneToMany::Collection show all
Defined in:
lib/dm-core/associations/many_to_many.rb

Overview

class Relationship

Instance Attribute Summary

Attributes inherited from OneToMany::Collection

#relationship, #source

Attributes inherited from Collection

#query

Attributes inherited from LazyArray

#head, #tail

Instance Method Summary collapse

Methods inherited from OneToMany::Collection

#clear, #reload, #replace, #update, #update!

Methods inherited from Collection

#<<, #[], #[]=, #all, #at, #clean?, #clear, #collect!, #concat, #create, #create!, #delete, #delete_at, #delete_if, #difference, #dirty?, #each, #first, #first_or_create, #first_or_new, #get, #get!, #hash, #insert, #inspect, #intersection, #last, #model, #new, #pop, #push, #reject!, #reload, #replace, #repository, #respond_to?, #reverse, #reverse!, #save, #save!, #set, #shift, #slice!, #union, #unshift, #update, #update!

Methods included from Deprecate

#deprecate

Methods inherited from LazyArray

#<<, #==, #[], #[]=, #any?, #at, #clear, #concat, #delete_at, #delete_if, #empty?, #eql?, #fetch, #first, #freeze, #frozen?, #include?, #index, #insert, #kind_of?, #last, #lazy_possible?, #load_with, #loaded?, #pop, #push, #replace, #respond_to?, #reverse, #reverse!, #shift, #slice!, #to_a, #unshift, #values_at

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class DataMapper::Collection

Instance Method Details

#destroyBoolean

Remove every Resource in the m:m Collection from the repository

This performs a deletion of each Resource in the Collection from the repository and clears the Collection.

Returns:

  • (Boolean)

    true if the resources were successfully destroyed



324
325
326
327
328
329
330
331
332
333
334
335
336
# File 'lib/dm-core/associations/many_to_many.rb', line 324

def destroy
  assert_source_saved 'The source must be saved before mass-deleting the collection'

  # make sure the records are loaded so they can be found when
  # the intermediaries are removed
  lazy_load

  unless intermediaries.all(via => self).destroy
    return false
  end

  super
end

#destroy!Boolean

Remove every Resource in the m:m Collection from the repository, bypassing validation

This performs a deletion of each Resource in the Collection from the repository and clears the Collection while skipping validation.

Returns:

  • (Boolean)

    true if the resources were successfully destroyed



348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
# File 'lib/dm-core/associations/many_to_many.rb', line 348

def destroy!
  assert_source_saved 'The source must be saved before mass-deleting the collection'

  model      = self.model
  key        = model.key(repository_name)
  conditions = Query.target_conditions(self, key, key)

  unless intermediaries.all(via => self).destroy!
    return false
  end

  unless model.all(:repository => repository, :conditions => conditions).destroy!
    return false
  end

  each do |resource|
    resource.persisted_state = Resource::State::Immutable.new(resource)
  end

  clear

  true
end

#intermediariesCollection

Return the intermediaries linking the source to the targets

Returns:



378
379
380
381
382
383
384
385
386
387
# File 'lib/dm-core/associations/many_to_many.rb', line 378

def intermediaries
  through = self.through
  source  = self.source

  @intermediaries ||= if through.loaded?(source)
    through.get_collection(source)
  else
    reset_intermediaries
  end
end