Class: ActiveEntity::Associations::Embeds::CollectionAssociation
- Inherits:
-
Association
- Object
- Association
- ActiveEntity::Associations::Embeds::CollectionAssociation
- Defined in:
- lib/active_entity/associations/embeds/collection_association.rb
Overview
Active Entity 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
Instance Attribute Summary
Attributes inherited from Association
Instance Method Summary collapse
- #add_to_target(record, skip_callbacks = false, &block) ⇒ Object
- #build(attributes = {}, &block) ⇒ Object
-
#concat(*records) ⇒ Object
Add
records
to this association. -
#delete(*records) ⇒ Object
(also: #destroy)
Removes
records
from this association callingbefore_remove
andafter_remove
callbacks. -
#delete_all ⇒ Object
(also: #destroy_all)
Removes all records from the association without calling callbacks on the associated records.
-
#empty? ⇒ Boolean
Returns true if the collection is empty.
- #find(&block) ⇒ Object
- #include?(record) ⇒ Boolean
-
#initialize(owner, reflection) ⇒ CollectionAssociation
constructor
:nodoc:.
-
#reader ⇒ Object
Implements the reader method, e.g.
-
#replace(other_array) ⇒ Object
Replace this collection with
other_array
. -
#size ⇒ Object
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. -
#writer(records) ⇒ Object
Implements the writer method, e.g.
Methods inherited from Association
#extensions, #initialize_attributes, #inversed_from, #klass, #loaded?, #marshal_dump, #marshal_load, #remove_inverse_instance, #set_inverse_instance
Constructor Details
#initialize(owner, reflection) ⇒ CollectionAssociation
:nodoc:
30 31 32 33 34 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 30 def initialize(owner, reflection) super @target = [] end |
Instance Method Details
#add_to_target(record, skip_callbacks = false, &block) ⇒ Object
151 152 153 154 155 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 151 def add_to_target(record, skip_callbacks = false, &block) # index = @target.index(record) # replace_on_target(record, index, skip_callbacks, &block) replace_on_target(record, nil, skip_callbacks, &block) end |
#build(attributes = {}, &block) ⇒ Object
50 51 52 53 54 55 56 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 50 def build(attributes = {}, &block) if attributes.is_a?(Array) attributes.collect { |attr| build(attr, &block) } else add_to_target(build_record(attributes, &block)) 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.
61 62 63 64 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 61 def concat(*records) records = records.flatten concat_records(records) end |
#delete(*records) ⇒ Object Also known as: destroy
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.
91 92 93 94 95 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 91 def delete(*records) records.each do |record| target.delete(record) end end |
#delete_all ⇒ Object Also known as: destroy_all
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.
79 80 81 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 79 def delete_all target.clear 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?
.
120 121 122 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 120 def empty? size.zero? end |
#find(&block) ⇒ Object
46 47 48 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 46 def find(&block) target.find(&block) end |
#include?(record) ⇒ Boolean
143 144 145 146 147 148 149 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 143 def include?(record) if record.is_a?(reflection.klass) target.include?(record) else false end end |
#reader ⇒ Object
Implements the reader method, e.g. foo.items for Foo.has_many :items
37 38 39 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 37 def reader @proxy ||= 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.
126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 126 def replace(other_array) records = other_array.map do |val| if val.is_a? reflection.klass val elsif val.nil? next elsif val.respond_to?(:to_h) build_record(val.to_h) end rescue => ex raise_on_type_mismatch!(val) raise ex end target.replace records end |
#size ⇒ Object
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.
108 109 110 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 108 def size target.size end |
#writer(records) ⇒ Object
Implements the writer method, e.g. foo.items= for Foo.has_many :items
42 43 44 |
# File 'lib/active_entity/associations/embeds/collection_association.rb', line 42 def writer(records) replace(records) end |