Enabled the use of variable values as the default values for editable content.

This commit is contained in:
M. Scott Ford 2011-07-06 12:47:28 -04:00
parent c9bc8c8222
commit 314c1293d9
2 changed files with 28 additions and 8 deletions

View File

@ -126,4 +126,16 @@ 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
"""

View File

@ -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