Make render work with same signature as

standard RABL gem
This commit is contained in:
ccocchi 2012-07-25 19:22:10 +02:00
parent 63b50b2a31
commit 9e6b4db8eb
2 changed files with 31 additions and 15 deletions

View File

@ -16,6 +16,8 @@ require 'rabl-rails/railtie'
require 'multi_json' require 'multi_json'
module RablRails module RablRails
extend Renderer
mattr_accessor :cache_templates mattr_accessor :cache_templates
@@cache_templates = true @@cache_templates = true

View File

@ -1,17 +1,30 @@
require 'rabl-rails/renderers/base' require 'rabl-rails/renderers/base'
require 'rabl-rails/renderers/json' require 'rabl-rails/renderers/json'
module RablRails
module Renderer module Renderer
mattr_reader :view_path mattr_reader :view_path
@@view_path = 'app/views' @@view_path = 'app/views'
class Context
def initialize
@_assigns = {}
end
def assigns
@_assigns
end
end
def render(object, template, options = {}) def render(object, template, options = {})
format = options.delete(:format) || 'json' format = options.delete(:format) || 'json'
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)
# TODO: context needs to be set from options c = Context.new
Renderers.const_get(format.upcase!).new(context).render(compiled_template) 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 end
private private
@ -19,6 +32,7 @@ module Renderer
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")
File.exists?(path) : File.read(path) : nil File.exists?(path) ? File.read(path) : nil
end
end end
end end