adding beer functionality that works, including tests
git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@18 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7master
parent
c06aaba80c
commit
bb312db094
|
@ -0,0 +1,88 @@
|
|||
class BeersController < ApplicationController
|
||||
append_before_filter :get_beer_and_page, :only => [ :show, :edit, :update,
|
||||
:destroy ]
|
||||
|
||||
# GET /beers
|
||||
# GET /beers.xml
|
||||
def index
|
||||
@pages, @beers = paginate :beers, :include => 'page', :per_page => 50,
|
||||
:order => 'beers.title ASC'
|
||||
respond_to do |format|
|
||||
format.html # index.rhtml
|
||||
format.xml { render :xml => @beers.to_xml }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /beers/1
|
||||
# GET /beers/1.xml
|
||||
def show
|
||||
respond_to do |format|
|
||||
format.html # show.rhtml
|
||||
format.xml { render :xml => @beer.to_xml }
|
||||
end
|
||||
end
|
||||
|
||||
# GET /beers/new
|
||||
def new
|
||||
@beer = Beer.new
|
||||
@page = Page.new
|
||||
end
|
||||
|
||||
# GET /beers/1;edit
|
||||
def edit
|
||||
end
|
||||
|
||||
# POST /beers
|
||||
# POST /beers.xml
|
||||
def create
|
||||
@beer = Beer.new(params[:beer])
|
||||
@page = Page.new(params[:page])
|
||||
@beer.page = @page
|
||||
respond_to do |format|
|
||||
if @beer.save
|
||||
flash[:notice] = 'Beer was successfully created.'
|
||||
format.html { redirect_to beer_url(@beer.page.title_for_url) }
|
||||
format.xml { head :created,
|
||||
:location => beer_url(@beer.page.title_for_url) }
|
||||
else
|
||||
format.html { render :action => "new" }
|
||||
format.xml { render :xml => @beer.errors.to_xml }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# PUT /beers/1
|
||||
# PUT /beers/1.xml
|
||||
def update
|
||||
respond_to do |format|
|
||||
@page.attributes = params[:page]
|
||||
@beer.attributes = params[:beer]
|
||||
if @beer.save
|
||||
flash[:notice] = 'Beer was successfully updated.'
|
||||
format.html { redirect_to beer_url(@beer) }
|
||||
format.xml { head :ok }
|
||||
else
|
||||
format.html { render :action => "edit" }
|
||||
format.xml { render :xml => @beer.errors.to_xml }
|
||||
end
|
||||
end
|
||||
end
|
||||
|
||||
# DELETE /beers/1
|
||||
# DELETE /beers/1.xml
|
||||
def destroy
|
||||
@beer.destroy
|
||||
respond_to do |format|
|
||||
format.html { redirect_to beers_url }
|
||||
format.xml { head :ok }
|
||||
end
|
||||
end
|
||||
|
||||
protected
|
||||
|
||||
def get_beer_and_page
|
||||
@beer = Beer.find_by_title(Page.title_from_url(params[:id]),
|
||||
:include => [ 'page' ])
|
||||
@page = @beer.page
|
||||
end
|
||||
end
|
|
@ -0,0 +1,15 @@
|
|||
<p>
|
||||
<label for="beer_title">Title</label> <%= text_field 'beer', 'title' %>
|
||||
</p>
|
||||
<p>
|
||||
<label for="beer_abv">ABV</label> <%= text_field 'beer', 'abv' %>
|
||||
</p>
|
||||
<p>
|
||||
<label for="beer_original_gravity">Original Gravity</label> <%= text_field 'beer', 'original_gravity' %>
|
||||
</p>
|
||||
<p>
|
||||
<label for="beer_final_gravity">Final Gravity</label> <%= text_field 'beer', 'final_gravity' %>
|
||||
</p>
|
||||
<p>
|
||||
<label for="page_redcloth">Description</label> <%= text_area 'page', 'redcloth' %>
|
||||
</p>
|
|
@ -1,8 +1,8 @@
|
|||
ActionController::Routing::Routes.draw do |map|
|
||||
map.resources :pages
|
||||
map.resources :beers, :pages
|
||||
|
||||
# The priority is based upon order of creation: first created -> highest priority.
|
||||
|
||||
|
||||
# Sample of regular route:
|
||||
# map.connect 'products/:id', :controller => 'catalog', :action => 'view'
|
||||
# Keep in mind you can assign values other than :controller and :action
|
||||
|
|
|
@ -0,0 +1,14 @@
|
|||
class CreateBeers < ActiveRecord::Migration
|
||||
def self.up
|
||||
create_table :beers do |t|
|
||||
t.column :title, :string
|
||||
t.column :abv, :float
|
||||
t.column :original_gravity, :float
|
||||
t.column :final_gravity, :float
|
||||
end
|
||||
end
|
||||
|
||||
def self.down
|
||||
drop_table :beers
|
||||
end
|
||||
end
|
|
@ -0,0 +1,4 @@
|
|||
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
|
||||
one:
|
||||
id: 1
|
||||
title: 1
|
|
@ -2,6 +2,8 @@
|
|||
one:
|
||||
id: 1
|
||||
title: my title page for testing
|
||||
owner_id: 1
|
||||
owner_type: Beer
|
||||
two:
|
||||
id: 2
|
||||
title: my other title page
|
||||
|
|
|
@ -0,0 +1,57 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
require 'beers_controller'
|
||||
|
||||
# Re-raise errors caught by the controller.
|
||||
class BeersController; def rescue_action(e) raise e end; end
|
||||
|
||||
class BeersControllerTest < Test::Unit::TestCase
|
||||
fixtures :beers, :pages
|
||||
|
||||
def setup
|
||||
@controller = BeersController.new
|
||||
@request = ActionController::TestRequest.new
|
||||
@response = ActionController::TestResponse.new
|
||||
end
|
||||
|
||||
def test_should_get_index
|
||||
get :index
|
||||
assert_response :success
|
||||
assert assigns(:beers)
|
||||
end
|
||||
|
||||
def test_should_get_new
|
||||
get :new
|
||||
assert_response :success
|
||||
end
|
||||
|
||||
def test_should_create_beer
|
||||
old_count = Beer.count
|
||||
post :create, :beer => { :title => '1' }
|
||||
assert_equal old_count+1, Beer.count
|
||||
|
||||
assert_redirected_to beer_path(assigns(:beer).page.title_for_url)
|
||||
end
|
||||
|
||||
def test_should_show_beer
|
||||
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_beer
|
||||
put :update, :id => 1, :beer => { :title => '1' }
|
||||
assert_redirected_to beer_path(assigns(:beer).page.title_for_url)
|
||||
end
|
||||
|
||||
def test_should_destroy_beer
|
||||
old_count = Beer.count
|
||||
delete :destroy, :id => 1
|
||||
assert_equal old_count-1, Beer.count
|
||||
|
||||
assert_redirected_to beers_path
|
||||
end
|
||||
end
|
|
@ -29,7 +29,7 @@ class PagesControllerTest < Test::Unit::TestCase
|
|||
post :create, :page => { :title => 'TestPage' }
|
||||
assert_equal old_count+1, Page.count
|
||||
|
||||
assert_redirected_to page_path(assigns(:page))
|
||||
assert_redirected_to page_path(assigns(:page).title_for_url)
|
||||
end
|
||||
|
||||
def test_should_show_page
|
||||
|
@ -44,7 +44,7 @@ class PagesControllerTest < Test::Unit::TestCase
|
|||
|
||||
def test_should_update_page
|
||||
put :update, :id => 'HomePage', :page => { :title => 'HomePage' }
|
||||
assert_redirected_to page_path(assigns(:page))
|
||||
assert_redirected_to page_path(assigns(:page).title_for_url)
|
||||
end
|
||||
|
||||
def test_should_destroy_page
|
||||
|
|
|
@ -0,0 +1,10 @@
|
|||
require File.dirname(__FILE__) + '/../test_helper'
|
||||
|
||||
class BeerTest < Test::Unit::TestCase
|
||||
fixtures :beers
|
||||
|
||||
# Replace this with your real tests.
|
||||
def test_truth
|
||||
assert true
|
||||
end
|
||||
end
|
Reference in New Issue