Class: ActiveFedora::Associations::HasAndBelongsToManyAssociation
- Inherits:
-
CollectionAssociation
- Object
- Association
- CollectionAssociation
- ActiveFedora::Associations::HasAndBelongsToManyAssociation
- Defined in:
- lib/active_fedora/associations/has_and_belongs_to_many_association.rb
Overview
:nodoc:
Instance Attribute Summary
Attributes inherited from CollectionAssociation
Attributes inherited from Association
#inversed, #owner, #reflection, #target
Instance Method Summary collapse
- #concat_records(*records) ⇒ Object
-
#count(options = {}) ⇒ Object
In a HABTM, just look in the RDF, no need to run a count query from solr.
- #find_target ⇒ Object
- #first ⇒ Object
-
#initialize(owner, reflection) ⇒ HasAndBelongsToManyAssociation
constructor
A new instance of HasAndBelongsToManyAssociation.
- #insert_record(record, force = true, validate = true) ⇒ Object
Methods inherited from CollectionAssociation
#add_to_target, #any?, #build, #concat, #create, #create!, #delete, #delete_all, #destroy, #destroy_all, #empty?, #find, #ids_reader, #ids_writer, #include?, #last, #load_from_solr, #load_target, #merge_target_lists, #null_scope?, #reader, #replace, #reset, #scope, #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) ⇒ HasAndBelongsToManyAssociation
Returns a new instance of HasAndBelongsToManyAssociation.
5 6 7 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 5 def initialize(owner, reflection) super end |
Instance Method Details
#concat_records(*records) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 33 def concat_records(*records) result = true records.flatten.each do |record| raise_on_type_mismatch(record) add_to_target(record) do |r| result &&= insert_record(record) end end result && records end |
#count(options = {}) ⇒ Object
In a HABTM, just look in the RDF, no need to run a count query from solr.
61 62 63 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 61 def count( = {}) owner[reflection.foreign_key].size end |
#find_target ⇒ Object
47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 47 def find_target page_size = @reflection.[:solr_page_size] page_size ||= 200 ids = owner[reflection.foreign_key] return [] if ids.blank? solr_result = [] 0.step(ids.size,page_size) do |startIdx| query = ActiveFedora::SolrQueryBuilder.construct_query_for_ids(ids.slice(startIdx,page_size)) solr_result += ActiveFedora::SolrService.query(query, rows: page_size) end return ActiveFedora::QueryResultBuilder.reify_solr_results(solr_result) end |
#first ⇒ Object
65 66 67 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 65 def first load_target.first end |
#insert_record(record, force = true, validate = true) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 9 def insert_record(record, force = true, validate = true) if record.new_record? if force record.save! else return false unless record.save(:validate => validate) end end owner[reflection.foreign_key] ||= [] owner[reflection.foreign_key] += [record.id] if @owner.new_record? and @reflection.[:inverse_of] ActiveFedora::Base.logger.warn("has_and_belongs_to_many #{@reflection.inspect} is cowardly refusing to insert the inverse relationship into #{record}, because #{@owner} is not persisted yet.") if ActiveFedora::Base.logger elsif @reflection.[:inverse_of] inverse = @reflection.inverse_of record[inverse.foreign_key] ||= [] record[inverse.foreign_key] += [owner.id] record.save end return true end |