Module: Wallaby::ResourcesConcern

Extended by:
ActiveSupport::Concern
Includes:
Authorizable, Baseable, Configurable, Decoratable, Paginatable, Prefixable, Resourcable, Servicable
Included in:
ResourcesController
Defined in:
lib/concerns/wallaby/resources_concern.rb

Overview

Resources concern defining the controller behaviors for ResourcesController and other controllers that include itself

Constant Summary

Constants included from Prefixable

Prefixable::MAPPING_ACTIONS

Instance Method Summary collapse

Methods included from Servicable

#current_servicer

Methods included from Resourcable

#collection, #create_params, #current_model_class, #current_resources_name, #new_resource, #resource, #resource_id, #resource_params, #update_params

Methods included from Paginatable

#current_paginator, #paginate, #pagination_params_for

Methods included from Prefixable

#wallaby_prefixes

Methods included from Decoratable

#current_decorator, #current_fields, #current_model_decorator, #decorate, #decorator_of, #extract

Methods included from Configurable

#wallaby_controller

Methods included from Authorizable

#authorized?, #current_authorizer, #unauthorized?

Instance Method Details

#create(location: -> { { action: :show, id: new_resource.id } }, **responder_options) {|format| ... } ⇒ Object Also known as: create!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to create a record that user is allowed to.

If record is created successfully, user will be navigated to the record show page. Otherwise, the form will be shown again with error messages.

It can be customized as below in subclasses:

def create
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  create!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

def create
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource = Product.new resource_params.merge(new_arrival: true)
  if @resource.save
    redirect_to helper.index_path(current_model_class)
  else
    render :new
  end
end

Parameters:

  • location (Proc, String) (defaults to: -> { { action: :show, id: new_resource.id } })

    (since 0.3.0) location for responder_options

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



172
173
174
175
176
# File 'lib/concerns/wallaby/resources_concern.rb', line 172

def create(location: -> { { action: :show, id: new_resource.id } }, **responder_options, &block)
  current_authorizer.authorize :create, new_resource
  current_servicer.create new_resource, create_params
  respond_with new_resource, responder_options.merge(location: location), &block
end

#destroy(location: -> { { action: resource_id ? :index : :show } }, **responder_options) {|format| ... } ⇒ Object Also known as: destroy!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to delete the record that user is allowed to.

It can be customized as below in subclasses:

def destroy
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  destroy!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

def destroy
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource = Product.find_by_slug params[:id]
  @resource.destroy
  redirect_to helper.index_path(current_model_class)
end

Parameters:

  • location (Proc, String) (defaults to: -> { { action: resource_id ? :index : :show } })

    (since 0.3.0) location for responder_options

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



327
328
329
330
331
# File 'lib/concerns/wallaby/resources_concern.rb', line 327

def destroy(location: -> { { action: resource_id ? :index : :show } }, **responder_options, &block)
  current_authorizer.authorize :destroy, resource
  current_servicer.destroy resource, {}
  respond_with resource, responder_options.merge(location: location), &block
end

#edit(**responder_options) {|format| ... } ⇒ Object Also known as: edit!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to show the form to edit record that user is allowed to.

It can be customized as below in subclasses:

def edit
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  edit!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

def edit
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource = Product.find_by_slug params[:id]
end

Parameters:

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



243
244
245
246
# File 'lib/concerns/wallaby/resources_concern.rb', line 243

def edit(**responder_options, &block)
  current_authorizer.authorize :edit, resource
  respond_with resource, responder_options, &block
end

#homeObject

Note:

This is a template method that can be overridden by subclasses.

This is an action for landing page display. It does nothing more than rendering ‘home` template.

It can be replaced completely in subclasses as below:

def home
  generate_dashboard_report
end


53
54
55
# File 'lib/concerns/wallaby/resources_concern.rb', line 53

def home
  # do nothing
end

#index(**responder_options) {|format| ... } ⇒ Object Also known as: index!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to list records that user can access.

It can be customized as below in subclasses:

‘WARN: Please keep in mind that Wallaby User Interface requires index action to respond to csv and json format as well.`

def index
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  index!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

‘WARN: Please keep in mind that Wallaby User Interface requires index action to respond to csv and json format as well.`

def index
  # NOTE: `@collection` will be used by the view, please ensure it is assigned, for example:
  @collection = Product.all
  respond_with @collection
end

Parameters:

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



93
94
95
96
# File 'lib/concerns/wallaby/resources_concern.rb', line 93

def index(**responder_options, &block)
  current_authorizer.authorize :index, current_model_class
  respond_with collection, responder_options, &block
end

#new(**responder_options) {|format| ... } ⇒ Object Also known as: new!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to show the form to create record that user is allowed to.

It can be customized as below in subclasses:

def new
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  new!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

def new
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource = Product.new new_arrival: true
end

Parameters:

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



128
129
130
131
# File 'lib/concerns/wallaby/resources_concern.rb', line 128

def new(**responder_options, &block)
  current_authorizer.authorize :new, new_resource
  respond_with new_resource, responder_options, &block
end

#show(**responder_options) {|format| ... } ⇒ Object Also known as: show!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to display the record details that user is allowed to.

It can be customized as below in subclasses:

def show
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  show!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

def show
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource = Product.find_by_slug params[:id]
end

Parameters:

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



208
209
210
211
# File 'lib/concerns/wallaby/resources_concern.rb', line 208

def show(**responder_options, &block)
  current_authorizer.authorize :show, resource
  respond_with resource, responder_options, &block
end

#update(location: -> { { action: :show } }, **responder_options) {|format| ... } ⇒ Object Also known as: update!

Note:

This is a template method that can be overridden by subclasses.

This is a resourceful action to update the record that user is allowed to.

If record is updated successfully, user will be navigated to the record show page. Otherwise, the form will be shown again with error messages.

It can be customized as below in subclasses:

def update
  # do something before the original action
  responder_options = {} # NOTE: see `responder_options` parameter for more details
  # NOTE: this is better than using `super` in many ways, but choose the one that better fits your scenario
  update!(responder_options) do |format|
    # NOTE: this block is for `respond_with` which works similar to `respond_to`
    # customize response behaviour, or do something before the request is rendered
  end
end

Otherwise, it can be replaced completely in subclasses:

def update
  # NOTE: `@resource` will be used by the view, please ensure it is assigned, for example:
  @resource = Product.find_by_slug params[:id]
  @resource.assign_attributes resource_params.merge(new_arrival: true)
  if @resource.save
    redirect_to helper.index_path(current_model_class)
  else
    render :new
  end
end

Parameters:

  • location (Proc, String) (defaults to: -> { { action: :show } })

    (since 0.3.0) location for responder_options

  • responder_options (Hash)

    (since wallaby-5.2.0) responder_options for respond_with

Yields:

  • (format)

    block for respond_with to customize response behaviour.

Raises:



288
289
290
291
292
# File 'lib/concerns/wallaby/resources_concern.rb', line 288

def update(location: -> { { action: :show } }, **responder_options, &block)
  current_authorizer.authorize :update, resource
  current_servicer.update resource, update_params
  respond_with resource, responder_options.merge(location: location), &block
end