Class: DuckRecord::Associations::CollectionAssociation

Inherits:
Association
  • Object
show all
Defined in:
lib/duck_record/associations/collection_association.rb

Overview

Active Record Association Collection

CollectionAssociation is an abstract class that provides common stuff to ease the implementation of association proxies that represent collections. See the class hierarchy in Association.

CollectionAssociation:
  HasManyAssociation => has_many
    HasManyThroughAssociation + ThroughAssociation => has_many :through

The CollectionAssociation class provides common methods to the collections defined by has_and_belongs_to_many, has_many or has_many with the :through association option.

You need to be careful with assumptions regarding the target: The proxy does not fetch records from the database until it needs them, but new ones created with build are added to the target. So, the target may be non-empty and still lack children waiting to be read from the database. If you look directly to the database you cannot assume that’s the entire collection because new records may have been added to the target, etc.

If you need to work on all current children, new and existing records, load_target and the loaded flag are your friends.

Direct Known Subclasses

HasManyAssociation

Instance Attribute Summary

Attributes inherited from Association

#owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from Association

#initialize, #initialize_attributes, #klass, #loaded?, #marshal_dump, #marshal_load

Constructor Details

This class inherits a constructor from DuckRecord::Associations::Association

Instance Method Details

#add_to_target(record, skip_callbacks = false, &block) ⇒ Object



140
141
142
143
144
# File 'lib/duck_record/associations/collection_association.rb', line 140

def add_to_target(record, skip_callbacks = false, &block)
  index = @target.index(record)

  replace_on_target(record, index, skip_callbacks, &block)
end

#build(attributes = {}, &block) ⇒ Object



42
43
44
45
46
47
48
49
50
# File 'lib/duck_record/associations/collection_association.rb', line 42

def build(attributes = {}, &block)
  if attributes.is_a?(Array)
    attributes.collect { |attr| build(attr, &block) }
  else
    add_to_target(build_record(attributes)) do |record|
      yield(record) if block_given?
    end
  end
end

#concat(*records) ⇒ Object

Add records to this association. Returns self so method calls may be chained. Since << flattens its argument list and inserts each record, push and concat behave identically.



55
56
57
58
# File 'lib/duck_record/associations/collection_association.rb', line 55

def concat(*records)
  records = records.flatten
  @target.concat records
end

#delete(*records) ⇒ Object

Removes records from this association calling before_remove and after_remove callbacks.

This method is abstract in the sense that delete_records has to be provided by descendants. Note this method does not imply the records are actually removed from the database, that depends precisely on delete_records. They are in any case removed from the collection.



84
85
86
87
# File 'lib/duck_record/associations/collection_association.rb', line 84

def delete(*records)
  return if records.empty?
  @target = @target - records
end

#delete_allObject

Removes all records from the association without calling callbacks on the associated records. It honors the :dependent option. However if the :dependent value is :destroy then in that case the :delete_all deletion strategy for the association is applied.

You can force a particular deletion strategy by passing a parameter.

Example:

@author.books.delete_all(:nullify) @author.books.delete_all(:delete_all)

See delete for more info.



73
74
75
# File 'lib/duck_record/associations/collection_association.rb', line 73

def delete_all
  @target.clear
end

#destroy(*records) ⇒ Object

Deletes the records and removes them from this association calling before_remove , after_remove , before_destroy and after_destroy callbacks.

Note that this method removes records from the database ignoring the :dependent option.



94
95
96
97
98
# File 'lib/duck_record/associations/collection_association.rb', line 94

def destroy(*records)
  return if records.empty?
  records = find(records) if records.any? { |record| record.kind_of?(Integer) || record.kind_of?(String) }
  delete_or_destroy(records, :destroy)
end

#empty?Boolean

Returns true if the collection is empty.

If the collection has been loaded it is equivalent to collection.size.zero?. If the collection has not been loaded, it is equivalent to collection.exists?. If the collection has not already been loaded and you are going to fetch the records anyway it is better to check collection.length.zero?.

Returns:

  • (Boolean)


126
127
128
# File 'lib/duck_record/associations/collection_association.rb', line 126

def empty?
  @target.blank?
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


136
137
138
# File 'lib/duck_record/associations/collection_association.rb', line 136

def include?(record)
  @target.include?(record)
end

#readerObject

Implements the reader method, e.g. foo.items for Foo.has_many :items



28
29
30
# File 'lib/duck_record/associations/collection_association.rb', line 28

def reader
  @_reader ||= CollectionProxy.new(klass, self)
end

#replace(other_array) ⇒ Object

Replace this collection with other_array. This will perform a diff and delete/add only records that have changed.



132
133
134
# File 'lib/duck_record/associations/collection_association.rb', line 132

def replace(other_array)
  @target = other_array
end

#replace_on_target(record, index, skip_callbacks) ⇒ Object



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/duck_record/associations/collection_association.rb', line 146

def replace_on_target(record, index, skip_callbacks)
  callback(:before_add, record) unless skip_callbacks

  begin
    if index
      record_was = target[index]
      target[index] = record
    else
      target << record
    end

    yield(record) if block_given?
  rescue
    if index
      target[index] = record_was
    else
      target.delete(record)
    end

    raise
  end

  callback(:after_add, record) unless skip_callbacks

  record
end

#resetObject



37
38
39
40
# File 'lib/duck_record/associations/collection_association.rb', line 37

def reset
  super
  @target = []
end

#sizeObject

Returns the size of the collection by executing a SELECT COUNT(*) query if the collection hasn’t been loaded, and calling collection.size if it has.

If the collection has been already loaded size and length are equivalent. If not and you are going to need the records anyway length will take one less query. Otherwise size is more efficient.

This method is abstract in the sense that it relies on count_records, which is a method descendants have to provide.



110
111
112
# File 'lib/duck_record/associations/collection_association.rb', line 110

def size
  @target.size
end

#uniqObject



114
115
116
# File 'lib/duck_record/associations/collection_association.rb', line 114

def uniq
  @target.uniq!
end

#writer(records) ⇒ Object

Implements the writer method, e.g. foo.items= for Foo.has_many :items



33
34
35
# File 'lib/duck_record/associations/collection_association.rb', line 33

def writer(records)
  replace(records)
end