class TagsController < ApplicationController
  before_action :set_releasable
  before_action :set_tag, only: [:new]
  before_action :authorize_tags, only: [:create, :destroy]

  def new
  end

  def create
    @releasable.tag_list.add(tag_params[:name])
    @releasable.save
  end

  def destroy
    @tag = ActsAsTaggableOn::Tag.find(params[:id])

    @releasable.tag_list.remove(@tag.name)
    @releasable.save
  end

  private

  def releasable_param
    @releasable_param ||= ReleasableParam.new(params.to_unsafe_h)
  end

  def set_releasable
    @releasable = policy_scope(releasable_param.type).find(releasable_param.id)
  end

  def set_tag
    @tag = authorize @releasable.tags.build
  end

  def tag_params
    params.require(:acts_as_taggable_on_tag).permit(:name)
  end
  
  def authorize_tags
    authorize @releasable.tags
  end
end
