Sha256: 30f88f3e5e0ef65aa3964fa7979232e606f2f85dd2e85f213cbbf24eeda4e13c

Contents?: true

Size: 1.12 KB

Versions: 1

Compression:

Stored size: 1.12 KB

Contents

# Assigns searchable objects to words in the search index.
module Pose
  class Assignment < ActiveRecord::Base
    belongs_to :word, class_name: 'Pose::Word'
    belongs_to :posable, polymorphic: true

    # Removes all Assignments for the given class.
    def self.delete_class_index clazz
      Assignment.delete_all(posable_type: clazz.name)
    end

    # Removes all Assignments that aren't used anymore.
    def self.cleanup_orphaned_pose_assignments progress_bar = nil
      Assignment.find_each(include: [:posable, :word], batch_size: 5000) do |assignment|
        progress_bar.increment if progress_bar

        # Delete the assignment if the posable object no longer exists.
        if assignment.posable.nil?
          puts "deleting assignment '#{assignment.id}' because the posable object no longer exists."
          assignment.delete
          next
        end

        # Delete the assignment if the Pose::Word for it no longer exists.
        if assignment.word.nil?
          puts "deleting assignment '#{assignment.id}' because its word no longer exists."
          assignment.delete
        end
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
pose-3.0.0 app/models/pose/assignment.rb