Sha256: 89186146a051b881aba1b099eacd597f23682c4a8592d76575367eb2f66c2ff3

Contents?: true

Size: 1.78 KB

Versions: 4

Compression:

Stored size: 1.78 KB

Contents

require_dependency "guts/application_controller"

module Guts
  # Options controller
  class OptionsController < ApplicationController
    before_action :set_option, only: [:show, :edit, :update, :destroy]
    before_action :set_per_page, only: [:index]

    # 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
        redirect_to options_path, notice: "Option was successfully created."
      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
        redirect_to options_path, notice: "Option was successfully updated."
      else
        render :edit
      end
    end

    # Destroys an option
    def destroy
      @option.destroy
      redirect_to options_path, notice: "Option was successfully destroyed."
    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)
    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-1.0.8 app/controllers/guts/options_controller.rb
guts-1.0.7 app/controllers/guts/options_controller.rb
guts-1.0.5 app/controllers/guts/options_controller.rb
guts-1.0.3 app/controllers/guts/options_controller.rb