Class: Hanami::Config::Actions

Inherits:
Object
  • Object
show all
Includes:
Dry::Configurable
Defined in:
lib/hanami/config/actions.rb,
lib/hanami/config/actions/cookies.rb,
lib/hanami/config/actions/sessions.rb,
lib/hanami/config/actions/content_security_policy.rb

Overview

Hanami actions config

This exposes all the settings from the standalone ‘Hanami::Action` class, pre-configured with sensible defaults for actions within a full Hanami app. It also provides additional settings for further integration of actions with other full stack app components.

Since:

  • 2.0.0

Defined Under Namespace

Classes: ContentSecurityPolicy, Cookies, Sessions

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(**options) ⇒ Actions

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of Actions.

Since:

  • 2.0.0



111
112
113
114
115
116
117
118
# File 'lib/hanami/config/actions.rb', line 111

def initialize(*, **options)
  super()

  @base_config = Hanami::Action.config.dup
  @content_security_policy = ContentSecurityPolicy.new

  configure_defaults
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name, *args, &block) ⇒ Object (private)

Since:

  • 2.0.0



152
153
154
155
156
157
158
159
160
# File 'lib/hanami/config/actions.rb', line 152

def method_missing(name, *args, &block)
  if config.respond_to?(name)
    config.public_send(name, *args, &block)
  elsif base_config.respond_to?(name)
    base_config.public_send(name, *args, &block)
  else
    super
  end
end

Instance Attribute Details

#content_security_policyHanami::Config::Actions::ContentSecurityPolicy

Returns the Content Security Policy config for actions.

The resulting policy is set as a default ‘“Content-Security-Policy”` response header.



75
76
77
# File 'lib/hanami/config/actions.rb', line 75

def content_security_policy
  @content_security_policy
end

#cookiesHanami::Config::Actions::Cookies

Sets or returns a hash of cookie options for actions.

The hash is wrapped by Cookies, which also provides an ‘enabled?` method, returning true in the case of any options provided.

Examples:

config.actions.cookies = {max_age: 300}

Returns:

Since:

  • 2.0.0



31
# File 'lib/hanami/config/actions.rb', line 31

setting :cookies, default: {}, constructor: -> options { Cookies.new(options) }

#csrf_protectionBoolean

Sets or returns whether CSRF protection should be enabled for action classes.

Defaults to true if #sessions is enabled. You can override this by explicitly setting a true or false value.

When true, this will include ‘Hanami::Action::CSRFProtection` in all action classes.

Returns:

  • (Boolean)

Since:

  • 2.0.0



65
# File 'lib/hanami/config/actions.rb', line 65

setting :csrf_protection

#method_overrideBoolean

Sets or returns whether HTTP method override should be enabled for action classes.

Defaults to true. You can override this by explicitly setting a true or false value.

When true, this will mount ‘Rack::MethodOverride` in the Rack middleware stack of the App.

Returns:

  • (Boolean)

Since:

  • 2.1.0



89
# File 'lib/hanami/config/actions.rb', line 89

setting :method_override, default: true

#name_inference_baseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.1.0



94
# File 'lib/hanami/config/actions.rb', line 94

setting :name_inference_base, default: "actions"

#sessionsConfig::Sessions

Sets or returns the session store (and its options) for actions.

The given values are taken as an argument list to be passed to Config::Sessions#initialize.

The configured session store is used when setting up the app or slice router.

Examples:

config.actions.sessions = :cookie, {secret: "xyz"}

Returns:

  • (Config::Sessions)

See Also:

Since:

  • 2.0.0



51
# File 'lib/hanami/config/actions.rb', line 51

setting :sessions, constructor: proc { |storage, *options| Sessions.new(storage, *options) }

#view_name_inference_baseObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.1.0



104
# File 'lib/hanami/config/actions.rb', line 104

setting :view_name_inference_base, default: "views"

#view_name_inferrerObject

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.1.0



99
# File 'lib/hanami/config/actions.rb', line 99

setting :view_name_inferrer, default: Slice::ViewNameInferrer

Instance Method Details

#finalize!(app_config) ⇒ Object

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Since:

  • 2.0.0



129
130
131
132
133
134
135
136
137
138
139
# File 'lib/hanami/config/actions.rb', line 129

def finalize!(app_config)
  @base_config.root_directory = app_config.root

  # A nil value for `csrf_protection` means it has not been explicitly configured
  # (neither true nor false), so we can default it to whether sessions are enabled
  self.csrf_protection = sessions.enabled? if csrf_protection.nil?

  if content_security_policy
    default_headers["Content-Security-Policy"] = content_security_policy.to_s
  end
end