From 3f1380408e091f4e1b7637b1a6c17c45908f913b Mon Sep 17 00:00:00 2001 From: did Date: Mon, 4 Jun 2012 08:26:10 -0700 Subject: [PATCH] fix issue #427 + make the content types API features more predictable --- Gemfile | 2 + .../api/content_types_controller.rb | 2 +- .../api/authorization/content_types.feature | 32 +++---- lib/locomotive/liquid/tags/extends.rb | 12 +-- .../locomotive/extensions/page/render_spec.rb | 88 +++++++++++++++++++ 5 files changed, 110 insertions(+), 26 deletions(-) create mode 100644 spec/models/locomotive/extensions/page/render_spec.rb diff --git a/Gemfile b/Gemfile index 36ceb703..0d21af11 100755 --- a/Gemfile +++ b/Gemfile @@ -51,4 +51,6 @@ group :test do gem 'json_spec' gem 'database_cleaner' + + # gem 'debugger', :git => 'git://github.com/cldwalker/debugger.git' end diff --git a/app/controllers/locomotive/api/content_types_controller.rb b/app/controllers/locomotive/api/content_types_controller.rb index 9b5014d6..df68b5b8 100644 --- a/app/controllers/locomotive/api/content_types_controller.rb +++ b/app/controllers/locomotive/api/content_types_controller.rb @@ -5,7 +5,7 @@ module Locomotive load_and_authorize_resource :class => Locomotive::ContentType def index - @content_types = current_site.content_types + @content_types = current_site.content_types.order_by([[:name, :asc]]) respond_with(@content_types) end diff --git a/features/api/authorization/content_types.feature b/features/api/authorization/content_types.feature index 0012d91a..32b1a394 100644 --- a/features/api/authorization/content_types.feature +++ b/features/api/authorization/content_types.feature @@ -88,14 +88,14 @@ Feature: Content Types Then the JSON response should be an array And the JSON response should have 2 entries And the JSON should have the following: - | 1/name | "Employees" | - | 1/slug | "employees" | - | 1/entries_custom_fields/0/label | "Name" | - | 1/entries_custom_fields/0/name | "name" | - | 1/entries_custom_fields/0/type | "string" | - | 1/entries_custom_fields/1/label | "Position" | - | 1/entries_custom_fields/1/name | "position" | - | 1/entries_custom_fields/1/type | "string" | + | 0/name | "Employees" | + | 0/slug | "employees" | + | 0/entries_custom_fields/0/label | "Name" | + | 0/entries_custom_fields/0/name | "name" | + | 0/entries_custom_fields/0/type | "string" | + | 0/entries_custom_fields/1/label | "Position" | + | 0/entries_custom_fields/1/name | "position" | + | 0/entries_custom_fields/1/type | "string" | Scenario: Creating new content type as a Designer Given I have a "designer" API token @@ -127,14 +127,14 @@ Feature: Content Types Then the JSON response should be an array And the JSON response should have 2 entries And the JSON should have the following: - | 1/name | "Employees" | - | 1/slug | "employees" | - | 1/entries_custom_fields/0/label | "Name" | - | 1/entries_custom_fields/0/name | "name" | - | 1/entries_custom_fields/0/type | "string" | - | 1/entries_custom_fields/1/label | "Position" | - | 1/entries_custom_fields/1/name | "position" | - | 1/entries_custom_fields/1/type | "string" | + | 0/name | "Employees" | + | 0/slug | "employees" | + | 0/entries_custom_fields/0/label | "Name" | + | 0/entries_custom_fields/0/name | "name" | + | 0/entries_custom_fields/0/type | "string" | + | 0/entries_custom_fields/1/label | "Position" | + | 0/entries_custom_fields/1/name | "position" | + | 0/entries_custom_fields/1/type | "string" | Scenario: Creating new content type as an Author Given I have an "author" API token diff --git a/lib/locomotive/liquid/tags/extends.rb b/lib/locomotive/liquid/tags/extends.rb index 634c60c0..a2f8f978 100644 --- a/lib/locomotive/liquid/tags/extends.rb +++ b/lib/locomotive/liquid/tags/extends.rb @@ -18,14 +18,7 @@ module Locomotive def parse_parent_template if @template_name == 'parent' - if @context[:cached_parent] - @context[:parent_page] = @context[:cached_parent] #.clone # parent must not be modified - - @context[:cached_parent].instance_variable_set(:@template, nil) # force to reload the template - @context[:cached_parent] = nil - else - @context[:parent_page] = @context[:page].parent - end + @context[:parent_page] = @context[:cached_parent] || @context[:page].parent else locale = ::Mongoid::Fields::I18n.locale @@ -40,7 +33,8 @@ module Locomotive raise PageNotTranslated.new("Page with fullpath '#{@template_name}' was not translated") if parent_template.nil? - @context[:parent_page].instance_variable_set(:@template, parent_template) + # force the page to restore the original version of its template (from the serialized version) + @context[:parent_page].instance_variable_set(:@template, nil) parent_template end diff --git a/spec/models/locomotive/extensions/page/render_spec.rb b/spec/models/locomotive/extensions/page/render_spec.rb new file mode 100644 index 00000000..14bb6fa1 --- /dev/null +++ b/spec/models/locomotive/extensions/page/render_spec.rb @@ -0,0 +1,88 @@ +require 'spec_helper' + +describe Locomotive::Extensions::Page::Render do + + before(:each) do + Locomotive::Site.any_instance.stubs(:create_default_pages!).returns(true) + @site = FactoryGirl.create(:site) + @home = FactoryGirl.create(:page, :site => @site, :raw_template => + """ + Hello world + {% block header %}Home header{% endblock %} + {% block main %}My home page{% endblock %} + """) + end + + it 'renders the home page' do + render(@home).should == 'Hello world, Home header, My home page' + end + + describe '#inheritance' do + + before(:each) do + @inner = FactoryGirl.create(:sub_page, :slug => 'innerpage', :site => @site, :raw_template => + """ + {% extends parent %} + {% block header %}Inner header{% endblock %} + {% block main %}Inner page{% endblock %} + """) + @contact = FactoryGirl.create(:sub_page, :slug => 'contact', :site => @site, :raw_template => + """ + {% extends 'innerpage' %} + {% block header %}Contact header{% endblock %} + {% block main %}Contact page{% endblock %} + """) + @about = FactoryGirl.create(:sub_page, :slug => 'about', :site => @site, :raw_template => + """ + {% extends 'innerpage' %} + {% block main %}About page{% endblock %} + """) + end + + it 'renders the inner page' do + render(@inner).should == 'Hello world, Inner header, Inner page' + end + + it 'renders the contact page' do + render(@contact).should == 'Hello world, Contact header, Contact page' + end + + it 'renders the contact page' do + render(@about).should == 'Hello world, Inner header, About page' + end + + context 'when parent page got modified' do + + before(:each) do + @home.raw_template = """ + Hello world (UPDATED) + {% block header %}Home header (UPDATED){% endblock %} + {% block main %}My home page (UPDATED){% endblock %} + """ + @home.save & @home.reload + @inner = Locomotive::Page.find(@inner._id) + @contact = Locomotive::Page.find(@contact._id) + @about = Locomotive::Page.find(@about._id) + end + + it 'reflects changes on the inner page' do + render(@inner).should == 'Hello world (UPDATED), Inner header, Inner page' + end + + it 'reflects changes on the contact page' do + render(@contact).should == 'Hello world (UPDATED), Contact header, Contact page' + end + + it 'reflects changes on the about page' do + render(@about).should == 'Hello world (UPDATED), Inner header, About page' + end + + end + + end + + def render(page) + page.render(::Liquid::Context.new({}, {}, {}, false)).strip.gsub(/(\s{2,})/, ', ') + end + +end \ No newline at end of file