Class: Cuprum::Rails::Responders::HtmlResponder

Inherits:
BaseResponder show all
Includes:
Actions, Cuprum::Rails::Responders::Html::Rendering, Matching
Defined in:
lib/cuprum/rails/responders/html_responder.rb

Overview

Provides a DSL for defining responses to HTML requests.

By default, responds to any successful result by rendering the template for the action name and passing the result value as assigned variables. For a failing result, redirects to either the show page or the index page for the resource, based on the resource’s defined #routes.

Examples:

Defining A Response

class CustomResponder < Cuprum::Rails::Responders::HtmlResponder
  match :failure, error: Spec::AuthorizationError do
    redirect_to('/login')
  end
end

Defining Responses For An Action

class ActionsResponder < Cuprum::Rails::Responders::HtmlResponder
  action :publish do
    match :failure do
      redirect_to(resource.routes.index_path)
    end

    match :success do
      redirect_to(resource.routes.show_path(@result.value))
    end
  end
end

See Also:

Instance Attribute Summary

Attributes included from Matching

#matcher

Attributes inherited from BaseResponder

#action_name, #controller, #controller_name, #request, #resource, #result

Instance Method Summary collapse

Methods included from Actions

included

Methods included from Matching

#initialize, #member_action?

Methods included from Cuprum::Rails::Responders::Html::Rendering

#head, #redirect_back, #redirect_to, #render

Methods inherited from BaseResponder

#initialize, #member_action?, #routes

Instance Method Details

#call(result) ⇒ #call

Finds and calls the response clause that matches the given result.

  1. Checks for an exact match (the result status, value, and error all match) in the given matcher (if any), then the responder class, then each ancestor of the responder class in ascending order.

  2. If a match is not found, checks for a partial match (the result status, and either the value or the error match) in the same order.

  3. If there is still no match found, checks for a generic match (the result status matches, and the match clause does not specify either an error or a value.

  4. If there is no matching response clause, raises an exception.

If the responder defines an action matcher that matches the given action name, that matcher is matched against the result before any match clauses defined directly on the responder.

Parameters:

  • result (Cuprum::Result)

    the result of the action call.

Returns:

  • (#call)

    the response object from the matching response clause.

Raises:

  • (Cuprum::Matching::NoMatchError)

    if none of the response clauses match the result.



# File 'lib/cuprum/rails/responders/html_responder.rb', line 57

#formatSymbol

Returns the format of the responder.

Returns:

  • (Symbol)

    the format of the responder.



61
62
63
# File 'lib/cuprum/rails/responders/html_responder.rb', line 61

def format
  :html
end