From 977100e92e6f09815794224ea321cf0c56948410 Mon Sep 17 00:00:00 2001 From: ouyangxuhua Date: Wed, 16 Dec 2015 11:17:06 +0800 Subject: [PATCH] =?UTF-8?q?=E5=8F=8C=E5=87=BB=E8=B5=84=E6=BA=90=E6=A0=8F?= =?UTF-8?q?=E7=9B=AE=E6=A0=87=E7=AD=BE=E5=8F=AF=E7=BC=96=E8=BE=91=EF=BC=8C?= =?UTF-8?q?=E7=BC=96=E8=BE=91=E6=A0=87=E7=AD=BE=E5=90=8E=E6=9B=B4=E6=96=B0?= =?UTF-8?q?=E6=A0=87=E7=AD=BE?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/controllers/tags_controller.rb | 80 +++++++++++++++++++ app/views/files/_subfield_tags.html.erb | 6 +- app/views/files/index.html.erb | 59 ++++++++++++++ .../tags/update_org_subfield_tag_name.js.erb | 11 +++ config/routes.rb | 1 + 5 files changed, 155 insertions(+), 2 deletions(-) create mode 100644 app/views/tags/update_org_subfield_tag_name.js.erb diff --git a/app/controllers/tags_controller.rb b/app/controllers/tags_controller.rb index 511fafdba..d40bc7c7c 100644 --- a/app/controllers/tags_controller.rb +++ b/app/controllers/tags_controller.rb @@ -320,6 +320,86 @@ class TagsController < ApplicationController end end + def update_org_subfield_tag_name + @tag_name = params[:tagName] + @rename_tag_name = params[:renameName] + @taggable_id = params[:taggableId] + @taggable_type = numbers_to_object_type(params[:taggableType]) + @rename_tag = (ActsAsTaggableOn::Tag.find_by_name(@rename_tag_name)) #查找重命名后的tag + @tag_id = (ActsAsTaggableOn::Tag.find_by_name(@tag_name)).id #重命名前的tag_id + @taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,@taggable_id,@taggable_type) unless @taggable_id.blank? + @obj = get_object(@taggable_id,params[:taggableType]) unless @taggable_id.blank? + if @taggable_id.blank? #如果没有传tag_id,那么直接更新tag_name就好了。但是要防止 重命名后的tag存在。 + if params[:org_subfield_id] + org_subfield = OrgSubfield.find params[:org_subfield_id] + if org_subfield + org_subfield.attachments.each do |attachment| + taggings = ActsAsTaggableOn::Tagging.find_by_tag_id_and_taggable_id_and_taggable_type(@tag_id,attachment.id,attachment.class) + if taggings + taggings.delete + attachment.tag_list.add(@rename_tag_name.split(",")) + attachment.save + end + end + end + end + else + if(@rename_tag.nil?) #这次命名的是新的tag + + # 是否还有其他记录 引用了 tag_id + @tagging = ActsAsTaggableOn::Tagging.where("tag_id = #{@tag_id}") + # 如果taggings表中记录为 1 ,那么改变@tag_id对应的tag的名字 + if @tagging.count == 1 + @tag = ActsAsTaggableOn::Tag.find_by_id(@tag_id) + @tag.update_attributes({:name=>@rename_tag_name}) + else #如果tagging表中的记录大于1,那么就要新增tag记录 + + unless @obj.nil? + @obj.tag_list.add(@rename_tag_name.split(",")) + @obj.save + end + #删除原来的对应的taggings的记录 + unless @taggings.nil? + @taggings.delete + end + end + else #这是已有的tag + # 更改taggings记录里的tag_id + unless @taggings.nil? + @taggings.update_attributes({:tag_id=>@rename_tag.id}) + end + end + end + + @obj_flag = params[:taggableType] + if @obj && @obj_flag == '6' && @obj.container.kind_of?(OrgSubfield) + @org_subfield = @obj.container + @tag_list = @tag_list = get_org_subfield_tag_list @org_subfield + elsif params[:org_subfield_id] + @org_subfield = OrgSubfield.find(params[:org_subfield_id]) + @tag_list = get_org_subfield_tag_list @org_subfield + + #这里要引用FilesController里的逻辑了。将资源库当前的文件列表刷新一遍。 + @flag = params[:flag] || false + sort = "" + @sort = "" + @order = "" + @is_remote = false + @isproject = false + + sort = "#{Attachment.table_name}.created_on desc" + + @containers = [ OrgSubfield.includes(:attachments).reorder(sort).find(@org_subfield.id)] + + show_attachments @containers + elsif @obj && @obj_flag == '5' + @forum = @obj + end + respond_to do |format| + format.js + end + end + def show_attachments obj @attachments = [] obj.each do |container| diff --git a/app/views/files/_subfield_tags.html.erb b/app/views/files/_subfield_tags.html.erb index b037fcaf4..ed7ad660e 100644 --- a/app/views/files/_subfield_tags.html.erb +++ b/app/views/files/_subfield_tags.html.erb @@ -12,13 +12,15 @@ <% else%> <%= k%>(<%= v%>) <% end%> <% end%> <% end%> diff --git a/app/views/tags/update_org_subfield_tag_name.js.erb b/app/views/tags/update_org_subfield_tag_name.js.erb new file mode 100644 index 000000000..4fabc1afd --- /dev/null +++ b/app/views/tags/update_org_subfield_tag_name.js.erb @@ -0,0 +1,11 @@ +<% if @obj %> + $("#tags_show-<%=@obj.class%>-<%=@obj.id%>").empty(); + $("#tags_show-<%=@obj.class%>-<%=@obj.id%>").html('<%= escape_javascript(render :partial => 'tags/tag_list', + :locals => {:obj => @obj,:object_flag => @obj_flag}) %>'); + $("#tags_name_<%=@obj.id%>").val(""); + $("#add_tag_<%=@obj.id%>").hide(); + $("#files_tag").html("<%= escape_javascript(render :partial => "files/subfield_tags", :locals => {:tag_list => @tag_list,:org_subfield => @org_subfield,:tag_name => @select_tag_name}) %>"); +<% else %> + $("#org_subfield_list").html("<%= escape_javascript(render :partial => 'files/org_subfield_list', + :locals => {org_subfield: @org_subfield,all_attachments: @all_attachments,sort:@sort,order:@order,org_subfield_attachments:@obj_attachments}) %>"); +<% end %> diff --git a/config/routes.rb b/config/routes.rb index 28a3fec70..6dce046de 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1047,6 +1047,7 @@ RedmineApp::Application.routes.draw do match 'tags/remove_tag_new', :as=>"remove_tag_new" match 'tags/tag_save', :as => "save_tag" match 'tags/update_tag_name',:as => "update_tag_name" + match 'tags/update_org_subfield_tag_name' match 'words/add_brief_introdution'