Skip to content
This repository has been archived by the owner on Jun 30, 2018. It is now read-only.

Adapter specific importing #656

Closed
wants to merge 1 commit into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 72 additions & 3 deletions lib/tire/model/import.rb
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,15 +13,84 @@ module Import


module ClassMethods module ClassMethods


def import options={}, &block def import options = {}, &block

options = { :method => 'paginate' }.update options options = { :method => 'paginate' }.update options
options[:per_page] ||= 1000

# Lets support multiple ORM's
if defined?(ActiveRecord) && klass.ancestors.include?(::ActiveRecord::Base) # Active Record
extend ActiveRecord
elsif defined?(Mondgoid::Document) && klass.ancestors.include?(::Mongoid::Document) # Mongoid
extend Mongoid
elsif defined?(Kaminari) && klass.respond_to?(:page) # Kaminari
extend Kaminari
elsif defined?(WillPaginate) && klass.respond_to?(:paginate) # Will Paginate
extend WillPaginate
else
extend Index
end

import options, &block

end

end

module ActiveRecord
def import options={}, &block
index = options[:index] ? Tire::Index.new(options.delete(:index)) : self.index index = options[:index] ? Tire::Index.new(options.delete(:index)) : self.index
index.import klass, options, &block klass.find_in_batches(batch_size: options[:per_page]) do |group|
index.import group, options, &block
end
end end
end


module Mongoid
def import options={}, &block
index = options[:index] ? Tire::Index.new(options.delete(:index)) : self.index
0.step(klass.count, options[:per_page]) do |offset|
items = klass.limit(options[:per_page]).skip(offset)
index.import items, options, &block
end
end
end end


end module Kaminari
def import options={}, &block
index = options[:index] ? Tire::Index.new(options.delete(:index)) : self.index
current = 0
page = 1
while current < klass.count
items = klass.page(page).per(options[:per_page])
index.import items, options, &block
current = current + items.size
page += 1
end
end
end

module WillPaginate
def import options={}, &block
index = options[:index] ? Tire::Index.new(options.delete(:index)) : self.index
current = 0
page = 1
while current < klass.count
items = klass.paginate(page: page, per_page: options[:per_page])
index.import items, options, &block
current += items.size
page += 1
end
end
end

module Index
def import options={}, &block
index = options[:index] ? Tire::Index.new(options.delete(:index)) : self.index
index.import klass, options, &block
end
end


end
end end
end end