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 @@ +