Class: ActiveFedora::Associations::HasManyAssociation
- Inherits:
-
CollectionAssociation
- Object
- Association
- CollectionAssociation
- ActiveFedora::Associations::HasManyAssociation
- Defined in:
- lib/active_fedora/associations/has_many_association.rb
Overview
:nodoc:
Instance Attribute Summary
Attributes inherited from CollectionAssociation
Attributes inherited from Association
#inversed, #owner, #reflection, #target
Instance Method Summary collapse
-
#count_records ⇒ Object
Returns the number of records in this collection.
-
#initialize(owner, reflection) ⇒ HasManyAssociation
constructor
A new instance of HasManyAssociation.
- #insert_record(record, validate = true, raise = false) ⇒ Object
- #set_owner_attributes(record) ⇒ Object
Methods inherited from CollectionAssociation
#add_to_target, #any?, #build, #concat, #concat_records, #count, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #find, #first, #ids_reader, #ids_writer, #include?, #last, #load_from_solr, #load_target, #null_scope?, #reader, #replace, #reset, #scope, #select, #size, #target=, #to_ary, #writer
Methods inherited from Association
#association_scope, #initialize_attributes, #load_target, #loaded!, #loaded?, #reload, #reset, #scope, #set_inverse_instance, #stale_target?, #target_scope
Constructor Details
#initialize(owner, reflection) ⇒ HasManyAssociation
Returns a new instance of HasManyAssociation.
4 5 6 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 4 def initialize(owner, reflection) super end |
Instance Method Details
#count_records ⇒ Object
Returns the number of records in this collection.
That does not depend on whether the collection has already been loaded or not. The size
method is the one that takes the loaded flag into account and delegates to count_records
if needed.
If the collection is empty the target is set to an empty array and the loaded flag is set to true as well.
16 17 18 19 20 21 22 23 24 25 26 27 28 29 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 16 def count_records count = if loaded? @target.size else @reflection.klass.count(conditions: construct_query) end # If there's nothing in the database and @target has no new records # we are certain the current target is an empty array. This is a # documented side-effect of the method that may avoid an extra SELECT. @target ||= [] and loaded! if count == 0 count end |
#insert_record(record, validate = true, raise = false) ⇒ Object
53 54 55 56 57 58 59 60 61 62 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 53 def insert_record(record, validate = true, raise = false) set_owner_attributes(record) set_inverse_instance(record) if raise record.save!(validate: validate) else record.save(validate: validate) end end |
#set_owner_attributes(record) ⇒ Object
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 31 def set_owner_attributes(record) if klass == ActiveFedora::Base inverse = find_polymorphic_inverse(record) if inverse.belongs_to? record[inverse.foreign_key] = owner.id else # HABTM record[inverse.foreign_key] ||= [] record[inverse.foreign_key] += [owner.id] end elsif owner.persisted? inverse = reflection.inverse_of if inverse && inverse.collection? record[inverse.foreign_key] ||= [] record[inverse.foreign_key] += [owner.id] elsif inverse && inverse.klass == ActiveFedora::Base record[inverse.foreign_key] = owner.id else record[reflection.foreign_key] = owner.id end end end |