section of the acts-as-taggable-on README 소유권 전용이며 모델의 세부 사항을 다루는 데 유용합니다.
그러나 제공된 메소드가 정확하다고는 생각하지 않습니다. 모든 소유 태그 (모든 사람이 소유 한 태그)를 각 소유자 <> 항목 관계에 적용합니다. 여기에 내가 그것을 할 거라고 방법은 다음과 같습니다
DEFAULT_ACTSASTAGGABLEON_TYPE = :tag
module TagToOwner
extend ActiveSupport::Concern
private
def add_owned_tag(item, owner, tags_to_add, options = {})
own_tag(item, owner, arrayify(tags_to_add), "add", options)
end
def remove_owned_tag(item, owner, tags_to_add, options = {})
own_tag(item, owner, arrayify(tags_to_add), "subtract", options)
end
def own_tag(item, owner, tags_to_add, direction = "add", opts)
tag_type = (options[:tag_type] || DEFAULT_ACTSASTAGGABLEON_TYPE)
owned_tag_list = item.owner_tags_on(owner, tag_type).map(&:name)
if direction == "subtract"
owned_tag_list = owned_tag_list.reject{|n| n.in?(tags_to_add)}
else
owned_tag_list.push(*tags_to_add)
end
owner.tag(item, with: stringify(owned_tag_list), on: tag_type, skip_save: (options[:skip_save] || true))
end
def arrayify(tags_to_add)
return tags_to_add if tags_to_add.is_a?(Array)
tags_to_add.split(",")
end
def stringify(tag_list)
tag_list.inject('') { |memo, tag| memo += (tag + ',') }.chomp(",")
end
end
그리고 :
class MyModelController < ApplicationController
include TagToOwner
# ...
def create
@my_model = MyModel.new(my_model_params)
add_tags
# ...
end
# ...
def update
add_tags
# ...
end
private
def add_tags
return unless params[:tag_list] && "#{params[:tag_list]}".split(",").any?
return unless validate_ownership_logiC# <- e.g. `current_user`
add_owned_tag(@my_model, current_user, params[:tag_list])
end
end
주 나는 그들의 README를 해결하는 행위로서의 taggable-에 대한 an issue 및 a corresponding pull request을 제기했다.
출처
2018-02-20 14:17:15
Sam