Now default tags can be removed from module

This commit is contained in:
chris 2013-04-25 19:20:37 +08:00
parent 19405cf006
commit 3714403f85
17 changed files with 248 additions and 109 deletions

View File

@ -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');

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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|

View File

@ -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
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

View File

@ -6,7 +6,7 @@
<button id="deselect" class="btn btn-inverse toggable hide"><%= t(:deselect) %></button>
<%= 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" %>
</div>
</div>
@ -16,7 +16,7 @@
<div class="tags">
<div id="tags-list">
<ul class="tags-groups checkbox-card module-tags">
<%= render partial: "tag", collection: @tags %>
<%= render partial: "module_tag", collection: @tags %>
</ul>
</div>
</div>
@ -28,4 +28,23 @@
<!-- delete -->
<%= render 'admin/tags/delete_modal' %>
<!-- delete:end -->
<!-- delete:end -->
<!-- remove_default -->
<div id="remove_defaults" class="modal hide" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
<div class="modal-header">
<button type="button" class="close" data-dismiss="modal" aria-hidden="true">×</button>
<h3 id="myModalLabel"><%= t('tag.remove_default') %></h3>
</div>
<div class="modal-body tags">
<span class="text-warning text-center"><%= t('tag.warning.remove_default') %></span>
<hr>
<ul class="tags-groups checkbox-card">
</ul>
</div>
<div class="modal-footer">
<button class="btn" data-dismiss="modal" aria-hidden="true">Close</button>
<%= link_to t(:remove_default), nil, class: "remove-defaults btn btn-danger", method: :post, remote: true %>
</div>
</div>
<!-- remove_default:end -->

View File

@ -0,0 +1,17 @@
<li class="filter-item <%= module_tag.is_default ? 'default' : 'module' %>">
<p class="card pull-left">
<input type="checkbox">
</p>
<%= hidden_field_tag "ids[]", module_tag.id, class: "tag_id" %>
<% if module_tag.is_default %>
<a>
<span class="amount"><%= get_tagging_count(module_tag) %></span>
<%= show_names_slash(module_tag) %>
</a>
<% else %>
<%= link_to edit_admin_module_tag_path(module_tag, module_app_id: @module_app_id), class: "open-slide" do %>
<span class="amount"><%= get_tagging_count(module_tag) %></span>
<%= show_names_slash(module_tag) %>
<% end %>
<% end %>
</li>

View File

@ -1,17 +0,0 @@
<li class="filter-item <%= tag.is_default ? 'default' : 'module' %>">
<p class="card pull-left">
<input type="checkbox">
</p>
<%= hidden_field_tag "ids[]", tag.id, class: "tag_id" %>
<% if tag.is_default %>
<a>
<span class="amount"><%= tag.taggings.count %></span>
<%= show_names_slash(tag) %>
</a>
<% else %>
<%= link_to edit_admin_module_tag_path(tag, module_app_id: @module_app_id), class: "open-slide" do %>
<span class="amount"><%= tag.taggings.count %></span>
<%= show_names_slash(tag) %>
<% end %>
<% end %>
</li>

View File

@ -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();

View File

@ -4,7 +4,7 @@
<h3 id="myModalLabel"><%= t('tag.delete') %></h3>
</div>
<div class="modal-body tags">
<span class="text-warning text-center"><%= t('tag.warning') %></span>
<span class="text-warning text-center"><%= t('tag.warning.delete') %></span>
<hr>
<ul class="tags-groups checkbox-card">
</ul>

View File

@ -15,14 +15,14 @@
<div id="tags-list">
<p class="tag-lead lead muted"><i class="icons-tag"></i> <%= t(:default) %> <span class="badge pull-right"><%= @tags.count %></span></p>
<ul class="tags-groups checkbox-card default-tags">
<%= render partial: "tag", collection: @tags %>
<%= render partial: "module_tag", collection: @tags %>
</ul>
<% @module_apps.each do |module_app| %>
<% tags = module_app.tags %>
<% tags = module_app.module_tags %>
<% icon_name = get_module_app_icon(module_app) %>
<p class="tag-lead lead muted"><i class="<%= icon_name %>"></i> <%= module_app.title %> <span class="badge pull-right"><%= tags.count %></span></p>
<ul class="tags-groups checkbox-card module-tags">
<%= render partial: "tag", collection: tags, locals: {in_module: true} %>
<%= render partial: "module_tag", collection: tags, locals: {in_module: true} %>
</ul>
<% end %>
</div>

View File

@ -0,0 +1,19 @@
<li class="filter-item <%= module_tag.is_default ? 'default' : '' %>">
<% unless defined?(in_module) && module_tag.is_default %>
<p class="card pull-left">
<input type="checkbox">
</p>
<%= hidden_field_tag "ids[]", module_tag.id, class: "tag_id" %>
<% end %>
<% if defined?(in_module) && module_tag.is_default %>
<a>
<span class="amount"><%= get_tagging_count(module_tag) %></span>
<%= show_names_slash(module_tag) %>
</a>
<% else %>
<%= link_to edit_admin_tag_path(module_tag), class: "open-slide" do %>
<span class="amount"><%= get_tagging_count(module_tag) %></span>
<%= show_names_slash(module_tag) %>
<% end %>
<% end %>
</li>

View File

@ -1,12 +0,0 @@
<li class="filter-item <%= tag.is_default ? 'default' : '' %>">
<% unless defined?(in_module) && tag.is_default %>
<p class="card pull-left">
<input type="checkbox">
</p>
<%= hidden_field_tag "ids[]", tag.id, class: "tag_id" %>
<% end %>
<%= link_to edit_admin_tag_path(tag), class: "open-slide" do %>
<span class="amount"><%= tag.taggings.count %></span>
<%= show_names_slash(tag) %>
<% end %>
</li>

View File

@ -13,13 +13,17 @@ en:
new:
tag: New tag
no_app: No module
remove_default: Remove default
tag:
add: Add tag
delete: Delete tags
new_name: New tag name
merge_help: Select the name you want to use or own defined
merger: Tag Merger
warning: This action can not be restored, are you sure you want to delete these tags?
remove_default: Remove default tags
warning:
delete: This action can not be restored, are you sure you want to delete these tags?
remove_default: Are you sure you want to remove the default tags?
update:
error:
tag: Error when updating tag.

View File

@ -74,31 +74,86 @@ module OrbitTag
end
def get_default_tags
Tag.where(default: true)
Tag.where(is_default: true)
end
end
module Merging
protected
def create_default_tag(name, taggings, module_app_ids)
create_tag(@module_app, name, taggings, module_app_ids, true)
end
def create_tag(module_app, name, taggings, module_app_ids=[module_app.id], default=false)
new_tag = module_app.module_tags.create(name_translations: name, module_app_ids: module_app_ids - [module_app.id], is_default: default)
def create_tag(name, taggings, module_app, default=false)
new_tag = module_app.module_tags.create(name_translations: name, module_app_id: module_app.id, is_default: default)
taggings.each do |tagging|
tagging.taggable.taggings.create(tag_id: new_tag.tag.id) unless tagging.taggable.taggings.detect{|t| t.tag_id.eql?(new_tag.tag.id)}
end
new_tag
end
def merge_default_tags(name, taggings, module_app_ids)
create_default_tag(name, taggings, module_app_ids)
def merge_default_tags(name, tag_leases)
parent = @module_app.module_tags.create(name_translations: name, is_default: true)
lease_by_module_app_id = tag_leases.inject({}) do |sorted, parent|
sorted = parent.children.inject(sorted) do |children, lease|
module_app_id = lease.module_app_id.to_s
if children[module_app_id]
children[module_app_id] += [lease]
else
children[module_app_id] = [lease]
end
children
end
sorted
end
merge_duplicates_in_module(parent, lease_by_module_app_id)
tag_leases.each(&:destroy)
parent
end
def merge_tags(name, taggings, module_app_id)
def merge_duplicates_in_module(parent, lease_by_module_app_id)
lease_by_module_app_id.each do |module_app_id, leases|
if leases.count > 0
taggings = leases.map { |l| l.tag.taggings }.flatten
module_tag = merge_tags(parent.name_translations, taggings, module_app_id, true)
module_tag.update_attribute(:parent_id, parent.id)
leases.each(&:destroy)
else
leases[0].update_attributes(name_translations: parent.name_translations, is_default: true, parent_id: parent.id) unless leases[0].is_default
end
end
end
def merge_tags(name, taggings, module_app_id, default=nil)
module_app = ModuleApp.find(module_app_id)
create_tag(module_app, name, taggings)
new_tag = create_tag(name, taggings, module_app, default)
new_tag
end
def merge_with_default_tags(name, tag_leases)
lease_by_module_app_id = tag_leases.inject({}) do |sorted, lease|
module_app_id = lease.is_default ? 'default' : lease.module_app_id.to_s
if sorted[module_app_id]
sorted[module_app_id] += [lease]
else
sorted[module_app_id] = [lease]
end
sorted
end
if lease_by_module_app_id['default'].count > 1
parent = merge_default_tags(name, lease_by_module_app_id['default'])
else
parent = lease_by_module_app_id['default'][0]
parent.update_attributes(name_translations: name)
end
lease_by_module_app_id.delete('default')
lease_by_module_app_id = parent.children.inject(lease_by_module_app_id) do |sorted, lease|
module_app_id = lease.module_app_id.to_s
if sorted[module_app_id]
sorted[module_app_id] += [lease]
else
sorted[module_app_id] = [lease]
end
sorted
end
merge_duplicates_in_module(parent, lease_by_module_app_id)
end
end
end

View File

@ -5,5 +5,5 @@
// the compiled file.
//
//= require jquery-ui
//= require bootstrap
//= require basic/bootstrap
//= require calendarAPI