2011-04-01 00:34:19 +00:00
|
|
|
module Extensions
|
|
|
|
module Page
|
|
|
|
module Parse
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
extend ActiveSupport::Concern
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
included do
|
|
|
|
field :serialized_template, :type => Binary
|
|
|
|
field :template_dependencies, :type => Array, :default => []
|
|
|
|
field :snippet_dependencies, :type => Array, :default => []
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
attr_reader :template_changed
|
2010-08-28 00:00:05 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
before_validation :serialize_template
|
|
|
|
after_save :update_template_descendants
|
2010-08-21 23:56:44 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
validate :template_must_be_valid
|
2010-08-22 23:48:11 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
scope :pages, lambda { |domain| { :any_in => { :domains => [*domain] } } }
|
|
|
|
end
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
module InstanceMethods
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def template
|
|
|
|
@template ||= Marshal.load(read_attribute(:serialized_template).to_s) rescue nil
|
|
|
|
end
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
protected
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def serialize_template
|
|
|
|
if self.new_record? || self.raw_template_changed?
|
|
|
|
@template_changed = true
|
2010-08-21 23:56:44 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
@parsing_errors = []
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
begin
|
|
|
|
self._parse_and_serialize_template
|
|
|
|
rescue ::Liquid::SyntaxError => error
|
2011-06-01 21:43:12 +00:00
|
|
|
@parsing_errors << I18n.t(:liquid_syntax, :fullpath => self.fullpath, :error => error.to_s, :scope => [:errors, :messages, :page])
|
2011-04-01 00:34:19 +00:00
|
|
|
rescue ::Locomotive::Liquid::PageNotFound => error
|
2011-06-01 21:43:12 +00:00
|
|
|
@parsing_errors << I18n.t(:liquid_extend, :fullpath => self.fullpath, :scope => [:errors, :messages, :page])
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
|
|
|
end
|
2011-04-01 00:34:19 +00:00
|
|
|
end
|
2010-08-21 22:48:24 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def _parse_and_serialize_template(context = {})
|
|
|
|
self.parse(context)
|
|
|
|
self._serialize_template
|
|
|
|
end
|
2010-08-24 13:04:53 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def _serialize_template
|
|
|
|
self.serialized_template = BSON::Binary.new(Marshal.dump(@template))
|
|
|
|
end
|
2010-08-22 23:48:11 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def parse(context = {})
|
|
|
|
self.disable_all_editable_elements
|
2010-08-24 22:59:22 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
default_context = { :site => self.site, :page => self, :templates => [], :snippets => [] }
|
2010-08-24 13:04:53 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
context = default_context.merge(context)
|
2010-08-26 11:06:44 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
@template = ::Liquid::Template.parse(self.raw_template, context)
|
2010-08-26 11:06:44 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
self.template_dependencies = context[:templates]
|
|
|
|
self.snippet_dependencies = context[:snippets]
|
2010-08-24 13:04:53 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
@template.root.context.clear
|
|
|
|
end
|
2010-08-24 13:04:53 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def template_must_be_valid
|
|
|
|
@parsing_errors.try(:each) { |msg| self.errors.add :template, msg }
|
|
|
|
end
|
2010-08-21 23:56:44 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
def update_template_descendants
|
|
|
|
return unless @template_changed == true
|
2010-08-22 23:48:11 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# we admit at this point that the current template is up-to-date
|
|
|
|
template_descendants = self.site.pages.any_in(:template_dependencies => [self.id]).to_a
|
2010-08-22 23:48:11 +00:00
|
|
|
|
2011-04-01 00:34:19 +00:00
|
|
|
# group them by fullpath for better performance
|
|
|
|
cached = template_descendants.inject({}) { |memo, page| memo[page.fullpath] = page; memo }
|
2010-08-22 23:48:11 +00:00
|
|
|
|
2011-05-04 12:53:09 +00:00
|
|
|
self._update_direct_template_descendants(template_descendants.clone, cached)
|
2010-08-22 23:48:11 +00:00
|
|
|
|
2011-04-28 15:04:18 +00:00
|
|
|
# finally save them all
|
|
|
|
::Page.without_callback(:save, :after, :update_template_descendants) do
|
|
|
|
template_descendants.each do |page|
|
|
|
|
page.save(:validate => false)
|
|
|
|
end
|
2010-08-22 23:48:11 +00:00
|
|
|
end
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
|
|
|
|
2011-05-04 12:53:09 +00:00
|
|
|
def _update_direct_template_descendants(template_descendants, cached)
|
|
|
|
direct_descendants = template_descendants.select do |page|
|
|
|
|
((self.template_dependencies || []) + [self._id]) == (page.template_dependencies || [])
|
|
|
|
end
|
|
|
|
|
|
|
|
direct_descendants.each do |page|
|
|
|
|
page.send(:_parse_and_serialize_template, { :cached_parent => self, :cached_pages => cached })
|
|
|
|
|
|
|
|
template_descendants.delete(page) # no need to loop over it next time
|
|
|
|
|
|
|
|
page.send(:_update_direct_template_descendants, template_descendants, cached) # move down
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
2011-04-01 00:34:19 +00:00
|
|
|
|
2010-08-21 22:48:24 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|