Sha256: 6f96949e61c407c5d6563c1d14e38f6c6e07b0578e93e15569da9743edf68325

Contents?: true

Size: 1.76 KB

Versions: 1

Compression:

Stored size: 1.76 KB

Contents

module Hyrax
  # Responsible for adding the necessary callbacks for updating the nested collection information
  # This is part of the after update index because it is a potentially very expensive process.
  #
  # @todo Consider extracting the update_index callback to ActiveFedora::Base
  module CollectionNesting
    extend ActiveSupport::Concern

    included do
      extend ActiveModel::Callbacks
      include ActiveModel::Validations::Callbacks

      define_model_callbacks :update_index, only: :after
      after_update_index :update_nested_collection_relationship_indices
      after_destroy :update_child_nested_collection_relationship_indices
      before_save :before_update_nested_collection_relationship_indices
      after_save :after_update_nested_collection_relationship_indices

      def before_update_nested_collection_relationship_indices
        @during_save = true
      end

      def after_update_nested_collection_relationship_indices
        @during_save = false
        Hyrax.config.nested_relationship_reindexer.call(id: id)
      end

      def update_nested_collection_relationship_indices
        return if @during_save
        Hyrax.config.nested_relationship_reindexer.call(id: id)
      end

      def update_child_nested_collection_relationship_indices
        children = find_children_of(destroyed_id: id)
        children.each do |child|
          Hyrax.config.nested_relationship_reindexer.call(id: child.id)
        end
      end
    end

    def update_index(*args)
      _run_update_index_callbacks { super }
    end

    def find_children_of(destroyed_id:)
      ActiveFedora::SolrService.query(ActiveFedora::SolrQueryBuilder.construct_query(member_of_collection_ids_ssim: destroyed_id))
    end

    def use_nested_reindexing?
      true
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
hyrax-2.1.0.beta2 app/models/concerns/hyrax/collection_nesting.rb