Module: Tire::Tasks::Import

Extended by:
Import
Included in:
Import
Defined in:
lib/tire/tasks.rb

Instance Method Summary collapse

Instance Method Details

#add_pagination_to_klass(klass) ⇒ Object



25
26
27
28
29
30
31
32
33
# 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) ⇒ Object



13
14
15
16
17
18
19
20
21
22
23
# 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) ⇒ Object



8
9
10
11
# 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) ⇒ Object



45
46
47
48
49
50
51
52
53
54
# 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

#progress_bar(klass, total = nil) ⇒ Object



35
36
37
38
39
40
41
42
43
# 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