class Admin::ModuleTagsController < OrbitBackendController include OrbitTag::Merging open_for_manager helper 'admin/tags' def index @tags = @module_app.module_tags @default_tags = get_default_tags end def new @default_tags = get_default_tags_without_included @tag = ModuleTag.new render layout: false end def edit @tag = ModuleTag.find(params[:id]) render layout: false end def create @tag = @module_app.module_tags.build(params[:module_tag]) if @tag.save redirect_to admin_module_tags_url(module_app_id: @module_app_id) else @tag = ModuleTag.new(params[:module_tag]) flash.now[:error] = t('create.error.tag') render :action => "new" end end def update @tag = ModuleTag.find(params[:id]) if @tag.update_attributes(params[:module_tag]) redirect_to admin_module_tags_url(module_app_id: @module_app_id) else flash.now[:error] = t('update.error.tag') render :action => "edit" end end def delete_tags tags = ModuleTag.find(params[:ids].split(',')) rescue nil if tags tags.each{|t|t.destroy} end redirect_to admin_module_tags_url(module_app_id: @module_app_id) end def merge module_tags = ModuleTag.find(params[:ids]) taggings = module_tags.map{|t| t.tag.taggings}.flatten module_tags name = params[:name] merge_tags(name, taggings, @module_app_id) module_tags.each(&:destroy) redirect_to admin_module_tags_url(module_app_id: @module_app_id) end def remove_default module_tags = ModuleTag.find(params[:ids].split(',')) rescue nil if module_tags module_tags.each(&:destroy) end redirect_to admin_module_tags_url(module_app_id: @module_app_id) end def update_included_default default_tags = ModuleTag.find(params[:ids].split(',')) rescue nil default_tags.each do |tag| clone_tag_from_default(tag) end redirect_to admin_module_tags_url(module_app_id: @module_app_id) end protected def setup_vars @module_app = ModuleApp.find(params[:module_app_id]) rescue nil raise ModuleAppError, 'Can not find ModuleApp' if @module_app.nil? @module_app_id = @module_app.id rescue nil end def get_default_tags_without_included ModuleTag.where(is_default: true, parent_id: nil) - @module_app.module_tags.where(is_default: true).map{|m| m.parent } end def get_default_tag_ids get_default_tags.map{|t| t.id} end end