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
Instance Method Summary collapse
-
#count(options = {}) ⇒ Object
In a HABTM, just look in the rels-ext, 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_record_to_target_with_callbacks, #add_to_target, #any?, #build, #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, #to_ary, #writer
Methods inherited from Association
#association_scope, #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
#count(options = {}) ⇒ Object
In a HABTM, just look in the rels-ext, no need to run a count query from solr.
46 47 48 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 46 def count( = {}) @owner.ids_for_outbound(@reflection.[:property]).size end |
#find_target ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 32 def find_target page_size = @reflection.[:solr_page_size] page_size ||= 200 pids = @owner.ids_for_outbound(@reflection.[:property]) return [] if pids.empty? solr_result = [] 0.step(pids.size,page_size) do |startIdx| query = ActiveFedora::SolrService.construct_query_for_pids(pids.slice(startIdx,page_size)) solr_result += ActiveFedora::SolrService.query(query, rows: page_size) end return ActiveFedora::SolrService.reify_solr_results(solr_result) end |
#first ⇒ Object
50 51 52 |
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 50 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 |
# 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 ### TODO save relationship @owner.add_relationship(@reflection.[:property], record) 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] record.add_relationship(@reflection.[:inverse_of], @owner) record.save end return true end |