| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  | namespace :new_ui do | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |   MODULE_APPS = [ %w(announcement bulletin), | 
					
						
							|  |  |  |                   %w(archive archive_file), | 
					
						
							|  |  |  |                   %w(ask ask_question ask_category), | 
					
						
							|  |  |  |                   %w(asset asset), | 
					
						
							|  |  |  |                   %w(faq qa), | 
					
						
							|  |  |  |                   %w(gallery gallery_album gallery_category), | 
					
						
							|  |  |  |                   %w(location location), | 
					
						
							|  |  |  |                   %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) | 
					
						
							|  |  |  |                 ] # app_key, model_name, category_name | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   task :migrate => :environment do | 
					
						
							|  |  |  |     @db = Mongoid.database | 
					
						
							|  |  |  |     migrate_tags | 
					
						
							|  |  |  |     migrate_categories | 
					
						
							|  |  |  |     migrate_approval_on_object | 
					
						
							|  |  |  |     migrate_managers | 
					
						
							| 
									
										
										
										
											2013-10-18 07:25:19 +00:00
										 |  |  |     migrate_ad_images_dates | 
					
						
							| 
									
										
										
										
											2013-10-20 10:44:04 +00:00
										 |  |  |     migrate_ad_banners | 
					
						
							| 
									
										
										
										
											2013-10-21 03:13:15 +00:00
										 |  |  |     save_pages | 
					
						
							| 
									
										
										
										
											2013-10-23 07:51:49 +00:00
										 |  |  |     menu_enabled_for_to_hash | 
					
						
							| 
									
										
										
										
											2013-10-24 13:29:02 +00:00
										 |  |  |     link_url_to_hash | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |   # :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| | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |     migrate_categories(args) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   task :migrate_tags => :environment do | 
					
						
							|  |  |  |     migrate_tags | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   task :fix_tagged_ids => :environment do | 
					
						
							|  |  |  |     fix_tagged_ids | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-30 08:36:21 +00:00
										 |  |  |   task :migrate_approval_on_object => :environment do | 
					
						
							|  |  |  |     migrate_approval_on_object | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   task :migrate_managers => :environment do | 
					
						
							|  |  |  |     migrate_managers | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 07:25:19 +00:00
										 |  |  |   task :migrate_ad_images_dates  => :environment do | 
					
						
							|  |  |  |     migrate_ad_images_dates | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-19 12:20:40 +00:00
										 |  |  |   task :migrate_ad_banners  => :environment do | 
					
						
							|  |  |  |     migrate_ad_banners | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 10:44:04 +00:00
										 |  |  |   task :save_pages => :environment do | 
					
						
							|  |  |  |     save_pages | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 07:51:49 +00:00
										 |  |  |   task :menu_enabled_for_to_hash => :environment do | 
					
						
							|  |  |  |     menu_enabled_for_to_hash | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 10:28:09 +00:00
										 |  |  |   task :link_url_to_hash => :environment do | 
					
						
							|  |  |  |     link_url_to_hash | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |   def migrate_categories(args = nil) | 
					
						
							|  |  |  |     if args && args[:app_key] && args[:model_name] | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |       migrate_category(args[:app_key], args[:model_name], args[:category_name]) | 
					
						
							|  |  |  |     else | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |       MODULE_APPS.each{|value_array| migrate_category(value_array[0], value_array[1], value_array[2])} | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |   def migrate_category(app_key, model_name, category_name = nil) | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     category_name = "#{model_name}_category" unless category_name | 
					
						
							|  |  |  |     collection_category = @db[category_name.pluralize] | 
					
						
							|  |  |  |     categories = collection_category.find.entries if collection_category | 
					
						
							|  |  |  |     collection_model = @db[model_name.pluralize] | 
					
						
							|  |  |  |     collection_buffer_category = @db['buffer_categories'] | 
					
						
							|  |  |  |     collection_prototype_auth = @db['prototype_auths'] | 
					
						
							|  |  |  |     if categories.present? | 
					
						
							|  |  |  |       module_app = ModuleApp.where(key: app_key).first | 
					
						
							|  |  |  |       if module_app.try(:has_category) | 
					
						
							|  |  |  |         categories.each do |category| | 
					
						
							|  |  |  |           new_category = module_app.categories.build | 
					
						
							|  |  |  |           new_category.title_translations = category['title'] | 
					
						
							|  |  |  |           new_category.disable = category['disable'] | 
					
						
							|  |  |  |           new_category.save | 
					
						
							|  |  |  |           collection_model.find("#{category_name}_id" => category['_id']).each do |object| | 
					
						
							|  |  |  |             object['category_id'] = new_category.id | 
					
						
							|  |  |  |             object.delete("#{category_name}_id") | 
					
						
							|  |  |  |             collection_model.save object | 
					
						
							|  |  |  |             collection_buffer_category.save ({ '_type' => 'BufferCategory', 'category_id' => new_category.id, 'categorizable_type' => model_name.camelize, 'categorizable_id' => object['_id'] }) | 
					
						
							| 
									
										
										
										
											2013-07-02 09:26:02 +00:00
										 |  |  |           end | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |           object_auths = collection_prototype_auth.find(_type: 'ObjectAuth', obj_authable_id: category['_id'], '$or' => [{privilege_user_ids: {'$not' => {'$size' => 0}}}, {role_ids: {'$not' => {'$size' => 0}}}, {sub_role_ids: {'$not' => {'$size' => 0}}}]).entries | 
					
						
							|  |  |  |           if object_auths.present? | 
					
						
							|  |  |  |             object_auths.each do |object_auth| | 
					
						
							|  |  |  |               if object_auth['title'].eql?('fact_check') | 
					
						
							|  |  |  |                 authorization = new_category.auth_approval || new_category.create_auth_approval(module_app_id: module_app.id, title: "category_approval_#{module_app.key}")    | 
					
						
							|  |  |  |               else | 
					
						
							|  |  |  |                 authorization = new_category.auth_sub_manager || new_category.create_auth_sub_manager(module_app_id: module_app.id, title: "category_authorization_#{module_app.key}") | 
					
						
							|  |  |  |               end | 
					
						
							|  |  |  |               add_users(object_auth, authorization) | 
					
						
							|  |  |  |               collection_prototype_auth.remove(object_auth) | 
					
						
							| 
									
										
										
										
											2013-07-22 10:39:24 +00:00
										 |  |  |             end | 
					
						
							| 
									
										
										
										
											2013-07-02 09:26:02 +00:00
										 |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |         collection_category.drop | 
					
						
							|  |  |  |         collection_prototype_auth.remove(obj_authable_type: category_name.classify) | 
					
						
							| 
									
										
										
										
											2013-07-02 09:26:02 +00:00
										 |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |     else | 
					
						
							|  |  |  |       collection_category.drop | 
					
						
							|  |  |  |       collection_prototype_auth.remove(obj_authable_type: category_name.classify) | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |   def fix_tagged_ids | 
					
						
							|  |  |  |     Tag.all.each do |tag| | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |       tag.taggings.each do |tagging| | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |         tagged_ids = tagging.taggable.tagged_ids | 
					
						
							|  |  |  |         unless tagged_ids.include?(tag.id.to_s) | 
					
						
							|  |  |  |           tagged_ids << tag.id.to_s | 
					
						
							|  |  |  |           tagging.taggable.update_attribute(:tagged_ids, tagged_ids) | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def migrate_tags | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     tags = Tag.where(_type: 'Tag').entries | 
					
						
							|  |  |  |     tags.each_with_index do |tag, i| | 
					
						
							|  |  |  |       unless tag.tag_lease | 
					
						
							|  |  |  |         if 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| | 
					
						
							|  |  |  |             tagged_ids = tagging.taggable.tagged_ids | 
					
						
							|  |  |  |             unless tagged_ids.include?(tag.id.to_s) | 
					
						
							|  |  |  |               tagged_ids << tag.id.to_s | 
					
						
							|  |  |  |               tagging.taggable.update_attribute(:tagged_ids, tagged_ids) | 
					
						
							|  |  |  |             end | 
					
						
							|  |  |  |             tagging.taggable.taggings.create(tag_id: n_tag.id) | 
					
						
							|  |  |  |           end | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |         tag.destroy | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |     old_tags = Tag.where(_type: {'$ne' => 'Tag'}).delete | 
					
						
							|  |  |  |     @db = Mongoid.database | 
					
						
							|  |  |  |     @db['proto_tags'].drop | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |   def migrate_approval_on_object | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     collection_prototype_auth = @db['prototype_auths'] | 
					
						
							|  |  |  |     object_auths = collection_prototype_auth.find({_type: 'ObjectAuth', obj_authable_type: {'$in' => ["AdBanner", "PageContext"]}, '$or' => [{privilege_user_ids: {'$not' => {'$size' => 0}}}, {role_ids: {'$not' => {'$size' => 0}}}, {sub_role_ids: {'$not' => {'$size' => 0}}}]}).entries | 
					
						
							|  |  |  |     object_auths.each do |object_auth| | 
					
						
							|  |  |  |       case object_auth['obj_authable_type'] | 
					
						
							|  |  |  |       when 'AdBanner' | 
					
						
							|  |  |  |         module_app = ModuleApp.where(key: 'ad_banner').first | 
					
						
							|  |  |  |       when 'PageContext' | 
					
						
							|  |  |  |         module_app = ModuleApp.where(key: 'page_content').first | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       if module_app | 
					
						
							|  |  |  |         object = object_auth['obj_authable_type'].constantize.find(object_auth['obj_authable_id']) rescue nil | 
					
						
							|  |  |  |         if object  | 
					
						
							|  |  |  |           authorization = object.create_auth_sub_manager(module_app_id: module_app.id, title: "#{object_auth['obj_authable_type'].underscore}_authorization_#{module_app.key}") | 
					
						
							|  |  |  |           add_users(object_auth, authorization) if authorization | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       collection_prototype_auth.remove(object_auth) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |     collection_prototype_auth.remove(obj_authable_type: {'$in' => ["AdBanner", "PageContext"]}) | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def add_users(object_auth, authorization)     | 
					
						
							|  |  |  |     if authorization | 
					
						
							|  |  |  |       if object_auth['role_ids'].present? | 
					
						
							|  |  |  |         roles = Role.find(object_auth['role_ids']) rescue nil | 
					
						
							|  |  |  |         authorization.add_roles(roles) if roles | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       if object_auth['sub_role_ids'].present? | 
					
						
							|  |  |  |         sub_roles = SubRole.find(object_auth['sub_role_ids']) rescue nil | 
					
						
							|  |  |  |         authorization.add_sub_roles(sub_roles) if sub_roles | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       if object_auth['privilege_user_ids'].present? | 
					
						
							|  |  |  |         users = User.find(object_auth['privilege_user_ids']) rescue nil | 
					
						
							|  |  |  |         authorization.add_users(users) if users | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							|  |  |  |   def migrate_managers | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     collection_app_manager = @db['app_managers'] | 
					
						
							|  |  |  |     app_managers = collection_app_manager.find(managing_app_id: {'$not' => {'$size' => 0}}).entries | 
					
						
							|  |  |  |     app_managers.each do |app_manager| | 
					
						
							|  |  |  |       module_app = ModuleApp.find(app_manager['managing_app_id']) rescue nil | 
					
						
							|  |  |  |       user = User.find(app_manager['user_id']) rescue nil | 
					
						
							|  |  |  |       if module_app && user | 
					
						
							|  |  |  |         authorization = module_app.auth_manager || module_app.create_auth_manager(module_app_id: module_app.id, title: module_app.key) | 
					
						
							|  |  |  |         authorization.add_users(user) | 
					
						
							|  |  |  |       end | 
					
						
							| 
									
										
										
										
											2013-07-25 09:36:55 +00:00
										 |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-10-15 15:38:16 +00:00
										 |  |  |     collection_app_manager.drop | 
					
						
							| 
									
										
										
										
											2013-07-25 09:36:55 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-07 08:23:07 +00:00
										 |  |  |   task :reset_user_position => :environment do | 
					
						
							| 
									
										
										
										
											2013-10-07 10:23:54 +00:00
										 |  |  |     User.where(email: /guest/).each{|d|d.update_attribute(:position, nil)} | 
					
						
							| 
									
										
										
										
											2013-10-07 08:23:07 +00:00
										 |  |  |     User.not_guest_user.each_with_index do |user, i| | 
					
						
							|  |  |  |       user.update_attribute(:position, i) | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-09-26 10:59:23 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-18 07:25:19 +00:00
										 |  |  |   def migrate_ad_images_dates | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     collection_ad_image = @db['ad_images'] | 
					
						
							|  |  |  |     ad_images = collection_ad_image.find() | 
					
						
							|  |  |  |     ad_images.each do |ad_image| | 
					
						
							|  |  |  |       ad_image['postdate'] = ad_image['post_date'] | 
					
						
							|  |  |  |       ad_image['deadline'] = ad_image['unpost_date'] | 
					
						
							|  |  |  |       ad_image.delete('post_date') | 
					
						
							|  |  |  |       ad_image.delete('unpost_date') | 
					
						
							|  |  |  |       collection_ad_image.save(ad_image) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-19 12:20:40 +00:00
										 |  |  |   def migrate_ad_banners | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     collection_ad_banner = @db['ad_banners'] | 
					
						
							|  |  |  |     ad_banners = collection_ad_banner.find() | 
					
						
							|  |  |  |     ad_banners.each do |ad_banner| | 
					
						
							|  |  |  |       ad_banner['timeout'] = ad_banner['transition_msec']/1000 if ad_banner['transition_msec'].present? | 
					
						
							|  |  |  |       ad_banner['speed'] = 500 unless ad_banner['speed'].present? | 
					
						
							|  |  |  |       ad_banner['width'], ad_banner['height'] = ad_banner['best_size'].gsub('px', '').delete(' ').split('x').map{|d| d.to_i} if ad_banner['best_size'].present? | 
					
						
							|  |  |  |       ad_banner['ad_fx'] = (AdBanner::FX_TYPES.include?(ad_banner['ad_fx']) ? ad_banner['ad_fx'] : AdBanner::FX_TYPES[0]) | 
					
						
							|  |  |  |       ad_banner.delete('best_size') | 
					
						
							|  |  |  |       ad_banner.delete('transition_msec') | 
					
						
							|  |  |  |       collection_ad_banner.save(ad_banner) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-20 10:44:04 +00:00
										 |  |  |   def save_pages | 
					
						
							| 
									
										
										
										
											2013-10-20 11:05:19 +00:00
										 |  |  |     Page.all.each do |page|  | 
					
						
							|  |  |  |       page.save | 
					
						
							|  |  |  |     end | 
					
						
							| 
									
										
										
										
											2013-10-20 10:44:04 +00:00
										 |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 07:51:49 +00:00
										 |  |  |   def menu_enabled_for_to_hash | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     collection_item = @db['items'] | 
					
						
							|  |  |  |     items = collection_item.find() | 
					
						
							|  |  |  |     items.each do |item| | 
					
						
							|  |  |  |       item['menu_enabled_for'] = \ | 
					
						
							|  |  |  |       if item['menu_enabled_for'].present? | 
					
						
							|  |  |  |         VALID_LOCALES.inject({}) do |enable, locale| | 
					
						
							| 
									
										
										
										
											2013-10-30 09:26:31 +00:00
										 |  |  |           enable[locale] = Array(item['menu_enabled_for']).include?(locale.to_s) | 
					
						
							| 
									
										
										
										
											2013-10-23 07:51:49 +00:00
										 |  |  |           enable | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       else | 
					
						
							|  |  |  |         VALID_LOCALES.inject({}) do |enable, locale| | 
					
						
							|  |  |  |           enable[locale] = false | 
					
						
							|  |  |  |           enable | 
					
						
							|  |  |  |         end | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       collection_item.save(item) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-10-23 10:28:09 +00:00
										 |  |  |   def link_url_to_hash | 
					
						
							|  |  |  |     @db ||= Mongoid.database | 
					
						
							|  |  |  |     collection_link = @db['items'] | 
					
						
							|  |  |  |     links = collection_link.find({_type: 'Link'}) | 
					
						
							|  |  |  |     links.each do |link| | 
					
						
							|  |  |  |       link['url'] = VALID_LOCALES.inject({}) do |enable, locale| | 
					
						
							|  |  |  |         enable[locale] = link['url'] | 
					
						
							|  |  |  |         enable | 
					
						
							|  |  |  |       end | 
					
						
							|  |  |  |       collection_link.save(link) | 
					
						
							|  |  |  |     end | 
					
						
							|  |  |  |   end | 
					
						
							|  |  |  | 
 | 
					
						
							| 
									
										
										
										
											2013-07-02 08:46:44 +00:00
										 |  |  | end |