Class: DuckRecord::Associations::EmbedsManyAssociation

Inherits:
EmbedsAssociation show all
Defined in:
lib/duck_record/associations/embeds_many_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.

Instance Attribute Summary

Attributes inherited from EmbedsAssociation

#owner, #reflection, #target

Instance Method Summary collapse

Methods inherited from EmbedsAssociation

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

Constructor Details

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

Instance Method Details

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



156
157
158
159
160
# File 'lib/duck_record/associations/embeds_many_association.rb', line 156

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
51
52
53
54
# File 'lib/duck_record/associations/embeds_many_association.rb', line 42

def build(attributes = {}, &block)
  if attributes.is_a?(klass)
    add_to_target(attributes) do |record|
      yield(record) if block_given?
    end
  elsif 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.



59
60
61
62
63
64
65
66
67
# File 'lib/duck_record/associations/embeds_many_association.rb', line 59

def concat(*records)
  records.flatten.each do |r|
    begin
      build(r)
    rescue
      raise_on_type_mismatch!(r)
    end
  end
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.



93
94
95
96
# File 'lib/duck_record/associations/embeds_many_association.rb', line 93

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.



82
83
84
# File 'lib/duck_record/associations/embeds_many_association.rb', line 82

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.



103
104
105
106
107
# File 'lib/duck_record/associations/embeds_many_association.rb', line 103

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)


135
136
137
# File 'lib/duck_record/associations/embeds_many_association.rb', line 135

def empty?
  @target.blank?
end

#include?(record) ⇒ Boolean

Returns:

  • (Boolean)


152
153
154
# File 'lib/duck_record/associations/embeds_many_association.rb', line 152

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/embeds_many_association.rb', line 28

def reader
  @_reader ||= EmbedsManyProxy.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.



141
142
143
144
145
146
147
148
149
150
# File 'lib/duck_record/associations/embeds_many_association.rb', line 141

def replace(other_array)
  delete_all
  other_array.each do |item|
    begin
      build(item)
    rescue
      raise_on_type_mismatch!(item)
    end
  end
end

#replace_on_target(record, index, skip_callbacks) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
# File 'lib/duck_record/associations/embeds_many_association.rb', line 162

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/embeds_many_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.



119
120
121
# File 'lib/duck_record/associations/embeds_many_association.rb', line 119

def size
  @target.size
end

#uniqObject



123
124
125
# File 'lib/duck_record/associations/embeds_many_association.rb', line 123

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/embeds_many_association.rb', line 33

def writer(records)
  replace(records)
end