class MiscReleasesController < ApplicationController
  include ProjectContext, MiscReleaseContext

  before_action :set_project, only: [:index]
  before_action :set_misc_release, only: [:destroy]

  include ProjectLayout

  def index
    @misc_releases = filtered_misc_releases.order_by_recent.paginate(page: params[:page])
  end

  def destroy
    @project = @misc_release.project

    if @misc_release.destroy
      redirect_to [@project, :misc_releases], alert: t(".alert")
    end
  end

  private

  def misc_releases
    if @project
      policy_scope(@project.misc_releases)
    else
      policy_scope(MiscRelease)
    end
  end

  def filtered_misc_releases
    results = misc_releases

    if params[:query].present?
      results = results.search(params[:query])
    end

    results
  end
end
