From 925c7fff8d344452d0605ec0beb5c2223f40aa49 Mon Sep 17 00:00:00 2001 From: Jacques Crocker Date: Sun, 1 Aug 2010 00:00:06 -0700 Subject: [PATCH] All Page to be passed in a :body attribute This :body attribute will then be used when creating the default body PagePart --- app/models/extensions/page/parts.rb | 8 +++++--- app/models/page_part.rb | 4 ++-- spec/models/page_spec.rb | 26 ++++++++++++++++++++++++++ 3 files changed, 33 insertions(+), 5 deletions(-) diff --git a/app/models/extensions/page/parts.rb b/app/models/extensions/page/parts.rb index 5151d9dc..25f03e92 100644 --- a/app/models/extensions/page/parts.rb +++ b/app/models/extensions/page/parts.rb @@ -6,9 +6,11 @@ module Models extend ActiveSupport::Concern included do - - before_create { |p| p.parts << PagePart.build_body_part if p.parts.empty? } - + before_validation do |p| + if p.parts.empty? + p.parts << PagePart.build_body_part(p.try(:body)) + end + end end module InstanceMethods diff --git a/app/models/page_part.rb b/app/models/page_part.rb index f2a3c51f..32cac9b0 100644 --- a/app/models/page_part.rb +++ b/app/models/page_part.rb @@ -24,10 +24,10 @@ class PagePart "{% capture content_for_#{self.slug} %}#{self.value}{% endcapture %}" end - def self.build_body_part + def self.build_body_part(body_content = nil) self.new({ :name => I18n.t('attributes.defaults.page_parts.name'), - :value => I18n.t('attributes.defaults.pages.other.body'), + :value => body_content || I18n.t('attributes.defaults.pages.other.body'), :slug => 'layout' }) end diff --git a/spec/models/page_spec.rb b/spec/models/page_spec.rb index d2cbaa7e..31b7826f 100644 --- a/spec/models/page_spec.rb +++ b/spec/models/page_spec.rb @@ -334,6 +334,32 @@ describe Page do end + + describe "creating a new page" do + + context "with a body" do + before do + @site = Factory(:site, :subdomain => "somethingweird") + @page = Page.create({ + :slug => "some_slug", + :title => "Page Title", + :body => "Page Body", + :published => true, + :site => @site + }) + end + + it "should be valid" do + @page.should be_valid + end + + it "should render the passed in body attribute of the page" do + @page.render(Liquid::Context.new).should == "PageBody" + end + end + end + + describe 'templatized extension' do before(:each) do