-
-
Notifications
You must be signed in to change notification settings - Fork 542
/
context.rb
275 lines (244 loc) · 8.6 KB
/
context.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
# frozen_string_literal: true
require_relative "../../errors"
module Hanami
module Extensions
module View
# View context for views in Hanami apps.
#
# @api public
# @since 2.1.0
module Context
class << self
# Returns a context class for the given slice. If a context class is not defined, defines
# a class named `Views::Context` within the slice's namespace.
#
# @api private
# @since 2.1.0
def context_class(slice)
views_namespace = views_namespace(slice)
if views_namespace.const_defined?(:Context)
return views_namespace.const_get(:Context)
end
views_namespace.const_set(:Context, Class.new(context_superclass(slice)).tap { |klass|
klass.configure_for_slice(slice)
})
end
private
# @api private
# @since 2.1.0
def context_superclass(slice)
return Hanami::View::Context if Hanami.app.equal?(slice)
begin
slice.inflector.constantize(
slice.inflector.camelize("#{slice.app.slice_name.name}/views/context")
)
rescue NameError => e
raise e unless %i[Views Context].include?(e.name)
Hanami::View::Context
end
end
# @api private
# @since 2.1.0
def views_namespace(slice)
# TODO: this could be moved into the top-level Extensions::View
if slice.namespace.const_defined?(:Views)
slice.namespace.const_get(:Views)
else
slice.namespace.const_set(:Views, Module.new)
end
end
end
# @api private
# @since 2.1.0
module ClassExtension
def self.included(context_class)
super
context_class.extend(Hanami::SliceConfigurable)
context_class.extend(ClassMethods)
context_class.prepend(InstanceMethods)
end
# @api private
# @since 2.1.0
module ClassMethods
# @api private
# @since 2.1.0
def configure_for_slice(slice)
extend SliceConfiguredContext.new(slice)
end
end
# @api public
# @since 2.1.0
module InstanceMethods
# Returns the app's inflector.
#
# @return [Dry::Inflector] the inflector
#
# @api public
# @since 2.1.0
attr_reader :inflector
# Returns the app's settings.
#
# @return [Hanami::Settings] the settings
#
# @api public
# @since 2.1.0
attr_reader :settings
# @see SliceConfiguredContext#define_new
#
# @api private
# @since 2.1.0
def initialize( # rubocop:disable Metrics/ParameterLists
inflector: nil,
settings: nil,
routes: nil,
assets: nil,
request: nil,
**args
)
@inflector = inflector
@settings = settings
@routes = routes
@assets = assets
@request = request
@content_for = {}
super(**args)
end
# @api private
# @since 2.1.0
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
# Returns the app's assets.
#
# @return [Hanami::Assets] the assets
#
# @raise [Hanami::ComponentLoadError] if the hanami-assets gem is not bundled
#
# @api public
# @since 2.1.0
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
# Returns the current request, if the view is rendered from within an action.
#
# @return [Hanami::Action::Request] the request
#
# @raise [Hanami::ComponentLoadError] if the view is not rendered from within a request
#
# @api public
# @since 2.1.0
def request
unless @request
raise Hanami::ComponentLoadError, "Request not available. Only views rendered from Hanami::Action instances have a request."
end
@request
end
# Returns the app's routes helper.
#
# @return [Hanami::Slice::RoutesHelper] the routes helper
#
# @raise [Hanami::ComponentLoadError] if the hanami-router gem is not bundled or routes
# are not defined
#
# @api public
# @since 2.1.0
def routes
unless @routes
raise Hanami::ComponentLoadError, "the hanami-router gem is required to access routes"
end
@routes
end
# @overload content_for(key, value = nil, &block)
# Stores a string or block of template markup for later use.
#
# @param key [Symbol] the content key, for later retrieval
# @param value [String, nil] the content, if no block is given
#
# @return [String] the content
#
# @example
# content_for(:page_title, "Hello world")
#
# @example In a template
# <%= content_for :page_title do %>
# <h1>Hello world</h1>
# <% end %>
#
# @overload content_for(key)
# Returns the previously stored content for the given key.
#
# @param key [Symbol] the content key
#
# @return [String, nil] the content, or nil if no content previously stored with the
# key
#
# @api public
# @since 2.1.0
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
# Returns the current request's CSRF token.
#
# @return [String] the token
#
# @raise [Hanami::ComponentLoadError] if the view is not rendered from within a request
# @raise [Hanami::Action::MissingSessionError] if sessions are not enabled
#
# @api public
# @since 2.1.0
def csrf_token
request.session[Hanami::Action::CSRFProtection::CSRF_TOKEN]
end
# Returns the session for the current request.
#
# @return [Rack::Session::Abstract::SessionHash] the session hash
#
# @raise [Hanami::ComponentLoadError] if the view is not rendered from within a request
# @raise [Hanami::Action::MissingSessionError] if sessions are not enabled
#
# @api public
# @since 2.1.0
def session
request.session
end
# Returns the flash hash for the current request.
#
# @return []
#
# @raise [Hanami::ComponentLoadError] if the view is not rendered from within a request
# @raise [Hanami::Action::MissingSessionError] if sessions are not enabled
#
# @api public
# @since 2.1.0
def flash
request.flash
end
end
end
end
end
end
end
Hanami::View::Context.include(Hanami::Extensions::View::Context::ClassExtension)