namespace :new_ui do # :category_name is optional, depends on the naming of the category model: if no conventional, specify it task :migrate_categories, [:app_key, :model_name, :category_name] => :environment do |t, args| @db = Mongoid.database if args[:app_key] && args[:model_name] migrate_category(args[:app_key], args[:model_name], args[:category_name]) else module_apps = [ %w(announcement bulletin), %w(archive archive_file), %w(ask ask_question ask), %w(asset asset), %w(faq qa), %w(gallery gallery_album gallery), %w(personal_book writing_book), %w(personal_conference writing_conference), %w(personal_experience experience), %w(personal_honor honor), %w(personal_journal writing_journal), %w(personal_patent writing_patent), %w(personal_project project), %w(web_resource web_link) ] module_apps.each{|value_array| migrate_category(value_array[0], value_array[1], value_array[2])} end end def migrate_category(app_key, model_name, category_name=nil) category_name = model_name unless category_name # # coll_cat = @db["#{category_name}_categories"] # # categories = coll_cat.find if coll_cat module_app = ModuleApp.where(key: app_key)[0] if module_app.has_category categories = "#{category_name}_categories".classify.constantize.admin_manager_all rescue nil categories = "#{category_name}_categories".classify.constantize.all if categories.blank? categories.each do |category| new_category = module_app.categories.build new_category.title_translations = category['title'] new_category.disable = category['disable'] new_category.save model_name.classify.constantize.where("#{category_name}_category_id" => category['_id']).each do |object| if object.id.is_a?(String) object.destroy else object.category = new_category.id object.remove_attribute("#{category_name}_category_id") object.save end end if object_auths = ObjectAuth.where(obj_authable_type: "#{category_name.classify}Category") object_auths.each do |object_auth| case object_auth.title when 'fact_check' new_object_auth = new_category.get_object_auth_by_title("approval_#{module_app.key}") else new_object_auth = new_category.get_object_auth_by_title("submit_#{module_app.key}") end object_auth.auth_users.each do |user| new_object_auth.add_user_to_privilege_list(user) end object_auth.destroy end end category.destroy end end end task :migrate_tags => :environment do tags = Tag.where(_type: 'Tag') tags.each do |tag| module_app = ModuleApp.find(tag.module_tag_id) new_tag = module_app.module_tags.build new_tag.name_translations = tag['name'] new_tag.is_default = tag['is_default'] new_tag.save n_tag = new_tag.tag n_tag.cloud_view_count = tag['cloud_view_count'] n_tag.view_count = tag['view_count'] n_tag.save tag.taggings.each do |tagging| tagging.taggable.taggings.create(tag_id: n_tag.id) end tag.destroy end end end