module Tire::Tasks::Import

Public Instance Methods

add_pagination_to_klass(klass) click to toggle source
# File lib/tire/tasks.rb, line 25
def add_pagination_to_klass(klass)
  if defined?(Kaminari) && klass.respond_to?(:page)
    klass.instance_eval do
      def paginate(options = {})
        page(options[:page]).per(options[:per_page])
      end
    end
  end unless klass.respond_to?(:paginate)
end
create_index(index, klass) click to toggle source
# File lib/tire/tasks.rb, line 13
def create_index(index, klass)
  unless index.exists?
    mapping = MultiJson.encode(klass.tire.mapping_to_hash, :pretty => Tire::Configuration.pretty)
    puts "[IMPORT] Creating index '#{index.name}' with mapping:", mapping
    unless index.create(:mappings => klass.tire.mapping_to_hash, :settings => klass.tire.settings)
      puts "[ERROR] There has been an error when creating the index -- Elasticsearch returned:",
                  index.response
      exit(1)
    end
  end
end
delete_index(index) click to toggle source
# File lib/tire/tasks.rb, line 8
def delete_index(index)
  puts "[IMPORT] Deleting index '#{index.name}'"
  index.delete
end
import_model(index, klass, params) click to toggle source
# File lib/tire/tasks.rb, line 45
def import_model(index, klass, params)
  unless progress_bar(klass)
    puts "[IMPORT] Importing '#{klass.to_s}'"
  end
  klass.tire.import(params) do |documents|
    progress_bar(klass).inc documents.size if progress_bar(klass)
    documents
  end
  progress_bar(klass).finish if progress_bar(klass)
end
paginate(options = {}) click to toggle source
# File lib/tire/tasks.rb, line 28
def paginate(options = {})
  page(options[:page]).per(options[:per_page])
end
progress_bar(klass, total=nil) click to toggle source
# File lib/tire/tasks.rb, line 35
def progress_bar(klass, total=nil)
  @progress_bars ||= {}

  if total
    @progress_bars[klass.to_s] ||= ANSI::Progressbar.new(klass.to_s, total)
  else
    @progress_bars[klass.to_s]
  end
end