require ‘csv’ @data_path = ‘data/’
namespace :db do
task(:load_forum => :environment){ ForumCategory.categories({none:0},0) ForumTopic.topics({category:'select category'},0) file_name='app_data/forum_base.csv' load_the_forum_file(file_name) } task(:load_help => :environment){ ForumCategory.categories({none:0},0) ForumTopic.topics({category:'select category'},0) file_name='app_data/forum_help.csv' load_the_forum_file(file_name) }
end
def load_the_forum_file(file_name)
CSV.foreach(file_name, headers: true) { |row| the_row=row.to_hash category_id=ForumCategory.where(name:the_row['category']).pluck(:id)[0] category_id=ForumCategory.create(name:the_row['category']).id if category_id.nil? topic_id=ForumTopic.where(name:the_row['topic']).pluck(:id)[0] topic_id=ForumTopic.create(name:the_row['topic'],forum_category_id:category_id).id if topic_id.nil? the_row.delete('category') the_row.delete('topic') the_row.store(:forum_topic_id,topic_id) Forum.create(the_row) } topics=ForumTopic.all.pluck(:id) topics.each{|topic| list=named_array(Forum.where(forum_topic_id:topic).pluck(:id,:parent,:tag),[:id,:parent,:tag],d2=true) list.each{ |comment| Forum.where(parent:comment[:tag],needs_translating:false).update_all(parent:comment[:id],needs_translating:true) } } Forum.all.update_all(needs_translating:false,tag:-1)
end
def named_array(data,params,d2)
temp=Array.new{{}} d2 ? size=data.size : size=1 (0...size).each {|i| temp[i]={} d2 ? datax=data[i] : datax=data (0...datax.size).each { |j| temp[i].store(params[j], datax[j]) } unless datax.nil? } d2 ? temp : temp[0]
end