engine/lib/locomotive/liquid/tags/editable/base.rb

74 lines
2.3 KiB
Ruby
Raw Normal View History

module Locomotive
module Liquid
module Tags
module Editable
class Base < ::Liquid::Block
Syntax = /(#{::Liquid::QuotedFragment})(\s*,\s*#{::Liquid::Expression}+)?/
def initialize(tag_name, markup, tokens, context)
if markup =~ Syntax
@slug = $1.gsub('\'', '')
2012-03-19 01:29:59 +00:00
@options = { :fixed => false }
markup.scan(::Liquid::TagAttributes) { |key, value| @options[key.to_sym] = value.gsub(/^'/, '').gsub(/'$/, '') }
else
raise ::Liquid::SyntaxError.new("Syntax Error in 'editable_xxx' - Valid syntax: editable_xxx <slug>(, <options>)")
end
super
end
def end_tag
super
2011-07-06 23:34:11 +00:00
if @context[:page].present?
2012-03-19 01:29:59 +00:00
@context[:page].add_or_update_editable_element(default_element_attributes, document_type)
end
end
def render(context)
current_page = context.registers[:page]
element = current_page.find_editable_element(context['block'].try(:name), @slug)
2011-07-06 23:34:11 +00:00
if element.present?
render_element(context, element)
else
Locomotive.log :error, "[editable element] missing element `#{context['block'].try(:name)}` / #{@slug} on #{current_page.fullpath}"
''
end
end
protected
2012-03-19 01:29:59 +00:00
def default_element_attributes
{
:block => @options[:block] || @context[:current_block].try(:name),
:slug => @slug,
:hint => @options[:hint],
:priority => @options[:priority] || 0,
:fixed => !!@options[:fixed],
:disabled => false,
:from_parent => false,
:_type => self.document_type.to_s
}
end
def render_element(element)
raise 'FIXME: has to be overidden'
end
def document_type
raise 'FIXME: has to be overidden'
end
2011-07-06 23:34:11 +00:00
def render_default_content(context)
render_all(@nodelist, context).join(' ')
end
end
end
end
end
end