Sha256: 7aafc739fc5b4232ba9243c78cdc0d61d4d8b28dd1062b9e9978f160103f8ba1

Contents?: true

Size: 1.26 KB

Versions: 5

Compression:

Stored size: 1.26 KB

Contents

module Trucker

  def self.migrate(name, options={})
    # Grab custom entity label if present
    label = options.delete(:label) if options[:label]

    unless options[:helper]
  
      # Grab model to migrate
      model = name.to_s.singularize.capitalize
  
      # Wipe out existing records
      model.constantize.delete_all

      # Status message
      status = "Migrating "
      status += "#{number_of_records || "all"} #{label || name}"
      status += " after #{offset_for_records}" if offset_for_records
  
      # Set import counter
      counter = 0
      counter += offset_for_records if offset_for_records
      total_records = "Legacy#{model}".constantize.find(:all).size
  
      # Start import
      "Legacy#{model}".constantize.find(:all, with(options)).each do |record|
        counter += 1
        puts status + " (#{counter}/#{total_records})"
        record.migrate
      end
    else
      eval options[:helper].to_s
    end
  end

  protected

    def self.with(options={})
      {:limit => number_of_records, :offset => offset_for_records}.merge(options)
    end

    def self.number_of_records
      nil || ENV['limit'].to_i if ENV['limit'].to_i > 0
    end

    def self.offset_for_records
      nil || ENV['offset'].to_i if ENV['offset'].to_i > 0
    end
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
trucker-0.5.0 lib/trucker.rb
trucker-0.4.1 lib/trucker.rb
trucker-0.3.0 lib/trucker.rb
trucker-0.2.0 lib/trucker.rb
trucker-0.1.0 lib/trucker.rb