From 35ba212214aa91ba5047dd548dc06dba224600ca Mon Sep 17 00:00:00 2001 From: ccocchi Date: Thu, 22 Mar 2012 11:24:55 +0100 Subject: [PATCH] Add root possibility --- lib/rabl-fast-json/compiler.rb | 10 +++++----- lib/rabl-fast-json/handler.rb | 2 +- lib/rabl-fast-json/library.rb | 7 ++++--- lib/rabl-fast-json/railtie.rb | 2 +- lib/rabl-fast-json/template.rb | 14 ++++++++++++++ 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/lib/rabl-fast-json/compiler.rb b/lib/rabl-fast-json/compiler.rb index e935e40..acbfb22 100644 --- a/lib/rabl-fast-json/compiler.rb +++ b/lib/rabl-fast-json/compiler.rb @@ -77,16 +77,16 @@ module RablFastJson @template.data, @template.root_name = data, name end - def method_missing(name, *args, &block) - @context.respond_to?(name) ? @context.send(name, *args, &block) : super - end - protected def extract_data_and_name(name_or_data) case name_or_data when Symbol - [name_or_data, name_or_data] + if name_or_data.to_s.start_with?('@') + [name_or_data, nil] + else + [name_or_data, name_or_data] + end when Hash name_or_data.first else diff --git a/lib/rabl-fast-json/handler.rb b/lib/rabl-fast-json/handler.rb index c86635c..b95f5e2 100644 --- a/lib/rabl-fast-json/handler.rb +++ b/lib/rabl-fast-json/handler.rb @@ -4,7 +4,7 @@ module RablFastJson cattr_accessor :default_format self.default_format = 'application/json' - def self.call(template) + def self.call(template) %{ RablFastJson::Library.instance. get_rendered_template(#{template.source.inspect}, self) diff --git a/lib/rabl-fast-json/library.rb b/lib/rabl-fast-json/library.rb index cb68fcc..4bcf258 100644 --- a/lib/rabl-fast-json/library.rb +++ b/lib/rabl-fast-json/library.rb @@ -16,12 +16,13 @@ module RablFastJson compiled_template = get_compiled_template(path, source, context) compiled_template.context = context - - ActiveSupport::JSON.encode(compiled_template.render) + body = compiled_template.render + ActiveSupport::JSON.encode(compiled_template.has_root_name? ? { compiled_template.root_name => body } : body) end def get_compiled_template(path, source, context) - @cached_templates[path] ||= Compiler.new(context).compile_source(source) + # @cached_templates[path] ||= + Compiler.new(context).compile_source(source) end def get(path, context) diff --git a/lib/rabl-fast-json/railtie.rb b/lib/rabl-fast-json/railtie.rb index 9d2453c..0bc78c3 100644 --- a/lib/rabl-fast-json/railtie.rb +++ b/lib/rabl-fast-json/railtie.rb @@ -2,7 +2,7 @@ module RablFastJson class Railtie < Rails::Railtie initializer "rabl.initialize" do |app| ActiveSupport.on_load(:action_view) do - ActionView::Template.register_template_handler :rabl2, RablFastJson::Handlers::Rabl + ActionView::Template.register_template_handler :rabl, RablFastJson::Handlers::Rabl end end end diff --git a/lib/rabl-fast-json/template.rb b/lib/rabl-fast-json/template.rb index b302862..be89e67 100644 --- a/lib/rabl-fast-json/template.rb +++ b/lib/rabl-fast-json/template.rb @@ -18,6 +18,20 @@ module RablFastJson instance_variable_set("@#{k}", v) unless k.start_with?('_') || k == @data } end + + def has_root_name? + !@root_name.nil? + end + + def method_missing(name, *args, &block) + @context.respond_to?(name) ? @context.send(name, *args, &block) : super + end + + def partial(template_path, options = {}) + raise "No object was given to partial" if options[:object].blank? + template = Library.instance.get(template_path, @context) + template.render_resource(options[:object]) + end def render get_object_from_context