Add tagged_ids to taggable
This commit is contained in:
parent
167a71272a
commit
aefd62da2a
|
@ -11,7 +11,7 @@ class Admin::AssetsController < OrbitBackendController
|
||||||
def new
|
def new
|
||||||
@asset = Asset.new
|
@asset = Asset.new
|
||||||
@asset_categories = AssetCategory.all
|
@asset_categories = AssetCategory.all
|
||||||
@tags = AssetTag.all
|
@tags = get_tags
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html {}
|
format.html {}
|
||||||
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/new'} }
|
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/new'} }
|
||||||
|
@ -21,7 +21,7 @@ class Admin::AssetsController < OrbitBackendController
|
||||||
def edit
|
def edit
|
||||||
@asset = Asset.find(params[:id])
|
@asset = Asset.find(params[:id])
|
||||||
@asset_categories = AssetCategory.all
|
@asset_categories = AssetCategory.all
|
||||||
@tags = AssetTag.all
|
@tags = get_tags
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html {}
|
format.html {}
|
||||||
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/edit'} }
|
format.js { render 'js/show_pop_up', :locals => {:partial => 'admin/assets/edit'} }
|
||||||
|
@ -46,7 +46,7 @@ class Admin::AssetsController < OrbitBackendController
|
||||||
else
|
else
|
||||||
flash[:error] = t('create.fail')
|
flash[:error] = t('create.fail')
|
||||||
@asset_categories = AssetCategory.all
|
@asset_categories = AssetCategory.all
|
||||||
@tags = AssetTag.all
|
@tags = get_tags
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.js {
|
format.js {
|
||||||
if params[:uploader]
|
if params[:uploader]
|
||||||
|
@ -69,7 +69,7 @@ class Admin::AssetsController < OrbitBackendController
|
||||||
else
|
else
|
||||||
flash[:error] = t('update.fail')
|
flash[:error] = t('update.fail')
|
||||||
@asset_categories = AssetCategory.all
|
@asset_categories = AssetCategory.all
|
||||||
@tags = AssetTag.all
|
@tags = get_tags
|
||||||
respond_to do |format|
|
respond_to do |format|
|
||||||
format.html { render :action => :edit }
|
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}} }
|
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
|
def file_upload
|
||||||
@asset = Asset.new
|
@asset = Asset.new
|
||||||
@asset_categories = AssetCategory.all
|
@asset_categories = AssetCategory.all
|
||||||
@tags = AssetTag.all
|
@tags = get_tags
|
||||||
render :layout => false
|
render :layout => false
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -6,4 +6,12 @@ class Tagging
|
||||||
|
|
||||||
belongs_to :tag
|
belongs_to :tag
|
||||||
belongs_to :taggable, polymorphic: true
|
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
|
end
|
|
@ -13,17 +13,35 @@ module OrbitTag
|
||||||
def init_tag
|
def init_tag
|
||||||
class_eval do
|
class_eval do
|
||||||
field :tags_to_destroy, type: Array, default: []
|
field :tags_to_destroy, type: Array, default: []
|
||||||
|
field :tagged_ids, type: Array, default: []
|
||||||
|
|
||||||
has_many :taggings, as: :taggable, autosave: true, dependent: :destroy
|
has_many :taggings, as: :taggable, autosave: true, dependent: :destroy
|
||||||
accepts_nested_attributes_for :taggings, allow_destroy: true
|
accepts_nested_attributes_for :taggings, allow_destroy: true
|
||||||
after_save :remove_taggings, unless: Proc.new{self.tags_to_destroy.blank?}
|
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
|
def tags
|
||||||
self.taggings.blank? ? [] : self.taggings.map{|t| t.tag}
|
self.taggings.blank? ? [] : self.taggings.map{|t| t.tag}.compact
|
||||||
end
|
end
|
||||||
|
|
||||||
def tags=(tag_ids)
|
def tags=(tag_ids)
|
||||||
tag_ids = [tag_ids].flatten
|
ids = [tag_ids].flatten
|
||||||
tag_ids.delete('')
|
tag_ids.delete('')
|
||||||
ids = self.taggings.blank? ? [] : self.taggings.map{|t| t.tag.id}
|
ids = self.taggings.blank? ? [] : self.taggings.map{|t| t.tag.id}
|
||||||
tag_ids.each do |tag_id|
|
tag_ids.each do |tag_id|
|
||||||
|
@ -40,17 +58,9 @@ module OrbitTag
|
||||||
|
|
||||||
def tag_ids=(ids)
|
def tag_ids=(ids)
|
||||||
self.tags = ids
|
self.tags = ids
|
||||||
end
|
ids = [ids].flatten
|
||||||
|
ids.delete('')
|
||||||
def sorted_tags
|
self.tagged_ids = ids
|
||||||
if tags.blank?
|
|
||||||
[]
|
|
||||||
else
|
|
||||||
tag_array = tags.inject([]){ |result, value|
|
|
||||||
result << [value.name, value]
|
|
||||||
}
|
|
||||||
tag_array.sort.map{|x| x[1] }
|
|
||||||
end
|
|
||||||
end
|
end
|
||||||
|
|
||||||
private
|
private
|
||||||
|
|
Reference in New Issue