Make render work with same signature as
standard RABL gem
This commit is contained in:
parent
63b50b2a31
commit
9e6b4db8eb
|
@ -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
|
||||||
|
|
||||||
|
|
|
@ -1,24 +1,38 @@
|
||||||
require 'rabl-rails/renderers/base'
|
require 'rabl-rails/renderers/base'
|
||||||
require 'rabl-rails/renderers/json'
|
require 'rabl-rails/renderers/json'
|
||||||
|
|
||||||
module Renderer
|
module RablRails
|
||||||
mattr_reader :view_path
|
module Renderer
|
||||||
@@view_path = 'app/views'
|
mattr_reader :view_path
|
||||||
|
@@view_path = 'app/views'
|
||||||
|
|
||||||
def render(object, template, options = {})
|
class Context
|
||||||
format = options.delete(:format) || 'json'
|
def initialize
|
||||||
source = find_template(template, format, options.delete(:view_path))
|
@_assigns = {}
|
||||||
compiled_template = Compiler.new.compile_source(source)
|
end
|
||||||
|
|
||||||
# TODO: context needs to be set from options
|
def assigns
|
||||||
Renderers.const_get(format.upcase!).new(context).render(compiled_template)
|
@_assigns
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
|
||||||
private
|
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)
|
||||||
|
|
||||||
def find_template(name, format, view_path = nil)
|
c = Context.new
|
||||||
view_path ||= self.view_path
|
c.assigns[compiled_template.data.to_s[1..-1]] = object if compiled_template.data
|
||||||
path = File.join(view_path, "#{name}.#{format}.rabl")
|
|
||||||
File.exists?(path) : File.read(path) : nil
|
Renderers.const_get(format.upcase!).new(c).render(compiled_template)
|
||||||
|
end
|
||||||
|
|
||||||
|
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
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
Loading…
Reference in New Issue