From b2212d1efad164028909a11c836ef780b7880c2e Mon Sep 17 00:00:00 2001 From: andrew Date: Wed, 3 Oct 2007 06:34:21 +0000 Subject: [PATCH] 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 --- app/controllers/application.rb | 4 -- app/controllers/pages_controller.rb | 89 +++++++++++++++++++++++- app/helpers/application_helper.rb | 8 ++- app/models/page.rb | 14 ++++ app/views/pages/_page.rhtml | 9 +++ app/views/pages/index.rhtml | 1 + config/routes.rb | 3 +- test/fixtures/pages.yml | 4 ++ test/functional/pages_controller_test.rb | 43 +++++++++++- test/unit/page_test.rb | 6 ++ 10 files changed, 171 insertions(+), 10 deletions(-) create mode 100644 app/views/pages/_page.rhtml create mode 100644 app/views/pages/index.rhtml diff --git a/app/controllers/application.rb b/app/controllers/application.rb index 54ae66f..42ebc61 100644 --- a/app/controllers/application.rb +++ b/app/controllers/application.rb @@ -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 diff --git a/app/controllers/pages_controller.rb b/app/controllers/pages_controller.rb index 95217e1..3d8c34b 100644 --- a/app/controllers/pages_controller.rb +++ b/app/controllers/pages_controller.rb @@ -1,2 +1,89 @@ class PagesController < ApplicationController -end \ No newline at end of file + 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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index 22a7940..952e033 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/page.rb b/app/models/page.rb index 6257120..7e7dc50 100644 --- a/app/models/page.rb +++ b/app/models/page.rb @@ -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 ## diff --git a/app/views/pages/_page.rhtml b/app/views/pages/_page.rhtml new file mode 100644 index 0000000..5fc4b0d --- /dev/null +++ b/app/views/pages/_page.rhtml @@ -0,0 +1,9 @@ +
+

<%= @page.title -%>

+
+<%= @page.html %> +
+
+Current Version: <%= @page.version -%> +
+
diff --git a/app/views/pages/index.rhtml b/app/views/pages/index.rhtml new file mode 100644 index 0000000..de5f585 --- /dev/null +++ b/app/views/pages/index.rhtml @@ -0,0 +1 @@ +<%= render :partial => 'page' -%> diff --git a/config/routes.rb b/config/routes.rb index f9893ec..c82955d 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/test/fixtures/pages.yml b/test/fixtures/pages.yml index ae4c1f9..d70fe73 100644 --- a/test/fixtures/pages.yml +++ b/test/fixtures/pages.yml @@ -5,3 +5,7 @@ one: two: id: 2 title: my other title page +homepage: + id: 3 + title: HomePage + redcloth: Homepage Text diff --git a/test/functional/pages_controller_test.rb b/test/functional/pages_controller_test.rb index 194d466..eb473ed 100644 --- a/test/functional/pages_controller_test.rb +++ b/test/functional/pages_controller_test.rb @@ -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 diff --git a/test/unit/page_test.rb b/test/unit/page_test.rb index 2aa389c..a450981 100644 --- a/test/unit/page_test.rb +++ b/test/unit/page_test.rb @@ -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