update start import and export menu
This commit is contained in:
@@ -1,4 +1,44 @@
|
||||
namespace :consolidate do
|
||||
|
||||
|
||||
desc 'export sers who have logged in since 2017-06-30'
|
||||
task :export => :environment do
|
||||
puts "Export users who have logged in since 2017-06-30"
|
||||
|
||||
# get a file ready, the 'data' directory has already been added in Rails.root
|
||||
filepath = File.join(Rails.root, 'data', 'recent_users.json')
|
||||
puts "- exporting users into #{filepath}"
|
||||
|
||||
# the key here is to use 'as_json', otherwise you get an ActiveRecord_Relation object, which extends
|
||||
# array, and works like in an array, but not for exporting
|
||||
users = Employee.all.as_json
|
||||
puts users
|
||||
# The pretty is nice so I can diff exports easily, if that's not important, JSON(users) will do
|
||||
File.open(filepath, 'w') do |f|
|
||||
f.write(JSON.pretty_generate(users))
|
||||
end
|
||||
|
||||
puts "- dumped #{users.size} users"
|
||||
end
|
||||
|
||||
desc 'import users from recent users dump'
|
||||
task :import => :environment do
|
||||
puts "Importing current users"
|
||||
|
||||
filepath = File.join(Rails.root, 'data', 'recent_users.json')
|
||||
abort "Input file not found: #{filepath}" unless File.exist?(filepath)
|
||||
|
||||
current_users = JSON.parse(File.read(filepath))
|
||||
|
||||
current_users.each do |cu|
|
||||
puts cu.to_json
|
||||
Employee.create(cu)
|
||||
end
|
||||
|
||||
puts "- imported #{current_users.size} users"
|
||||
end
|
||||
|
||||
|
||||
desc "Receipt"
|
||||
task :receipt => :environment do
|
||||
Receipt.delete_all
|
||||
|
||||
Reference in New Issue
Block a user