adding base help system

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@71 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2007-12-27 07:24:38 +00:00
parent 8280da87e1
commit cca9f190a8
11 changed files with 231 additions and 2 deletions

View File

@ -0,0 +1,104 @@
class HelpController < ApplicationController
append_before_filter :fetch_page, :only => [ :show, :edit, :update, :destroy ]
# GET /helps
# GET /helps.xml
def index
@page = Page.find_by_title_and_owner_type 'HomePage', 'Help'
@content_title = 'BarleySodas Help'
@secondary_title = ''
@tags = Page.tags(:limit => 25, :order => 'name DESC',
:owner_type => 'Help')
end
# GET /helps/1
# GET /helps/1.xml
def show
@secondary_title = ''
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @page.to_xml }
end
end
# GET /helps/new
def new
@page = Page.new
@secondary_title = 'Creating help page'
end
# GET /helps/1;edit
def edit
@secondary_title = 'Updating help page'
end
# POST /helps
# POST /helps.xml
def create
@page = Page.new(params[:page])
@page.owner_type = 'Help'
respond_to do |format|
if @page.save
flash[:notice] = 'Help was successfully created.'
format.html {
if @page.title == 'HomePage'
redirect_to :controller => 'help', :action => 'index'
else
redirect_to help_url({ :id => @page.title_for_url })
end
}
format.xml { head :created,
:location => help_url({ :id => @page.title_for_url }) }
else
format.html {
@secondary_title = 'Creating help page'
render :action => "new"
}
format.xml { render :xml => @page.errors.to_xml }
end
end
end
# PUT /helps/1
# PUT /helps/1.xml
def update
@page.attributes = params[:page]
respond_to do |format|
if @page.save
flash[:notice] = 'Help was successfully updated.'
format.html {
if @page.title == 'HomePage'
redirect_to :controller => 'help', :action => 'index'
else
redirect_to help_url({ :id => @page.title_for_url })
end
}
format.xml { head :ok }
else
format.html {
@secondary_title = 'Updating help page'
render :action => "edit"
}
format.xml { render :xml => @page.errors.to_xml, :status => 400 }
end
end
end
# DELETE /helps/1
# DELETE /helps/1.xml
def destroy
@page.destroy
respond_to do |format|
format.html { redirect_to :controller => 'help', :action => 'index' }
format.xml { head :ok }
end
end
protected
def fetch_page
@page = Page.find_by_title_and_owner_type(Page.title_from_url(params[:id]),
'Help')
raise ActiveRecord::RecordNotFound.new if @page.nil?
end
end

View File

@ -0,0 +1,11 @@
module HelpHelper
def show_help_link(page, better_title = 'Show')
link_to better_title, help_path({ :id => page.title_for_url }),
{ :title => page.title }
end
def edit_help_link(page)
link_to 'Edit Help Page', edit_help_path({ :id => page.title_for_url }),
{ :title => "Edit Help Page #{page.title}" }
end
end

12
app/views/help/edit.rhtml Normal file
View File

@ -0,0 +1,12 @@
<%= error_messages_for :page %>
<% form_for(:help, :url => help_path({ :id => @page.title_for_url }), :html => { :method => :put }) do |f| %>
<%= render :partial => 'pages/page_form' %>
<p>
<%= submit_tag "Update" %>
</p>
<% end %>
<% content_for :sidebar do -%>
<%= show_help_link(@page) unless @page.title == 'HomePage' -%><br />
<% end -%>

View File

@ -0,0 +1,6 @@
<%= render :partial => 'pages/page' -%>
<% content_for :sidebar do -%>
<%= edit_help_link(@page) -%><br />
<%= render :partial => 'shared/tag_cloud' %>
<% end -%>

11
app/views/help/new.rhtml Normal file
View File

@ -0,0 +1,11 @@
<%= error_messages_for :page %>
<% form_for(:help, :url => { :controller => 'help', :action => 'index' }, :html => { :method => :post }) do |f| %>
<p>
<label for="page_title">Title</label> <%= text_field 'page', 'title' %>
</p>
<%= render :partial => 'pages/page_form' %>
<p>
<%= submit_tag "Create" %>
</p>
<% end %>

View File

@ -0,0 +1,6 @@
<%= render :partial => 'pages/page' %>
<% content_for :sidebar do -%>
<%= edit_help_link(@page) -%><br />
<%= link_to 'Destroy', help_path(@page.title_for_url), :confirm => 'Are you sure?', :method => :delete unless @page.title == 'HomePage' -%><br />
<% end -%>

View File

@ -38,6 +38,7 @@
<%= link_to_unless_current 'Discussions', discussions_path -%><br />
<%= link_to_unless_current 'Peoples', peoples_path -%><br />
<%= link_to_unless_current 'Roles', roles_path -%><br />
<%= link_to_unless_current 'Help', :controller => 'help', :action => 'index' -%><br />
<% unless logged_in? -%><%= link_to_unless_current 'Login', new_session_path -%><% else -%><%= link_to 'Logout', session_path(:id => session[:people_title]), :method => :delete -%><% end %>
<hr />
<%= yield :sidebar %>

View File

@ -1,6 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :beers, :breweries, :pages, :discussions, :peoples, :roles,
:sessions
:sessions, :help
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'

View File

@ -0,0 +1,21 @@
class HelpStartPage < ActiveRecord::Migration
def self.up
redcloths = "
h1. Thanks for using BarleySodas
Please check out these topics for more specialized information.
* [[Peoples]]
* [[Beers]]
* [[Breweries]]
* [[Discussions]]
* [[Pages]]
* [[Sessions]]
"
Page.create :title => 'HomePage', :owner_type => 'Help', :redcloth => redcloths
end
def self.down
Page.destroy_all("owner_type = 'Help'")
end
end

View File

@ -5,7 +5,7 @@ base_actions = ApplicationController.action_methods
# rather than defining them here.
controllers = [ AutocompleteController, SessionsController, PagesController,
PeoplesController, BeersController, BreweriesController, RolesController,
DiscussionsController ]
DiscussionsController, HelpController ]
controllers.each do |c|
actions = c.action_methods - base_actions
cname = c.controller_name

View File

@ -0,0 +1,57 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'helps_controller'
# Re-raise errors caught by the controller.
class HelpController; def rescue_action(e) raise e end; end
class HelpControllerTest < Test::Unit::TestCase
fixtures :helps
def setup
@controller = HelpController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_get_index
get :index
assert_response :success
assert assigns(:helps)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_help
old_count = Help.count
post :create, :help => { }
assert_equal old_count+1, Help.count
assert_redirected_to help_path(assigns(:help))
end
def test_should_show_help
get :show, :id => 1
assert_response :success
end
def test_should_get_edit
get :edit, :id => 1
assert_response :success
end
def test_should_update_help
put :update, :id => 1, :help => { }
assert_redirected_to help_path(assigns(:help))
end
def test_should_destroy_help
old_count = Help.count
delete :destroy, :id => 1
assert_equal old_count-1, Help.count
assert_redirected_to helps_path
end
end