class UpdateSearchObserver < Mongoid::Observer
  observe :artefact

  def after_save(artefact)
    rummageable_artefact = RummageableArtefact.new(artefact)
    rummageable_artefact.submit if rummageable_artefact.should_be_indexed?
    # Relying on current behaviour where this does not raise errors
    # if done more than once, or done on artefacts never put live
    rummageable_artefact.delete if artefact.archived?
  end
end

class UpdateSearchObserver < Mongoid::Observer
  observe :artefact

  def after_save(artefact)
    rummageable_artefact = RummageableArtefact.new(artefact)
    
    rummageable_artefact.submit if rummageable_artefact.should_be_indexed?

    if artefact.live? && becoming_nonindexed_kind?(artefact)
      rummageable_artefact.delete
    end

    # Relying on current behaviour where this does not raise errors
    # if done more than once, or done on artefacts never put live
    rummageable_artefact.delete if artefact.archived?
  end

  def becoming_nonindexed_kind?(artefact)
    old_kind = artefact.kind_was
    new_kind = artefact.kind

    not_a_new_record = ! old_kind.nil?
    not_a_new_record &&
        (! RummageableArtefact::FORMATS_NOT_TO_INDEX.include?(old_kind)) &&
        RummageableArtefact::FORMATS_NOT_TO_INDEX.include?(new_kind)
  end
end

Delete artefacts when changed to nonindexed kinds
Delete artefacts when changed to nonindexed kinds

We have had some artefacts created (incorrectly) as Answers when they should
have been CompletedTransactions. These were not removed automatically. This
change would ensure that they are in future.
