# frozen_string_literal: true

module Admin
  class LinkBlocksController < BaseController
    before_action :set_link_block, only: [:show, :destroy]

    def index
      authorize :link_block, :index?
      @link_blocks = LinkBlock.page(params[:page])
    end

    def new
      authorize :link_block, :create?
      @link_block = LinkBlock.new
    end

    def create
      authorize :link_block, :create?

      @link_block = LinkBlock.new(resource_params)

      if @link_block.save
        log_action :create, @link_block
        redirect_to admin_link_blocks_path, notice: I18n.t('admin.link_blocks.created_msg')
      else
        render :new
      end
    end

    def destroy
      authorize @link_block, :destroy?
      @link_block.destroy!
      log_action :destroy, @link_block
      redirect_to admin_link_blocks_path, notice: I18n.t('admin.link_blocks.destroyed_msg')
    end

    private

    def set_link_block
      @link_block = LinkBlock.find(params[:id])
    end

    def resource_params
      params.require(:link_block).permit(:link)
    end
  end
end
