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.
- #handle_dependency ⇒ Object
-
#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, #reset_scope, #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 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 16 def count_records count = scope.count # 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.zero? count end |
#handle_dependency ⇒ Object
[View source]
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 60 def handle_dependency case [:dependent] when :restrict_with_exception raise ActiveFedora::DeleteRestrictionError, reflection.name unless empty? when :restrict_with_error unless empty? record = owner.class.human_attribute_name(reflection.name).downcase owner.errors.add(:base, || :'restrict_dependent_destroy.has_many', record: record) throw(:abort) end else if [:dependent] == :destroy # No point in executing the counter update since we're going to destroy the parent anyway load_target.each { |t| t.destroyed_by_association = reflection } destroy_all else delete_all end end end |
#insert_record(record, validate = true, raise = false) ⇒ Object
[View source]
49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 49 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
[View source]
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/active_fedora/associations/has_many_association.rb', line 27 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 |