Module: N::Render

Included in:
Context, Controller
Defined in:
lib/nitro/render.rb

Overview

The rendering mixin.

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#contextObject Also known as: ctx

The context.



178
179
180
# File 'lib/nitro/render.rb', line 178

def context
  @context
end

#outObject

The outbut buffer. The output of a script/action is accumulated in this buffer.



174
175
176
# File 'lib/nitro/render.rb', line 174

def out
  @out
end

#rendering_errorsObject

An array holding the rendering errors for this request.



189
190
191
# File 'lib/nitro/render.rb', line 189

def rendering_errors
  @rendering_errors
end

#requestObject

Alias for context.



184
185
186
# File 'lib/nitro/render.rb', line 184

def request
  @request
end

Instance Method Details

#initialize(context, base) ⇒ Object

Initialize the render.

context

A parent render/controller acts as the context.



196
197
198
199
200
# File 'lib/nitro/render.rb', line 196

def initialize(context, base)
	@request = @context = context
	@out = context.out
	@base = base
end

#log_error(str) ⇒ Object

Log a rendering error.



254
255
256
257
258
259
260
261
262
# File 'lib/nitro/render.rb', line 254

def log_error(str)
	@rendering_errors ||= []
	@rendering_errors << str

	# gmosx: Hmm perhaps this should not be logged 
	# to avoid DOS attacks.

	Logger.error str
end

#redirect(url, status = 303) ⇒ Object

Send a redirect response.

Raises:



238
239
240
241
242
243
244
# File 'lib/nitro/render.rb', line 238

def redirect(url, status = 303)
	@context.status = status
	@context.out = "<html><a href=\"#{url.to_s}\">#{url.to_s}</a>.</html>\n"
	@context.response_headers['location'] = url.to_s

	raise RenderExit
end

#redirect_referer(postfix = nil, status = 303) ⇒ Object

Redirect to the referer of this method.



248
249
250
# File 'lib/nitro/render.rb', line 248

def redirect_referer(postfix = nil, status = 303)
	redirect("#{@context.referer}#{postfix}", status)
end

#render(path) ⇒ Object

Renders the action denoted by path. The path is resolved by the dispatcher to get the correct controller.



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
# File 'lib/nitro/render.rb', line 206

def render(path)
	Logger.debug "Rendering '#{path}'." if $DBG
	
	klass, action, base, ctype = @context.dispatcher.dispatch(path, @context)

	@context.content_type = ctype

	raise 'No controller for action' unless klass
	
	if self.class == klass 
		self.send(action)
	else
		klass.new(self, base).send(action)
	end

rescue RenderExit => e

	# Just stop rendering.
	# For example called by redirects.
	
rescue Exception, StandardError => e
	log_error "Error while handling '#{path}'."
	log_error pp_exception(e)
	
	# More fault tolerant, only flags the erroneous box with 
	# error not the full page.
	
	@out << '(error)'
end

#sessionObject

Convenience method to lookup the session.



266
267
268
# File 'lib/nitro/render.rb', line 266

def session
	@context.session
end