diff --git a/app/assets/javascripts/lib/tags.js.erb b/app/assets/javascripts/lib/tags.js.erb index 044a61ac3..e8b4a1251 100644 --- a/app/assets/javascripts/lib/tags.js.erb +++ b/app/assets/javascripts/lib/tags.js.erb @@ -64,16 +64,16 @@ function checkedLength() { if($defaultTags.length > 0 && $moduleTags.length > 0) { $('#deleteTags').attr('rel', ""); $('#deleteTags').addClass('hide'); - $('#removeDefault').attr('rel', ""); - $('#removeDefault').addClass('hide'); + $('#removeDefaults').attr('rel', ""); + $('#removeDefaults').addClass('hide'); $('#mergerTags').addClass('hide'); } else if ($defaultTags.length > 0) { var ids = new Array(); $defaultTags.each(function(i) { ids.push($defaultTags.eq(i).parent().siblings('.tag_id').val()); }); - $('#removeDefault').attr('rel', "<%= Rails.application.routes.url_helpers.remove_default_admin_module_tags_path %>" + "?module_app_id=" + $('#module_app_id').val() + "&ids=" + ids); - $('#removeDefault').removeClass('hide'); + $('#removeDefaults').attr('rel', "<%= Rails.application.routes.url_helpers.remove_default_admin_module_tags_path %>" + "?module_app_id=" + $('#module_app_id').val() + "&ids=" + ids); + $('#removeDefaults').removeClass('hide'); } else { var ids = new Array(); $moduleTags.each(function(i) { @@ -91,8 +91,8 @@ function checkedLength() { $('#deselect').addClass('hide'); $('#deleteTags').attr('rel', ""); $('#deleteTags').addClass('hide'); - $('#removeDefault').attr('rel', ""); - $('#removeDefault').addClass('hide'); + $('#removeDefaults').attr('rel', ""); + $('#removeDefaults').addClass('hide'); $('#mergerTags').addClass('hide'); $('#deselect').off('click', deselect); }; @@ -120,8 +120,23 @@ function checkedLength() { }); $('#delete_tags').modal('show', cleanTagInputs()); function cleanTagInputs(){ - var $tagsMerger = $('#delete_tags'), - $filterItem = $tagsMerger.find('.filter-item'); + var $tagsDelete = $('#delete_tags'), + $filterItem = $tagsDelete.find('.filter-item'); + $filterItem.find('.card, .amount').remove(); + $filterItem.find('a').removeAttr('class'); + $filterItem.find('a').removeAttr('href'); + } + }); + $('#removeDefaults').on('click', function() { + $('#remove_defaults .tags-groups').empty(); + $('#remove_defaults a.remove-defaults').attr("href", $(this).attr("rel")); + $defaultTags.each(function(i) { + $defaultTags.eq(i).parents('.filter-item').clone().appendTo('#remove_defaults .tags-groups'); + }); + $('#remove_defaults').modal('show', cleanTagInputs()); + function cleanTagInputs(){ + var $removeDefaults = $('#remove_defaults'), + $filterItem = $removeDefaults.find('.filter-item'); $filterItem.find('.card, .amount').remove(); $filterItem.find('a').removeAttr('class'); $filterItem.find('a').removeAttr('href'); diff --git a/app/controllers/admin/module_tags_controller.rb b/app/controllers/admin/module_tags_controller.rb index 6d83685df..ad7eb1a76 100644 --- a/app/controllers/admin/module_tags_controller.rb +++ b/app/controllers/admin/module_tags_controller.rb @@ -7,44 +7,42 @@ class Admin::ModuleTagsController < OrbitBackendController helper 'admin/tags' def index - @tags = get_tags @included_tags = @module_app.module_tags.where(is_default: true) + @tags = @module_app.module_tags @default_tags = get_default_tags end def new - @tag = Tag.new + @tag = ModuleTag.new render layout: false end def edit - @tag = Tag.find(params[:id]) + @tag = ModuleTag.find(params[:id]) render layout: false end def create - @tag = @module_app.module_tags.build(params[:tag]) + @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 = Tag.new(params[:tag]) - flash.now[:error] = t('create.error.tag') + @tag = ModuleTag.new(params[:module_tag]) render :action => "new" end end def update - @tag = Tag.find(params[:id]) - if @tag.update_attributes(params[:tag]) + @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.link') render :action => "edit" end end def delete_tags - tags = Tag.find(params[:ids].split(',')) rescue nil + tags = ModuleTag.find(params[:ids].split(',')) rescue nil if tags tags.each{|t|t.destroy} end @@ -52,23 +50,20 @@ class Admin::ModuleTagsController < OrbitBackendController end def merge - tags = Tag.find(params[:ids]) - taggings = tags.map{|t| t.taggings}.flatten - tag_leases = tags.map{|t| t.tag_lease} + 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) - tag_leases.each(&:destroy) + module_tags.each(&:destroy) redirect_to admin_module_tags_url(module_app_id: @module_app_id) end def remove_default - debugger - # tags = Tag.find(params[:ids].split(',')) rescue nil - # if tags - # tag_leases = tags.map{|t| t.tag_lease}.flatten - # tag_leases.each {|t| t.update_attribute(:module_app_ids, t.module_app_ids - [@module_app.id])} - # tags.each {|t| t.update_attribute(:is_default, true)} - # end + 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 diff --git a/app/controllers/admin/tags_controller.rb b/app/controllers/admin/tags_controller.rb index 73d0401a5..cc9e4c8f2 100644 --- a/app/controllers/admin/tags_controller.rb +++ b/app/controllers/admin/tags_controller.rb @@ -5,34 +5,34 @@ class Admin::TagsController < OrbitBackendController before_filter :for_app_sub_manager, except: [:index] def index - @tags = get_tags + @tags = @module_app.module_tags @module_apps = ModuleApp.where(has_tag: true) end def new - @tag = Tag.new + @tag = ModuleTag.new render layout: false end def edit - @tag = Tag.find(params[:id]) + @tag = ModuleTag.find(params[:id]) render layout: false end def create - @tag = @module_app.module_tags.build(params[:tag].merge(is_default: true)) + @tag = @module_app.module_tags.build(params[:module_tag].merge(is_default: true)) if @tag.save redirect_to admin_tags_url else - @tag = Tag.new(params[:tag]) + @tag = Tag.new(params[:module_tag]) flash.now[:error] = t('create.error.tag') render :action => "new" end end def update - @tag = Tag.find(params[:id]) - if @tag.update_attributes(params[:tag]) + @tag = ModuleTag.find(params[:id]) + if @tag.update_attributes(params[:module_tag]) redirect_to action: :index else flash.now[:error] = t('update.error.link') @@ -41,35 +41,48 @@ class Admin::TagsController < OrbitBackendController end def add_to_default - tags = Tag.find(params[:ids].split(',')) rescue nil - if tags - tag_leases = tags.map{|t| t.tag_lease}.flatten - tags.each {|t| t.update_attribute(:is_default, true)} - tag_leases.each {|t| t.update_attribute(:module_app_ids, t.module_app_ids << @module_app.id)} + module_tags = ModuleTag.find(params[:ids].split(',')) rescue nil + if module_tags + module_tags.each do |module_tag| + module_tag.update_attribute(:is_default, true) + new_tag = module_tag.clone + new_tag['_id'] = BSON::ObjectId.new + new_tag['module_app_id'] = @module_app.id + new_tag.save + module_tag.update_attribute(:parent_id, new_tag.id) + end end redirect_to admin_tags_url end def delete_tags - tags = Tag.find(params[:ids].split(',')) rescue nil + tags = ModuleTag.find(params[:ids].split(',')) rescue nil if tags - tags.each{|t|t.destroy} + tags.each(&:destroy) end redirect_to admin_tags_url end def merge - tags = Tag.find(params[:ids]) - taggings = tags.map{|t| t.taggings}.flatten - tag_leases = tags.map{|t| t.tag_lease} + module_tags = ModuleTag.find(params[:ids]) + tags = module_tags.map{|t| t.tag} + taggings, is_default = tags.inject([[], []]) do |values, tag| + values[0] << tag.taggings + values[1] << tag.is_default + values + end + taggings.flatten! + is_default.uniq! name = params[:name] - module_app_ids = tag_leases.map{|t| t.module_app_ids}.flatten.uniq - if tags.detect{|t| t.is_default == true} || module_app_ids.count > 1 - merge_default_tags(name, taggings, module_app_ids) + module_app_ids = module_tags.map{|t| t.module_app_id}.flatten.uniq + if is_default.size == 1 && is_default[0] == true + merge_default_tags(name, module_tags) + elsif is_default.include?(true) || module_app_ids.count > 1 + merge_with_default_tags(name, module_tags) else merge_tags(name, taggings, module_app_ids[0]) + module_tags.each(&:destroy) end - tag_leases.each(&:destroy) redirect_to admin_tags_url end diff --git a/app/helpers/admin/tags_helper.rb b/app/helpers/admin/tags_helper.rb index afdcaaebf..d42a50065 100644 --- a/app/helpers/admin/tags_helper.rb +++ b/app/helpers/admin/tags_helper.rb @@ -7,4 +7,16 @@ module Admin::TagsHelper span_names.join(" / ").html_safe end + def get_tagging_count(module_tag) + if module_tag.is_default && module_tag.parent.blank? + count = module_tag.children.inject(0) do |count, child| + count += child.tag.taggings.count + count + end + else + count = module_tag.tag.taggings.count + end + count + end + end diff --git a/app/models/module_app.rb b/app/models/module_app.rb index d8fbae359..aea7e49f2 100644 --- a/app/models/module_app.rb +++ b/app/models/module_app.rb @@ -8,7 +8,7 @@ class ModuleApp field :title field :sidebar_order,type: Integer,default: 0 - has_and_belongs_to_many :module_tags, dependent: :destroy + has_many :module_tags, dependent: :destroy def refetch_setting!(reg) # %w{module_label category base_url version organization author intro update_info create_date}.each do |field| diff --git a/app/models/module_tag.rb b/app/models/module_tag.rb index 415a9cd14..40c17f897 100644 --- a/app/models/module_tag.rb +++ b/app/models/module_tag.rb @@ -3,20 +3,22 @@ class ModuleTag include Mongoid::Timestamps field :is_default, type: Boolean, default: false + field :name, localize: true has_one :tag, as: :tag_lease, autosave: true - has_and_belongs_to_many :module_apps + belongs_to :module_app - after_initialize :init_tag + belongs_to :parent, :class_name => 'ModuleTag', :inverse_of => :children + has_many :children, :class_name => 'ModuleTag', :inverse_of => :parent, autosave: true, dependent: :destroy + + validates :name, :at_least_one => true + + before_save :init_update_tag before_destroy :destroy_tag - private + after_save :update_children - def init_tag - if new_record? - self.build_tag(name_translations: self.name_translations, is_default: self.is_default) - end - end + private def destroy_tag Tag.without_callback(:destroy, :before, :destroy_module_tag) do @@ -24,4 +26,20 @@ class ModuleTag end end -end \ No newline at end of file + def init_update_tag + if self.new_record? + self.build_tag(name_translations: self.name_translations) + else + self.tag.name_translations = self.name_translations + end + end + + def update_children + unless self.children.blank? + self.children.each do |child| + child.update_attributes(name_translations: self.name_translations) + end + end + end + +end diff --git a/app/views/admin/module_tags/_index.html.erb b/app/views/admin/module_tags/_index.html.erb index 410cb7c49..9501845d5 100644 --- a/app/views/admin/module_tags/_index.html.erb +++ b/app/views/admin/module_tags/_index.html.erb @@ -6,7 +6,7 @@ <%= link_to t(:delete), '#', id: "deleteTags", class: "btn btn-danger toggable hide", rel: '' %> <%= link_to t(:merge), '#', id: "mergerTags", class: "btn btn-success toggable hide", rel: merge_admin_module_tags_path(module_app_id: @module_app_id) %> - <%= link_to t(:remove_default), remove_default_admin_module_tags_path(module_app_id: @module_app_id), id: "removeDefault", class: "btn btn-info toggable hide", method: :post %> + <%= link_to t(:remove_default), '#', id: "removeDefaults", class: "btn btn-info toggable hide", rel: '' %> <%= link_to content_tag(:i, nil, class: "icons-plus") + " " + t(:add), new_admin_module_tag_path(module_app_id: @module_app_id), class: "btn btn-primary open-slide" %> @@ -16,7 +16,7 @@
@@ -28,4 +28,23 @@ <%= render 'admin/tags/delete_modal' %> - \ No newline at end of file + + + + + \ No newline at end of file diff --git a/app/views/admin/module_tags/_module_tag.html.erb b/app/views/admin/module_tags/_module_tag.html.erb new file mode 100644 index 000000000..9b617ecab --- /dev/null +++ b/app/views/admin/module_tags/_module_tag.html.erb @@ -0,0 +1,17 @@ +
  • +

    + +

    + <%= hidden_field_tag "ids[]", module_tag.id, class: "tag_id" %> + <% if module_tag.is_default %> + + <%= get_tagging_count(module_tag) %> + <%= show_names_slash(module_tag) %> + + <% else %> + <%= link_to edit_admin_module_tag_path(module_tag, module_app_id: @module_app_id), class: "open-slide" do %> + <%= get_tagging_count(module_tag) %> + <%= show_names_slash(module_tag) %> + <% end %> + <% end %> +
  • \ No newline at end of file diff --git a/app/views/admin/module_tags/_tag.html.erb b/app/views/admin/module_tags/_tag.html.erb deleted file mode 100644 index 9105c232e..000000000 --- a/app/views/admin/module_tags/_tag.html.erb +++ /dev/null @@ -1,17 +0,0 @@ -
  • -

    - -

    - <%= hidden_field_tag "ids[]", tag.id, class: "tag_id" %> - <% if tag.is_default %> - - <%= tag.taggings.count %> - <%= show_names_slash(tag) %> - - <% else %> - <%= link_to edit_admin_module_tag_path(tag, module_app_id: @module_app_id), class: "open-slide" do %> - <%= tag.taggings.count %> - <%= show_names_slash(tag) %> - <% end %> - <% end %> -
  • \ No newline at end of file diff --git a/app/views/admin/module_tags/index.js.erb b/app/views/admin/module_tags/index.js.erb index a1c2e5e7f..d10a3ef34 100644 --- a/app/views/admin/module_tags/index.js.erb +++ b/app/views/admin/module_tags/index.js.erb @@ -1,5 +1,6 @@ $("#delete_tags").modal('hide'); $("#tags-merger").modal('hide'); +$("#remove_defaults").modal('hide'); $("#tags_index").html("<%= j render 'index' %>") $.pageslide.close(); openSlide(); diff --git a/app/views/admin/tags/_delete_modal.html.erb b/app/views/admin/tags/_delete_modal.html.erb index 4dc173817..6f1ac399b 100644 --- a/app/views/admin/tags/_delete_modal.html.erb +++ b/app/views/admin/tags/_delete_modal.html.erb @@ -4,7 +4,7 @@

    <%= t('tag.delete') %>