Class: ActiveFedora::Associations::HasAndBelongsToManyAssociation

Inherits:
CollectionAssociation show all
Defined in:
lib/active_fedora/associations/has_and_belongs_to_many_association.rb

Overview

:nodoc:

Instance Attribute Summary

Attributes inherited from CollectionAssociation

#proxy

Attributes inherited from Association

#owner, #reflection, #target

Instance Method Summary (collapse)

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

- (HasAndBelongsToManyAssociation) initialize(owner, reflection)

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

- (Object) count(options = {})

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(options = {})
  @owner.ids_for_outbound(@reflection.options[:property]).size
end

- (Object) find_target



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.options[:solr_page_size]
    page_size ||= 200
    pids = @owner.ids_for_outbound(@reflection.options[: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

- (Object) first



50
51
52
# File 'lib/active_fedora/associations/has_and_belongs_to_many_association.rb', line 50

def first
  load_target.first
end

- (Object) insert_record(record, force = true, validate = true)



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.options[:property], record)

  if @owner.new_record? and @reflection.options[: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.options[:inverse_of]
    record.add_relationship(@reflection.options[:inverse_of], @owner)
    record.save
  end

  return true
end