class DataMapper::Associations::OneToMany::Collection
Attributes
@api private
@api private
Public Instance Methods
Removes all Resources from the 1:m Collection
This should remove and orphan each Resource from the 1:m Collection.
@return [Collection]
self
@api public
# File lib/dm-core/associations/one_to_many.rb, line 214 def clear lazy_load # lazy load so that targets are always orphaned super end
Remove every Resource in the 1:m Collection from the repository
This performs a deletion of each Resource in the Collection from the repository and clears the Collection.
@return [Boolean]
true if the resources were successfully destroyed
@api public
# File lib/dm-core/associations/one_to_many.rb, line 256 def destroy assert_source_saved 'The source must be saved before mass-deleting the collection' super end
Remove every Resource in the 1: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.
@return [Boolean]
true if the resources were successfully destroyed
@api public
# File lib/dm-core/associations/one_to_many.rb, line 271 def destroy! assert_source_saved 'The source must be saved before mass-deleting the collection' super end
@api public
# File lib/dm-core/associations/one_to_many.rb, line 187 def reload(*) assert_source_saved 'The source must be saved before reloading the collection' super end
Replace the Resources within the 1:m Collection
@param [Enumerable] other
List of other Resources to replace with
@return [Collection]
self
@api public
# File lib/dm-core/associations/one_to_many.rb, line 201 def replace(*) lazy_load # lazy load so that targets are always orphaned super end
Update every Resource in the 1:m Collection
@param [Hash] attributes
attributes to update with
@return [Boolean]
true if the resources were successfully updated
@api public
# File lib/dm-core/associations/one_to_many.rb, line 228 def update(*) assert_source_saved 'The source must be saved before mass-updating the collection' super end
Update every Resource in the 1:m Collection, bypassing validation
@param [Hash] attributes
attributes to update
@return [Boolean]
true if the resources were successfully updated
@api public
# File lib/dm-core/associations/one_to_many.rb, line 242 def update!(*) assert_source_saved 'The source must be saved before mass-updating the collection' super end
Private Instance Methods
@api private
# File lib/dm-core/associations/one_to_many.rb, line 279 def _create(*) assert_source_saved 'The source must be saved before creating a resource' super end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 285 def _save(execute_hooks = true) assert_source_saved 'The source must be saved before saving the collection' # update removed resources to not reference the source @removed.all? { |resource| resource.destroyed? || resource.__send__(execute_hooks ? :save : :save!) } && super end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 337 def assert_source_saved(message) unless source.saved? raise UnsavedParentError, message end end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 330 def inverse_set(source, target) unless source.readonly? relationship.inverse.set(source, target) end end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 293 def lazy_load if source.saved? super end end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 300 def new_collection(query, resources = nil, &block) collection = self.class.new(query, &block) collection.relationship = relationship collection.source = source resources ||= filter(query) if loaded? # set the resources after the relationship and source are set if resources collection.set(resources) end collection end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 317 def resource_added(resource) resource = initialize_resource(resource) inverse_set(resource, source) super end
@api private
# File lib/dm-core/associations/one_to_many.rb, line 324 def resource_removed(resource) inverse_set(resource, nil) super end