class ApprovalsController < ApplicationController
  before_action :set_releasable
  before_action :set_project

  layout "project"

  def create
    @releasable.approve_by(current_user)

    if @releasable.save
      redirect_to [@project, "#{@releasable_param.name.pluralize}"], notice: t('.release_approved', release_type: @releasable.model_name.human)
    end
  end

  private

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

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

  def set_project
    @project = @releasable.project
  end
end
