Sha256: 00b4edfa20f464ea04e1abd734d3b970a38a73d0de01e02e641feac12b03c9ea

Contents?: true

Size: 1.82 KB

Versions: 2

Compression:

Stored size: 1.82 KB

Contents

# auto load rails tasks
require 'sunspot/rails/tasks'

# hack to remove 'sunspot:reindex' task and add our own
Rake::TaskManager.class_eval do
  def remove_task(task_name)
    @tasks.delete(task_name.to_s)
  end
end

Rake.application.remove_task('sunspot:reindex')

# override the tasks that depends on active record
namespace :sunspot do
  
  desc "Reindex all solr models that are located in your application's models directory. (Batch size ignored)"
  # This task depends on the standard Rails file naming \
  # conventions, in that the file name matches the defined class name. \
  # By default the indexing system works in batches of 50 records, you can \
  # set your own value for this by using the batch_size argument. You can \
  # also optionally define a list of models to separated by a forward slash '/'
  # 
  # $ rake sunspot:reindex                # reindex all models
  # $ rake sunspot:reindex[1000]          # reindex in batches of 1000
  # $ rake sunspot:reindex[false]         # reindex without batching
  # $ rake sunspot:reindex[,Post]         # reindex only the Post model
  # $ rake sunspot:reindex[1000,Post]     # reindex only the Post model in
  #                                       # batchs of 1000
  # $ rake sunspot:reindex[,Post+Author]  # reindex Post and Author model
  task :reindex, :batch_size, :models, :needs => :environment do |t, args|
    unless args[:models]
      all_files = Dir.glob(Rails.root.join('app', 'models', '*.rb'))
      all_models = all_files.map { |path| File.basename(path, '.rb').camelize.constantize }
      sunspot_models = all_models.select { |m| m.respond_to?(:searchable?) and m.searchable? }
    else
      sunspot_models = args[:models].split('+').map{|m| m.constantize}
    end
    sunspot_models.each do |model|
      puts "Re-indexing #{model.name}"
      model.solr_reindex
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
aq1018-sunspot_mongoid-0.5.0 tasks/sunspot_mongoid.rake
aq1018-sunspot_mongoid-0.4.0 tasks/sunspot_mongoid.rake