From c60306eee54b07985d010cb0ef28b4e0d1eb7680 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Wed, 25 Jul 2012 22:46:20 +0200 Subject: [PATCH] Add documentation for RablRails#render --- lib/rabl-rails/renderer.rb | 22 +++++++++++++++++++++- 1 file changed, 21 insertions(+), 1 deletion(-) diff --git a/lib/rabl-rails/renderer.rb b/lib/rabl-rails/renderer.rb index 7590150..5f2c081 100644 --- a/lib/rabl-rails/renderer.rb +++ b/lib/rabl-rails/renderer.rb @@ -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")