template errors in pages are handled correctly now (bug) + {% extends parent %} is implemented

This commit is contained in:
dinedine 2010-08-22 01:56:44 +02:00
parent 8196c0a6de
commit 4805d8813e
5 changed files with 36 additions and 8 deletions

View File

@ -9,6 +9,8 @@ module Models
field :serialized_template, :type => Binary
before_validation :serialize_template
validate :template_must_be_valid
end
module InstanceMethods
@ -19,22 +21,38 @@ module Models
protected
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
def serialize_template
if self.new_record? || self.raw_template_changed?
@parsing_errors = []
begin
@template = ::Liquid::Template.parse(self.raw_template, { :site => self.site })
@template.root.context.clear
self.parse
self.serialized_template = BSON::Binary.new(Marshal.dump(@template))
# TODO: let other pages inheriting from that one and modify them in consequences
# TODO: build array of parent pages
rescue ::Liquid::SyntaxError => error
self.errors.add :template, :liquid_syntax
@parsing_errors << :liquid_syntax
rescue ::Locomotive::Liquid::PageNotFound => error
self.errors.add :template, :liquid_extend
@parsing_errors << :liquid_extend
end
end
end
def template_must_be_valid
@parsing_errors.try(:each) { |msg| self.errors.add :template, msg }
end
end
end

View File

@ -13,6 +13,8 @@ class Snippet
## callbacks ##
before_validation :normalize_slug
# TODO: after_save callback to let pages embedding this snippet know about the changes the user has just made.
## validations ##
validates_presence_of :site, :name, :slug, :template
validates_uniqueness_of :slug, :scope => :site_id

View File

@ -245,7 +245,7 @@ en:
information: General information
meta: SEO Metadata
code: Code
raw_template: Layout
raw_template: Template
credentials: Credentials
language: Language
sites: Sites
@ -260,6 +260,8 @@ en:
presentation: Presentation
attributes: Attributes
labels:
page:
raw_template: Template
theme_asset:
new:
source: File

View File

@ -18,7 +18,7 @@ Scenario: Creating a valid page
And I fill in "Title" with "Test"
And I fill in "Slug" with "test"
And I select "Home page" from "Parent"
And I fill in "Layout template" with "Lorem ipsum...."
And I fill in "Template" with "Lorem ipsum...."
And I press "Create"
Then I should see "Page was successfully created."
And I should have "Lorem ipsum...." in the test page
@ -27,7 +27,7 @@ Scenario: Updating a valid page
When I go to pages
And I follow "Home page"
And I fill in "Title" with "Home page !"
And I fill in "Layout template" with "My new content is here"
And I fill in "Template" with "My new content is here"
And I press "Update"
Then I should see "Page was successfully updated."
And I should have "My new content is here" in the index page

View File

@ -4,7 +4,13 @@ module Locomotive
class Extends < ::Liquid::Extends
def parse_parent_template(context)
page = context[:site].pages.where(:fullpath => @template_name.gsub("'", '')).first
page = nil
if @template_name == 'parent'
page = context[:page].parent
else
page = context[:site].pages.where(:fullpath => @template_name.gsub("'", '')).first
end
raise PageNotFound.new("Page with fullpath '#{@template_name}' was not found") if page.nil?