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 mattr_reader :view_path
@@view_path = 'app/views' @@view_path = 'app/views'
#
# Context class to emulate normal rendering view
# context
#
class Context class Context
def initialize def initialize
@_assigns = {} @_assigns = {}
@ -16,8 +20,19 @@ module RablRails
end end
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 = {}) def render(object, template, options = {})
format = options.delete(:format) || 'json' format = options.delete(:format) || 'json'
object = options[:locals].delete(:object) if !object && options[:locals]
source = find_template(template, format, options.delete(:view_path)) source = find_template(template, format, options.delete(:view_path))
compiled_template = Compiler.new.compile_source(source) compiled_template = Compiler.new.compile_source(source)
@ -29,6 +44,11 @@ module RablRails
private 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) def find_template(name, format, view_path = nil)
view_path ||= self.view_path view_path ||= self.view_path
path = File.join(view_path, "#{name}.#{format}.rabl") path = File.join(view_path, "#{name}.#{format}.rabl")