73 lines
2.8 KiB
Ruby
73 lines
2.8 KiB
Ruby
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
|
|
categories = "#{category_name}_categories".classify.constantize.admin_manager_all
|
|
categories = "#{category_name}_categories".classify.constantize.all if categories.blank?
|
|
module_app = ModuleApp.where(key: app_key)[0]
|
|
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
|
|
category.destroy
|
|
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
|