class BulkTaggingsController < ApplicationController
  before_action :set_project
  before_action :set_releasables
  before_action :authorize_bulk_taggings, only: [:new, :create]
  
  def new
    @releasable_ids = releasable_params[:ids]
  end

  def create
    @releasables.each do |releasable|
      releasable.tag_list.add(params[:name])
      releasable.save
    end

    all_releasables
  end

  private

  def releasable_params
    { type: params[:releasable_name].classify.constantize, ids: JSON.parse(params[:releasable_ids]) }
  end

  def set_releasables
    @releasables = releasable_params[:type].where(id: releasable_params[:ids])
  end

  def set_project
    @project = policy_scope(Project).find(params[:project_id])
  end
  
  def authorize_bulk_taggings
    authorize @releasables.first.tags
  end

  def all_releasables
    @all_releasables = policy_scope(releasable_params[:type].where(project: @project)).order_by_recent.paginate(page: params[:page])
  end
end
