adding basic page functionality, a default route and functional tests for the page controller

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@8 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2007-10-03 06:34:21 +00:00
parent 77a8931fd8
commit b2212d1efa
10 changed files with 171 additions and 10 deletions

View File

@ -1,7 +1,3 @@
# Filters added to this controller apply to all controllers in the application.
# Likewise, all the methods added will be available for all controllers.
class ApplicationController < ActionController::Base
# Pick a unique cookie name to distinguish our session data from others'
session :session_key => '_barleysodas_session_id'
end

View File

@ -1,2 +1,89 @@
class PagesController < ApplicationController
end
append_before_filter :fetch_page, :only => [ :show, :edit, :update, :destroy ]
# GET /pages
# GET /pages.xml
def index
@page = Page.find_by_title 'HomePage'
@page ||= Page.create :title => 'HomePage',
:redcloth => 'Welcome to BarleySodas!'
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @page.to_xml }
end
end
# GET /pages/1
# GET /pages/1.xml
def show
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @page.to_xml }
end
end
# GET /pages/new
def new
@page = Page.new
end
# GET /pages/1;edit
def edit
end
# POST /pages
# POST /pages.xml
def create
@page = Page.new params[:page]
respond_to do |format|
if @page.save
flash[:notice] = 'Page was successfully created.'
format.html { redirect_to page_url(@page) }
format.xml { head :created, :location => page_url(@page) }
else
format.html { render :action => "new" }
format.xml { render :xml => @page.errors.to_xml }
end
end
end
# PUT /pages/1
# PUT /pages/1.xml
def update
respond_to do |format|
if @page.update_attributes(params[:page])
flash[:notice] = 'Page was successfully updated.'
format.html { redirect_to page_url(@page) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @page.errors.to_xml }
end
end
end
# DELETE /pages/1
# DELETE /pages/1.xml
def destroy
@page.destroy
respond_to do |format|
format.html { redirect_to pages_url }
format.xml { head :ok }
end
end
##
# Redirects the user to a default location.
#
def default_action
redirect_to(pages_path)
end
protected
def fetch_page
@page = Page.find_by_title(Page.title_from_url(params[:id]))
end
end

View File

@ -1,3 +1,9 @@
# Methods added to this helper will be available to all templates in the application.
module ApplicationHelper
##
# Returns the title for a page. This could be a Page title or something else.
#
def page_title
return @page.title if @page
"BarleySodas :: #{controller_class_name.gsub(/Controller/, '')}"
end
end

View File

@ -20,6 +20,20 @@ class Page < ActiveRecord::Base
:message => 'may only contain letters, numbers and spaces'
before_save :update_html
##
# Returns an url-friendly title for making links.
#
def title_for_url
self.title.gsub(/ /, '_')
end
##
# Gets a title from an url name.
#
def self.title_from_url(title)
title.to_s.gsub(/_/, ' ')
end
protected
##

View File

@ -0,0 +1,9 @@
<div class="page_container">
<h1><%= @page.title -%></h1>
<div class="page_container_content">
<%= @page.html %>
</div>
<div class="page_container_controls">
Current Version: <%= @page.version -%>
</div>
</div>

View File

@ -0,0 +1 @@
<%= render :partial => 'page' -%>

View File

@ -17,9 +17,10 @@ ActionController::Routing::Routes.draw do |map|
# Allow downloading Web Service WSDL as a file with an extension
# instead of a file named 'wsdl'
map.connect ':controller/service.wsdl', :action => 'wsdl'
# Install the default route as the lowest priority.
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
map.connect '/', :controller => 'pages', :action => 'default_action'
end

View File

@ -5,3 +5,7 @@ one:
two:
id: 2
title: my other title page
homepage:
id: 3
title: HomePage
redcloth: Homepage Text

View File

@ -13,8 +13,45 @@ class PagesControllerTest < Test::Unit::TestCase
@response = ActionController::TestResponse.new
end
# Replace this with your real tests.
def test_truth
assert true
def test_should_get_index
get :index
assert_response :success
assert assigns(:page)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_page
old_count = Page.count
post :create, :page => { :title => 'TestPage' }
assert_equal old_count+1, Page.count
assert_redirected_to page_path(assigns(:page))
end
def test_should_show_page
get :show, :id => 'HomePage'
assert_response :success
end
def test_should_get_edit
get :edit, :id => 'HomePage'
assert_response :success
end
def test_should_update_page
put :update, :id => 'HomePage', :page => { :title => 'HomePage' }
assert_redirected_to page_path(assigns(:page))
end
def test_should_destroy_page
old_count = Page.count
delete :destroy, :id => 'HomePage'
assert_equal old_count-1, Page.count
assert_redirected_to pages_path
end
end

View File

@ -16,4 +16,10 @@ class PageTest < Test::Unit::TestCase
p.save
assert p.html !~ /ul/
end
def test_url_titles
p = Page.new :title => 'Test Title'
assert Page.title_from_url('Test_Title') == p.title
assert p.title_for_url == 'Test_Title'
end
end