Sha256: 927a94e09e509cdae0ab93f0839df8bff6a0ecea3b865328337e9dab4528f82d

Contents?: true

Size: 1.93 KB

Versions: 4

Compression:

Stored size: 1.93 KB

Contents

require_dependency 'guts/application_controller'

module Guts
  # Options controller
  class OptionsController < ApplicationController
    include ControllerPermissionConcern

    before_action :set_option, only: [:show, :edit, :update, :destroy]
    before_action :set_per_page, only: [:index]
    load_and_authorize_resource
    
    # Display a list of options
    def index
      @options = Option.paginate(page: params[:page], per_page: @per_page)
    end

    # Show a single options
    def show
    end

    # Creation of a new option
    def new
      @option = Option.new
    end

    # Editing of an option
    def edit
    end

    # Creates an option through post
    # @note Redirects to #index if successfull or re-renders #new if not
    def create
      @option = Option.new option_params

      if @option.save
        flash[:notice] = 'Option was successfully created.'
        redirect_to edit_option_path(@option)
      else
        render :new
      end
    end

    # Updates an option through patch
    # @note Redirects to #index if successfull or re-renders #edit if not
    def update
      if @option.update option_params
        flash[:notice] = 'Option was successfully updated.'
        redirect_to edit_option_path(@option)
      else
        render :edit
      end
    end

    # Destroys an option
    def destroy
      @option.destroy

      flash[:notice] = 'Option was successfully destroyed.'
      redirect_to options_path
    end

    private

    # Sets a coption from the database using `id` param
    # @note This is a `before_action` callback
    # @private
    def set_option
      @option = Option.find params[:id]
    end

    # Permits option params from forms
    # @private
    def option_params
      params.require(:option).permit(:key, :value, :site_id)
    end

    # Gets the per-page value to use
    # @note Default is 30
    # @private
    def set_per_page
      @per_page = params[:per_page] || 30
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
guts-2.1.0 app/controllers/guts/options_controller.rb
guts-2.0.2 app/controllers/guts/options_controller.rb
guts-2.0.1 app/controllers/guts/options_controller.rb
guts-2.0.0 app/controllers/guts/options_controller.rb