Sha256: d85215b0bd945013abc848805d78d9adf6ac4abf0f6d4a8797644c88ee8dd739

Contents?: true

Size: 1.25 KB

Versions: 2

Compression:

Stored size: 1.25 KB

Contents

# Assigns searchable objects to words in the search index.
module Pose
  class Assignment < ActiveRecord::Base
    self.table_name_prefix = 'pose_'

    belongs_to :word, class_name: 'Pose::Word'
    belongs_to :posable, polymorphic: true

    # Removes all Assignments for the given class.
    # Returns a number for removed records.
    # @param [Class] clazz
    # @return [Integer]
    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.includes([:posable, :word]).find_each(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

2 entries across 2 versions & 1 rubygems

Version Path
pose-3.2.0 lib/pose/assignment.rb
pose-3.1.1 lib/pose/assignment.rb