Permalink
Browse files

Increased test coverage. Made preview work for page type changes.

  • Loading branch information...
1 parent 64d5311 commit 4fe2e788306240c1c3e54480711fa8cae95961e5 Glenn Murray committed Oct 22, 2008
Showing with 64 additions and 20 deletions.
  1. +5 −0 README
  2. +5 −3 app/controllers/preview_controller.rb
  3. +54 −17 spec/controllers/preview_controller_spec.rb
View
5 README
@@ -6,3 +6,8 @@ An extension to preview editing a page in it's enclosing layout to give the auth
git clone git://github.com/tricycle/radiant-page-preview-extension.git vendor/extensions/page_preview
rake radiant:extensions:page_preview:update
+
+== Todo
+
+ 1. Allow previewing of existing pages when changing page types
+
@@ -1,11 +1,13 @@
+class ClassChangedException < Exception; end
+
class PreviewController < ApplicationController
layout false
def show
- Page.transaction do # what a horrible way to achieve readonly-ness :(
+ Page.transaction do # Extra safe don't save anything voodoo
PagePart.transaction do
construct_page.process(request,response)
@performed_render = true
- raise "Don't you dare save any changes"
+ raise "Don't you dare save any changes"
end
end
rescue => exception
@@ -27,7 +29,7 @@ def construct_page
page = page_class.new(params[:page])
page.parent = Page.find($1)
elsif request.referer =~ %r{/admin/pages/edit/(\d+)}
- page = Page.find($1)
+ page = Page.find($1).becomes(page_class)
page.parts = []
page.attributes = params[:page]
else
@@ -9,9 +9,14 @@ class TestPage < Page
login_as :admin
end
- it 'should construct the page and process it' do
- controller.should_receive(:construct_page).and_return(mock('page', :process => true))
- post :show
+ describe 'show' do
+ it 'should construct the page and process it' do
+ controller.should_receive(:construct_page).and_return(mock('page', :process => true))
+ post :show
+ end
+
+ it 'should test that it does not modify page'
+ it 'should test that it renders exceptions'
end
describe 'construct page' do
@@ -21,20 +26,39 @@ class TestPage < Page
before(:each) do
controller.stub!(:params).
and_return({:page => {:class_name => 'page'}.with_indifferent_access}.with_indifferent_access)
+ @page = Page.new(:id => 1)
+ controller.stub!(:request).and_return(request)
end
describe 'any page', :shared => true do
- it 'should not save any changes'
- it 'should contain parts created'
- it 'should contain new parts contents'
+ it 'should retrieve parts from params' do
+ controller.params[:part] = []
+ controller.construct_page
+ controller.construct_page.parts.should be_empty
+ end
+ it 'should contain parts created' do
+ controller.params[:part] = {'0' => {:name=> 'new part'}}
+
+ controller.construct_page.parts.should_not be_empty
+ controller.construct_page.parts.first.name.should == 'new part'
+ end
+ it 'should not contain remove deleted parts' do
+ @page.parts.build :name => 'delete me'
+
+ controller.construct_page.parts.should be_empty
+ end
end
describe 'new child' do
before do
- Page.stub!(:find).and_return Page.new(:id => 1)
+ Page.stub!(:find).and_return @page
request.stub!(:referer).and_return('/admin/pages/1/child/new')
- controller.stub!(:request).and_return(request)
end
it_should_behave_like "any page"
+ it 'should not save any changes' do
+ page_count = Page.count
+ controller.construct_page
+ Page.count.should == page_count
+ end
it 'should get the referrer' do
request.should_receive(:referer).at_least(:once)
controller.construct_page
@@ -57,20 +81,33 @@ class TestPage < Page
Page.stub!(:find).and_return parent
controller.construct_page.parent.should == parent
end
- it 'should not save' do
- debugger
- end
end
describe 'edit existing page' do
before do
-
+ request.stub!(:referer).and_return('/admin/pages/edit/1')
+ controller.params[:page][:class_name] = "Page"
+ @page = Page.find(:first)
+ Page.stub!(:find).and_return @page
end
it_should_behave_like "any page"
- it 'should have the original pages children'
- it 'should have the original pages parent'
- it 'should not contain parts deleted'
- it 'should complain if page class has changed on page edit'
- it 'should have params attributes, not the existing page attributes'
+ it 'should not save any changes' #TODO: Test this somehow
+ it 'should have the original pages children' do
+ #TODO: Make this text more explicitly test children
+ controller.construct_page.children.should == @page.children
+ end
+ it 'should have the original pages parent' do
+ #TODO: Make this text more explicitly test parent
+ controller.construct_page.parent.should == @page.parent
+ end
+ it 'should change page class if it has changed' do
+ controller.params[:page][:class_name] = "TestPage"
+ controller.construct_page.class.should == TestPage
+ end
+ it 'should have params attributes, not the existing page attributes' do
+ @page.title = 'foo'
+ controller.params[:page][:title] = 'bar'
+ controller.construct_page.title.should == 'bar'
+ end
end
end
end

0 comments on commit 4fe2e78

Please sign in to comment.