From 9e6b4db8ebd1654229560c38bff58c4473ea112d Mon Sep 17 00:00:00 2001 From: ccocchi Date: Wed, 25 Jul 2012 19:22:10 +0200 Subject: [PATCH] Make render work with same signature as standard RABL gem --- lib/rabl-rails.rb | 2 ++ lib/rabl-rails/renderer.rb | 44 +++++++++++++++++++++++++------------- 2 files changed, 31 insertions(+), 15 deletions(-) diff --git a/lib/rabl-rails.rb b/lib/rabl-rails.rb index 471fa00..5fbf960 100644 --- a/lib/rabl-rails.rb +++ b/lib/rabl-rails.rb @@ -16,6 +16,8 @@ require 'rabl-rails/railtie' require 'multi_json' module RablRails + extend Renderer + mattr_accessor :cache_templates @@cache_templates = true diff --git a/lib/rabl-rails/renderer.rb b/lib/rabl-rails/renderer.rb index f149b4f..7590150 100644 --- a/lib/rabl-rails/renderer.rb +++ b/lib/rabl-rails/renderer.rb @@ -1,24 +1,38 @@ require 'rabl-rails/renderers/base' require 'rabl-rails/renderers/json' -module Renderer - mattr_reader :view_path - @@view_path = 'app/views' +module RablRails + module Renderer + mattr_reader :view_path + @@view_path = 'app/views' - def render(object, template, options = {}) - format = options.delete(:format) || 'json' - source = find_template(template, format, options.delete(:view_path)) - compiled_template = Compiler.new.compile_source(source) + class Context + def initialize + @_assigns = {} + end + + def assigns + @_assigns + end + end + + def render(object, template, options = {}) + format = options.delete(:format) || 'json' + source = find_template(template, format, options.delete(:view_path)) + compiled_template = Compiler.new.compile_source(source) - # TODO: context needs to be set from options - Renderers.const_get(format.upcase!).new(context).render(compiled_template) - end + c = Context.new + c.assigns[compiled_template.data.to_s[1..-1]] = object if compiled_template.data + + Renderers.const_get(format.upcase!).new(c).render(compiled_template) + end - private + private - def find_template(name, format, view_path = nil) - view_path ||= self.view_path - path = File.join(view_path, "#{name}.#{format}.rabl") - File.exists?(path) : File.read(path) : nil + def find_template(name, format, view_path = nil) + view_path ||= self.view_path + path = File.join(view_path, "#{name}.#{format}.rabl") + File.exists?(path) ? File.read(path) : nil + end end end \ No newline at end of file