Permalink
Browse files

Initial spec coverage (new page) and change all

params access to symbols
  • Loading branch information...
1 parent 02e5587 commit a5d5da61b6b7350e9b2811ad95b45ee68456fd3d @cradle cradle committed Oct 17, 2008
Showing with 60 additions and 5 deletions.
  1. +5 −5 app/controllers/preview_controller.rb
  2. +55 −0 spec/controllers/preview_controller_spec.rb
@@ -13,7 +13,7 @@ def show
private
def page_class
- classname = params['page']['class_name'].classify
+ classname = params[:page][:class_name].classify
if Page.descendants.collect(&:name).include?(classname)
classname.constantize
else
@@ -23,16 +23,16 @@ def page_class
def construct_page
if request.referer =~ %r{/admin/pages/(\d+)/child/new}
- page = page_class.new(params['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.parts = []
- page.attributes = params['page']
+ page.attributes = params[:page]
else
- page = page_class.new(params['page'])
+ page = page_class.new(params[:page])
end
- params.fetch('part', []).each do |i, attrs|
+ params.fetch(:part, []).each do |i, attrs|
page.parts.build(attrs)
end
return page
@@ -0,0 +1,55 @@
+require File.expand_path(File.dirname(__FILE__) + '/../spec_helper')
+
+class TestPage < Page
+end
+
+describe PreviewController do
+ scenario :users
+ before(:each) do
+ 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
+ end
+
+ describe 'construct page' do
+ before(:all) do
+ PreviewController.class_eval { public :construct_page }
+ end
+ before(:each) do
+ controller.stub!(:params).and_return({:page => {:class_name => 'page'}.with_indifferent_access}.with_indifferent_access)
+ end
+ describe 'new child' do
+ before do
+ Page.stub!(:find).and_return Page.new(:id => 1)
+ request.stub!(:referer).and_return('/admin/pages/1/child/new')
+ controller.stub!(:request).and_return(request)
+ end
+ it 'should get the referrer' do
+ request.should_receive(:referer).at_least(:once)
+ controller.construct_page
+ end
+ it 'should retrieve the class_name from the params' do
+ controller.params[:page].should_receive(:[]).with(:class_name).and_return 'page'
+ controller.construct_page
+ end
+ it 'should create a new page' do
+ controller.params[:page].stub!(:[]).with(:class_name).and_return 'test_page'
+ controller.construct_page.class.should == TestPage
+ end
+ it 'should set get the parent from the referer' do
+ request.stub!(:referer).and_return('/admin/pages/100/child/new')
+ Page.should_receive(:find).with('100')
+ controller.construct_page
+ end
+ it 'should assign the parent from the referer to the page' do
+ parent = mock_model(Page)
+ Page.stub!(:find).and_return parent
+ controller.construct_page.parent.should == parent
+ end
+ end
+ end
+
+end

0 comments on commit a5d5da6

Please sign in to comment.