Skip to content

Commit

Permalink
make CollectionAssociation#find_target private
Browse files Browse the repository at this point in the history
  • Loading branch information
jcoyne committed Jun 30, 2015
1 parent bd71b7f commit b019322
Showing 1 changed file with 35 additions and 39 deletions.
74 changes: 35 additions & 39 deletions lib/active_fedora/associations/collection_association.rb
Expand Up @@ -239,51 +239,13 @@ def target=(target)

def load_target
if find_target?
targets = []

begin
targets = find_target
rescue ObjectNotFoundError, Ldp::Gone => e
ActiveFedora::Base.logger.error "Solr and Fedora may be out of sync:\n" + e.message if ActiveFedora::Base.logger
reset
end

@target = merge_target_lists(targets, @target)
@target = merge_target_lists(find_target, @target)
end

loaded!
target
end

def find_target
# TODO: don't reify, just store the solr results and lazily reify.
# For now, we set a hard limit of 1000 results.
records = ActiveFedora::QueryResultBuilder.reify_solr_results(load_from_solr(rows: 1000))
records.each { |record| set_inverse_instance(record) }
records
end

def merge_target_lists(loaded, existing)
return loaded if existing.empty?
return existing if loaded.empty?

loaded.map do |f|
i = existing.index(f)
if i
existing.delete_at(i).tap do |t|
keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
# FIXME: this call to attributes causes many NoMethodErrors
attributes = f.attributes
(attributes.keys - keys).each do |k|
t.send("#{k}=", attributes[k])
end
end
else
f
end
end + existing
end

# @param opts [Hash] Options that will be passed through to ActiveFedora::SolrService.query.
def load_from_solr(opts = Hash.new)
finder_query = construct_query
Expand All @@ -293,6 +255,7 @@ def load_from_solr(opts = Hash.new)
SolrService.query(finder_query, { rows: rows }.merge(opts))
end


def add_to_target(record, skip_callbacks = false)
# transaction do
callback(:before_add, record) unless skip_callbacks
Expand Down Expand Up @@ -336,6 +299,39 @@ def construct_query

private

def find_target
# TODO: don't reify, just store the solr results and lazily reify.
# For now, we set a hard limit of 1000 results.
records = ActiveFedora::QueryResultBuilder.reify_solr_results(load_from_solr(rows: 1000))
records.each { |record| set_inverse_instance(record) }
records
rescue ObjectNotFoundError, Ldp::Gone => e
ActiveFedora::Base.logger.error "Solr and Fedora may be out of sync:\n" + e.message if ActiveFedora::Base.logger
reset
[]
end

def merge_target_lists(loaded, existing)
return loaded if existing.empty?
return existing if loaded.empty?

loaded.map do |f|
i = existing.index(f)
if i
existing.delete_at(i).tap do |t|
keys = ["id"] + t.changes.keys + (f.attribute_names - t.attribute_names)
# FIXME: this call to attributes causes many NoMethodErrors
attributes = f.attributes
(attributes.keys - keys).each do |k|
t.send("#{k}=", attributes[k])
end
end
else
f
end
end + existing
end

def find_reflection
return reflection if @reflection.options[:predicate]
if @reflection.class_name && @reflection.class_name != 'ActiveFedora::Base' && @reflection.macro != :has_and_belongs_to_many
Expand Down

0 comments on commit b019322

Please sign in to comment.