From 8376c0d974cb21bd608bebc0a9dde52aae7aa5c2 Mon Sep 17 00:00:00 2001 From: ccocchi Date: Wed, 12 Sep 2012 01:34:54 +0200 Subject: [PATCH] RablRails now accepts local methods for child nodes and root objects. Local methods are used by decent_exposure or focused_controller --- lib/rabl-rails/compiler.rb | 2 +- lib/rabl-rails/renderers/base.rb | 12 ++++++++++-- 2 files changed, 11 insertions(+), 3 deletions(-) diff --git a/lib/rabl-rails/compiler.rb b/lib/rabl-rails/compiler.rb index 7e78ea2..eb2f9a5 100644 --- a/lib/rabl-rails/compiler.rb +++ b/lib/rabl-rails/compiler.rb @@ -133,7 +133,7 @@ module RablRails name_or_data end end - + def sub_compile(data) return {} unless block_given? old_template, @template = @template, {} diff --git a/lib/rabl-rails/renderers/base.rb b/lib/rabl-rails/renderers/base.rb index cb9bcac..2f09f30 100644 --- a/lib/rabl-rails/renderers/base.rb +++ b/lib/rabl-rails/renderers/base.rb @@ -18,7 +18,9 @@ module RablRails # method defined by the renderer. # def render(template) - collection_or_resource = instance_variable_get(template.data) if template.data + collection_or_resource = if template.data + template.data.to_s.start_with?('@') ? instance_variable_get(template.data) : @_context.send(template.data) + end collection_or_resource = @_context.target_object unless collection_or_resource || template.data == false || !@_context.respond_to?(:target_object) output_hash = collection_or_resource.respond_to?(:each) ? render_collection(collection_or_resource, template.source) : render_resource(collection_or_resource, template.source) @@ -55,7 +57,13 @@ module RablRails when Hash current_value = value.dup data_symbol = current_value.delete(:_data) - object = data_symbol.nil? ? data : data_symbol.to_s.start_with?('@') ? instance_variable_get(data_symbol) : data.send(data_symbol) + object = if data_symbol == nil + data + else + data_symbol.to_s.start_with?('@') ? instance_variable_get(data_symbol) + : data.respond_to?(data_symbol) ? data.send(data_symbol) + : send(data_symbol) + end if key.to_s.start_with?('_') # glue current_value.each_pair { |k, v|