# encoding: utf-8 require 'pry' namespace :import do task :ntu_mb_news_report => :environment do jfile = File.new("#{Rails.root}/lib/import_data/news_report.json").read jdata = JSON.parse(jfile) end task :ntu_mb => :environment do DEPARTMENTS = %w[Management BA Acc Fin IB IM EMBA GMBA] print 'Enter the numer of department you want to import' puts DEPARTMENTS.map.with_index{|t,i| "#{i}.#{t}" }.to_s num = STDIN.gets.gsub("\n",'').to_i puts "Destroy all BulletinCategory? (Default No)" if STDIN.gets.gsub("\n",'').downcase == 'yes' BulletinCategory.destroy_all else puts "Did not destroy" end jfile = File.new("#{Rails.root}/lib/import_data/announce_categories.json").read jdata = JSON.parse(jfile) jdata.each do |data| a = BulletinCategory.new data a.save end news_report_category = BulletinCategory.new {"key":"news_report","title_translations":{"en":"NewsReport","zh_tw":"新聞報導"}} puts "Imported #{jdata.count}+1(NewsReport)" puts "Destroy all Bulletin? (Default No)" if STDIN.gets.gsub("\n",'').downcase == 'yes' Bulletin.destroy_all else puts "Did not destroy" end counter = 0 jfile = File.new("#{Rails.root}/lib/import_data/announcements.json").read jdata = JSON.parse(jfile) jdata.each do |data| # puts data # puts DEPARTMENTS[num] if data["department"] == DEPARTMENTS[num] a = Bulletin.new data a.bulletin_category = BulletinCategory.where(:key=>data["category_key"]).first a.save counter = counter +1 end end puts "Imported #{counter}" end end