From aefd62da2a50359e78a54871837014f8901b60f9 Mon Sep 17 00:00:00 2001 From: chris Date: Fri, 3 May 2013 10:01:26 +0800 Subject: [PATCH] Add tagged_ids to taggable --- app/controllers/admin/assets_controller.rb | 10 +++--- app/models/tagging.rb | 8 +++++ lib/orbit_tag/taggable.rb | 36 ++++++++++++++-------- 3 files changed, 36 insertions(+), 18 deletions(-) diff --git a/app/controllers/admin/assets_controller.rb b/app/controllers/admin/assets_controller.rb index d458045b..91a1a124 100644 --- a/app/controllers/admin/assets_controller.rb +++ b/app/controllers/admin/assets_controller.rb @@ -11,7 +11,7 @@ class Admin::AssetsController < OrbitBackendController def new @asset = Asset.new @asset_categories = AssetCategory.all - @tags = AssetTag.all + @tags = get_tags respond_to do |format| format.html {} format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/new'} } @@ -21,7 +21,7 @@ class Admin::AssetsController < OrbitBackendController def edit @asset = Asset.find(params[:id]) @asset_categories = AssetCategory.all - @tags = AssetTag.all + @tags = get_tags respond_to do |format| format.html {} format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/edit'} } @@ -46,7 +46,7 @@ class Admin::AssetsController < OrbitBackendController else flash[:error] = t('create.fail') @asset_categories = AssetCategory.all - @tags = AssetTag.all + @tags = get_tags respond_to do |format| format.js { if params[:uploader] @@ -69,7 +69,7 @@ class Admin::AssetsController < OrbitBackendController else flash[:error] = t('update.fail') @asset_categories = AssetCategory.all - @tags = AssetTag.all + @tags = get_tags respond_to do |format| format.html { render :action => :edit } format.js { render 'js/reload_pop_up', :locals => {:value => @asset, :values => nil, :partial => 'admin/assets/edit', :locals => {:is_html => false}} } @@ -96,7 +96,7 @@ class Admin::AssetsController < OrbitBackendController def file_upload @asset = Asset.new @asset_categories = AssetCategory.all - @tags = AssetTag.all + @tags = get_tags render :layout => false end diff --git a/app/models/tagging.rb b/app/models/tagging.rb index b244ce8d..86925c03 100644 --- a/app/models/tagging.rb +++ b/app/models/tagging.rb @@ -6,4 +6,12 @@ class Tagging belongs_to :tag belongs_to :taggable, polymorphic: true + + before_destroy :update_taggable_tag_ids + + private + + def update_taggable_tag_ids + self.taggable.remove_id(self.tag.id) + end end \ No newline at end of file diff --git a/lib/orbit_tag/taggable.rb b/lib/orbit_tag/taggable.rb index 18fba1bd..fc2d79d9 100644 --- a/lib/orbit_tag/taggable.rb +++ b/lib/orbit_tag/taggable.rb @@ -13,17 +13,35 @@ module OrbitTag def init_tag class_eval do field :tags_to_destroy, type: Array, default: [] + field :tagged_ids, type: Array, default: [] has_many :taggings, as: :taggable, autosave: true, dependent: :destroy accepts_nested_attributes_for :taggings, allow_destroy: true after_save :remove_taggings, unless: Proc.new{self.tags_to_destroy.blank?} + def remove_id(id) + self.class.without_callback(:save, :after, :remove_taggings) do + self.update_attribute(:tagged_ids, self.tagged_ids - [id.to_s]) + end + end + + def sorted_tags + if tags.blank? + [] + else + tag_array = tags.inject([]){ |result, value| + result << [value.name, value] + } + tag_array.sort.map{|x| x[1] } + end + end + def tags - self.taggings.blank? ? [] : self.taggings.map{|t| t.tag} + self.taggings.blank? ? [] : self.taggings.map{|t| t.tag}.compact end def tags=(tag_ids) - tag_ids = [tag_ids].flatten + ids = [tag_ids].flatten tag_ids.delete('') ids = self.taggings.blank? ? [] : self.taggings.map{|t| t.tag.id} tag_ids.each do |tag_id| @@ -40,17 +58,9 @@ module OrbitTag def tag_ids=(ids) self.tags = ids - end - - def sorted_tags - if tags.blank? - [] - else - tag_array = tags.inject([]){ |result, value| - result << [value.name, value] - } - tag_array.sort.map{|x| x[1] } - end + ids = [ids].flatten + ids.delete('') + self.tagged_ids = ids end private