Add documentation for RablRails#render

This commit is contained in:
ccocchi 2012-07-25 22:46:20 +02:00
parent 9e6b4db8eb
commit c60306eee5
1 changed files with 21 additions and 1 deletions

View File

@ -6,6 +6,10 @@ module RablRails
mattr_reader :view_path
@@view_path = 'app/views'
#
# Context class to emulate normal rendering view
# context
#
class Context
def initialize
@_assigns = {}
@ -16,11 +20,22 @@ module RablRails
end
end
#
# Renders object with the given rabl template.
#
# Object can also be passed as an option :
# { locals: { object: obj_to_render } }
#
# Default render format is JSON, but can be changed via
# an option: { format: 'xml' }
#
def render(object, template, options = {})
format = options.delete(:format) || 'json'
object = options[:locals].delete(:object) if !object && options[:locals]
source = find_template(template, format, options.delete(:view_path))
compiled_template = Compiler.new.compile_source(source)
c = Context.new
c.assigns[compiled_template.data.to_s[1..-1]] = object if compiled_template.data
@ -29,6 +44,11 @@ module RablRails
private
#
# Manually find given rabl template file with given format.
# View path can be set via options, otherwise default Rails
# path is used
#
def find_template(name, format, view_path = nil)
view_path ||= self.view_path
path = File.join(view_path, "#{name}.#{format}.rabl")