From 314c1293d9d2f67428c9442edf48733b0a8c8dd9 Mon Sep 17 00:00:00 2001 From: "M. Scott Ford" Date: Wed, 6 Jul 2011 12:47:28 -0400 Subject: [PATCH] Enabled the use of variable values as the default values for editable content. --- features/engine/editable_elements.feature | 14 ++++++++++++- lib/locomotive/liquid/tags/editable/base.rb | 22 ++++++++++++++------- 2 files changed, 28 insertions(+), 8 deletions(-) diff --git a/features/engine/editable_elements.feature b/features/engine/editable_elements.feature index de72d5a3..053d1bea 100644 --- a/features/engine/editable_elements.feature +++ b/features/engine/editable_elements.feature @@ -125,5 +125,17 @@ Scenario: Insert editable files """ My application file is /default.pdf """ - + +Scenario: Render liquid variable used as default editable file name + Given a page named "hello-world" with the template: + """ + {% assign default_file_name = '/different-default.pdf' %} + My application file is {% editable_file 'a_file', hint: 'please enter a new file' %}{{ default_file_name }}{% endeditable_file %} + """ + When I view the rendered page at "/hello-world" + Then the rendered output should look like: + """ + + My application file is /different-default.pdf + """ diff --git a/lib/locomotive/liquid/tags/editable/base.rb b/lib/locomotive/liquid/tags/editable/base.rb index aaa04c50..1c06c8e0 100644 --- a/lib/locomotive/liquid/tags/editable/base.rb +++ b/lib/locomotive/liquid/tags/editable/base.rb @@ -27,7 +27,7 @@ module Locomotive :slug => @slug, :hint => @options[:hint], :default_attribute => @options[:default], - :default_content => default_content, + :default_content => default_content_option, :assignable => @options[:assignable], :disabled => false, :from_parent => false @@ -41,6 +41,9 @@ module Locomotive element = current_page.find_editable_element(context['block'].try(:name), @slug) if element.present? + unless element.default_content.present? + element.default_content = render_default_content(context) + end render_element(context, element) else Locomotive.logger "[editable element] missing element `#{context['block'].try(:name)}` / #{@slug} on #{current_page.fullpath}" @@ -58,15 +61,20 @@ module Locomotive raise 'FIXME: has to be overidden' end - - def default_content + def default_content_option + result = nil if @options[:default].present? - @context[:page].send(@options[:default]) - else - @nodelist.first.to_s + result = @context[:page].send(@options[:default]) end + result + end + + def render_default_content(context) + puts "Old: #{@nodelist.first.to_s}" + result = render_all(@nodelist, context) + puts "New: #{result.inspect}" + result end - end end