Module: Hanami::Extensions::View::Context::ClassExtension::InstanceMethods
- Defined in:
- lib/hanami/extensions/view/context.rb
Overview
Instance Attribute Summary collapse
-
#inflector ⇒ Dry::Inflector
readonly
Returns the app’s inflector.
Instance Method Summary collapse
-
#assets ⇒ Hanami::Assets
Returns the app’s assets.
- #content_for(key, value = nil) ⇒ Object
-
#csrf_token ⇒ String
Returns the current request’s CSRF token.
-
#flash ⇒ Object
Returns the flash hash for the current request.
- #initialize(inflector: nil, routes: nil, assets: nil, request: nil, **args) ⇒ Object private
- #initialize_copy(source) ⇒ Object private
-
#request ⇒ Hanami::Action::Request
Returns the current request, if the view is rendered from within an action.
-
#request? ⇒ Boolean
Returns true if the view is rendered from within an action and a request is available.
-
#routes ⇒ Hanami::Slice::RoutesHelper
Returns the app’s routes helper.
-
#session ⇒ Rack::Session::Abstract::SessionHash
Returns the session for the current request.
Instance Attribute Details
#inflector ⇒ Dry::Inflector (readonly)
Returns the app’s inflector.
92 93 94 |
# File 'lib/hanami/extensions/view/context.rb', line 92 def inflector @inflector end |
Instance Method Details
#assets ⇒ Hanami::Assets
Returns the app’s assets.
135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/hanami/extensions/view/context.rb', line 135 def assets unless @assets msg = if Hanami.bundled?("hanami-assets") "Have you put files into your assets directory?" else "The hanami-assets gem is required to access assets." end raise Hanami::ComponentLoadError, "Assets not available. #{msg}" end @assets end |
#content_for(key, value = nil, &block) ⇒ String #content_for(key) ⇒ String?
219 220 221 222 223 224 225 226 227 228 229 |
# File 'lib/hanami/extensions/view/context.rb', line 219 def content_for(key, value = nil) if block_given? @content_for[key] = yield nil elsif value @content_for[key] = value nil else @content_for[key] end end |
#csrf_token ⇒ String
Returns the current request’s CSRF token.
240 241 242 |
# File 'lib/hanami/extensions/view/context.rb', line 240 def csrf_token request.session[Hanami::Action::CSRFProtection::CSRF_TOKEN] end |
#flash ⇒ Object
Returns the flash hash for the current request.
266 267 268 |
# File 'lib/hanami/extensions/view/context.rb', line 266 def flash request.flash end |
#initialize(inflector: nil, routes: nil, assets: nil, request: nil, **args) ⇒ 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.
98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 |
# File 'lib/hanami/extensions/view/context.rb', line 98 def initialize( # rubocop:disable Metrics/ParameterLists inflector: nil, routes: nil, assets: nil, request: nil, **args ) @inflector = inflector @routes = routes @assets = assets @request = request @content_for = {} super(**args) end |
#initialize_copy(source) ⇒ 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.
117 118 119 120 121 122 123 124 125 |
# File 'lib/hanami/extensions/view/context.rb', line 117 def initialize_copy(source) # The standard implementation of initialize_copy will make shallow copies of all # instance variables from the source. This is fine for most of our ivars. super # Dup any objects that will be mutated over a given rendering to ensure no leakage of # state across distinct view renderings. @content_for = source.instance_variable_get(:@content_for).dup end |
#request ⇒ Hanami::Action::Request
Returns the current request, if the view is rendered from within an action.
158 159 160 161 162 163 164 |
# File 'lib/hanami/extensions/view/context.rb', line 158 def request unless @request raise Hanami::ComponentLoadError, "Request not available. Only views rendered from Hanami::Action instances have a request." end @request end |
#request? ⇒ Boolean
Returns true if the view is rendered from within an action and a request is available.
172 173 174 |
# File 'lib/hanami/extensions/view/context.rb', line 172 def request? !!@request end |
#routes ⇒ Hanami::Slice::RoutesHelper
Returns the app’s routes helper.
185 186 187 188 189 190 191 |
# File 'lib/hanami/extensions/view/context.rb', line 185 def routes unless @routes raise Hanami::ComponentLoadError, "the hanami-router gem is required to access routes" end @routes end |
#session ⇒ Rack::Session::Abstract::SessionHash
Returns the session for the current request.
253 254 255 |
# File 'lib/hanami/extensions/view/context.rb', line 253 def session request.session end |