Sha256: 797f0efb12df255a50d66c43224ba10e9d9826b4ec86dd29374d74e1a1b0d693

Contents?: true

Size: 1.54 KB

Versions: 5

Compression:

Stored size: 1.54 KB

Contents

require 'config/environment'

namespace :tokens do
  desc "Generates the token for objects without tokens."
  task :generate => :environment do
    tokenize_models
  end

  desc "Re-builds the token for all objects."
  task :regenerate => :environment do
    tokenize_models(true)
  end
end

def array_of_active_record_models
  Dir.glob(RAILS_ROOT + '/app/models/*.rb').each { |file| require file }
 
  models_with_token = ActiveRecord::Base.send(:subclasses).select{|m| m.respond_to?(:tokenized_by)}
end
 
def tokenize_records(records)
  total_count = records.size
  
  count = 0
  
  records.each do |record|
    record.tokenize     #this generates tokens
    record.save false   #this saves without checking validations
    count += 1
    print "\r#{count}/#{total_count}"
    GC.start if count % 1000 == 0 #launch garbage collection each 1000 registers
  end
  puts ""
end
 
def tokenize_models(regenerate = false)
  start = Time.now
  puts "Start token generation"
  puts "++++++++++++++++++++++++++++++++"
  
  array_of_active_record_models.each do |model|
  puts "Generating new tokens for #{model.name.pluralize}"
  
  conditions = "#{model.token_field_name} IS NULL OR #{model.token_field_name} = ''" unless regenerate
 
  records_without_token = model.all(:conditions => conditions)
    if records_without_token.size > 0 
      tokenize_records(records_without_token)
    else
      puts "There are no records without token"
      puts "++++++++++++++++++++++++++++++++"
    end
  end
  puts "Elapsed time " + (Time.now - start).seconds.to_s + " seconds"
  
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
acts_as_tokenizable-0.3.3 lib/tasks/acts_as_tokenizable.rake
acts_as_tokenizable-0.3.2 lib/tasks/acts_as_tokenizable.rake
acts_as_tokenizable-0.3.1 lib/tasks/acts_as_tokenizable.rake
acts_as_tokenizable-0.3.0 lib/tasks/acts_as_tokenizable.rake
acts_as_tokenizable-0.2.0 lib/tasks/acts_as_tokenizable.rake