module Hanami::Action::Session
Session
API
This module isn't included by default.
@since 0.1.0
Constants
- ERRORS_KEY
The key that is used by flash to transport errors
@since 0.3.0 @api private
- SESSION_KEY
The key that returns raw session from the
Rack
env@since 0.1.0 @api private
Public Class Methods
Add session to default exposures
@since 0.4.4 @api private
# File lib/hanami/action/session.rb, line 27 def self.included(action) action.class_eval do _expose :session, :flash end end
Public Instance Methods
Read errors from flash or delegate to the superclass
@return [Hanami::Validations::Errors] A collection of validation errors
@since 0.3.0
@see Hanami::Action::Validatable
@see Hanami::Action::Session#flash
# File lib/hanami/action/session.rb, line 72 def errors flash[ERRORS_KEY] || params.respond_to?(:errors) && params.errors end
Gets the session from the request and expose it as an Hash.
@return [Hash] the HTTP session from the request
@since 0.1.0
@example
require 'hanami/controller' require 'hanami/action/session' class Show include Hanami::Action include Hanami::Action::Session def call(params) # ... # get a value session[:user_id] # => '23' # set a value session[:foo] = 'bar' # remove a value session[:bax] = nil end end
# File lib/hanami/action/session.rb, line 60 def session @_env[SESSION_KEY] ||= {} end
Private Instance Methods
Finalize the response
@return [void]
@since 0.3.0 @api private
# File lib/hanami/action/session.rb, line 149 def finish super flash.clear end
Container useful to transport data with the HTTP session
@return [Hanami::Action::Flash] a Flash
instance
@since 0.3.0
# File lib/hanami/action/session.rb, line 85 def flash @flash ||= Flash.new(session) end
In case of validations errors, preserve those informations after a redirect.
@return [void]
@since 0.3.0 @api private
@see Hanami::Action::Redirect#redirect_to
@example
require 'hanami/controller' module Comments class Index include Hanami::Action include Hanami::Action::Session expose :comments def call(params) @comments = CommentRepository.all end end class Create include Hanami::Action include Hanami::Action::Session params do param :text, type: String, presence: true end def call(params) comment = Comment.new(params) CommentRepository.create(comment) if params.valid? redirect_to '/comments' end end end # The validation errors caused by Comments::Create are available # **after the redirect** in the context of Comments::Index.
# File lib/hanami/action/session.rb, line 133 def redirect_to(*args) if params.respond_to?(:valid?) flash[ERRORS_KEY] = errors.to_a unless params.valid? end flash.keep! super end