2010-08-21 22:48:24 +00:00
|
|
|
module Models
|
|
|
|
module Extensions
|
|
|
|
module Page
|
|
|
|
module Parse
|
|
|
|
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
field :serialized_template, :type => Binary
|
|
|
|
|
|
|
|
before_validation :serialize_template
|
2010-08-21 23:56:44 +00:00
|
|
|
|
|
|
|
validate :template_must_be_valid
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
|
|
|
|
|
|
|
def template
|
|
|
|
@template ||= Marshal.load(read_attribute(:serialized_template).to_s) rescue nil
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
2010-08-21 23:56:44 +00:00
|
|
|
def parse
|
|
|
|
@template = ::Liquid::Template.parse(self.raw_template, { :site => self.site, :page => self })
|
|
|
|
@template.root.context.clear
|
|
|
|
|
|
|
|
# TODO: walk thru the document tree to get parents as well as used snippets
|
|
|
|
end
|
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
def serialize_template
|
|
|
|
if self.new_record? || self.raw_template_changed?
|
2010-08-21 23:56:44 +00:00
|
|
|
@parsing_errors = []
|
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
begin
|
2010-08-21 23:56:44 +00:00
|
|
|
self.parse
|
2010-08-21 22:48:24 +00:00
|
|
|
|
|
|
|
self.serialized_template = BSON::Binary.new(Marshal.dump(@template))
|
2010-08-21 22:58:26 +00:00
|
|
|
|
2010-08-21 23:56:44 +00:00
|
|
|
# TODO: let other pages inheriting from that one and modify them in consequences
|
|
|
|
|
|
|
|
# TODO: build array of parent pages
|
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
rescue ::Liquid::SyntaxError => error
|
2010-08-21 23:56:44 +00:00
|
|
|
@parsing_errors << :liquid_syntax
|
2010-08-21 22:58:26 +00:00
|
|
|
rescue ::Locomotive::Liquid::PageNotFound => error
|
2010-08-21 23:56:44 +00:00
|
|
|
@parsing_errors << :liquid_extend
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-21 23:56:44 +00:00
|
|
|
def template_must_be_valid
|
|
|
|
@parsing_errors.try(:each) { |msg| self.errors.add :template, msg }
|
|
|
|
end
|
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|