Add tagged_ids to taggable
This commit is contained in:
parent
167a71272a
commit
aefd62da2a
|
@ -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
|
||||
|
||||
|
|
|
@ -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
|
|
@ -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
|
||||
|
|
Reference in New Issue