Module: ControllerResources::Extension

Extended by:
ActiveSupport::Concern
Defined in:
lib/controller_resources/extension.rb

Overview

The DSL mixin for ActionController that allows you to quickly define both singular and collection model resources that can be operated on within the controller. Attempts to DRY up most of the boilerplate code at the top of each controller used to set up its state.

Examples:


class PostsController < ApplicationController
  resource :post do
    permit :title, :body, :author_id
  end

  def index
    respond_with posts
  end

  def show
    respond_with post
  end

  def create
    post.save
    respond_with post
  end

Author:

  • Tom Scott

Instance Method Summary collapse

Instance Method Details

#collectionObject

Reader method for the defined collection resource.

has been defined.

Returns:

  • (Object)

    the collection of models or nil if no resource



119
120
121
122
# File 'lib/controller_resources/extension.rb', line 119

def collection
  return unless resource?
  public_send collection_name
end

#edit_paramsActionController::Parameters

White-listed parameters for mass assignment as defined by StrongParameters. Throws a ControllerResources::NotDefinedError if no resource block has been defined on this controller.

been defined on this controller.

Returns:

  • (ActionController::Parameters)

    the StrongParameters hash.

Raises:



131
132
133
134
135
# File 'lib/controller_resources/extension.rb', line 131

def edit_params
  fail NotDefinedError unless resource?
  return params.require(model_name).permit! unless params_to_permit.present?
  params.require(model_name).permit(*params_to_permit)
end

#modelObject

Reader method for the defined singular resource.

defined.

Returns:

  • (Object)

    the model object or nil if no resource has been



110
111
112
113
# File 'lib/controller_resources/extension.rb', line 110

def model
  return unless resource?
  public_send model_name
end