Sha256: 56797c121b40883f77471c1d2add8f9199bbd5bfaac26c5a8c5bb25177f16583

Contents?: true

Size: 1.01 KB

Versions: 11

Compression:

Stored size: 1.01 KB

Contents

namespace :searchkick do
  desc "reindex a model (specify CLASS)"
  task reindex: :environment do
    class_name = ENV["CLASS"]
    abort "USAGE: rake searchkick:reindex CLASS=Product" unless class_name

    model = class_name.safe_constantize
    abort "Could not find class: #{class_name}" unless model
    abort "#{class_name} is not a searchkick model" unless Searchkick.models.include?(model)

    puts "Reindexing #{model.name}..."
    model.reindex
    puts "Reindex successful"
  end

  namespace :reindex do
    desc "reindex all models"
    task all: :environment do
      # eager load models to populate Searchkick.models
      if Rails.respond_to?(:autoloaders) && Rails.autoloaders.zeitwerk_enabled?
        # fix for https://github.com/rails/rails/issues/37006
        Zeitwerk::Loader.eager_load_all
      else
        Rails.application.eager_load!
      end

      Searchkick.models.each do |model|
        puts "Reindexing #{model.name}..."
        model.reindex
      end
      puts "Reindex complete"
    end
  end
end

Version data entries

11 entries across 11 versions & 1 rubygems

Version Path
searchkick-4.6.3 lib/tasks/searchkick.rake
searchkick-4.6.2 lib/tasks/searchkick.rake
searchkick-4.6.1 lib/tasks/searchkick.rake
searchkick-4.6.0 lib/tasks/searchkick.rake
searchkick-4.5.2 lib/tasks/searchkick.rake
searchkick-4.5.1 lib/tasks/searchkick.rake
searchkick-4.5.0 lib/tasks/searchkick.rake
searchkick-4.4.4 lib/tasks/searchkick.rake
searchkick-4.4.3 lib/tasks/searchkick.rake
searchkick-4.4.2 lib/tasks/searchkick.rake
searchkick-4.4.1 lib/tasks/searchkick.rake