diff --git a/app/controllers/beers_controller.rb b/app/controllers/beers_controller.rb new file mode 100644 index 0000000..672e97a --- /dev/null +++ b/app/controllers/beers_controller.rb @@ -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 diff --git a/app/views/beers/_beer_form.rhtml b/app/views/beers/_beer_form.rhtml new file mode 100644 index 0000000..675b1c4 --- /dev/null +++ b/app/views/beers/_beer_form.rhtml @@ -0,0 +1,15 @@ +

+ <%= text_field 'beer', 'title' %> +

+

+ <%= text_field 'beer', 'abv' %> +

+

+ <%= text_field 'beer', 'original_gravity' %> +

+

+ <%= text_field 'beer', 'final_gravity' %> +

+

+ <%= text_area 'page', 'redcloth' %> +

\ No newline at end of file diff --git a/config/routes.rb b/config/routes.rb index c82955d..755064a 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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 diff --git a/db/migrate/003_create_beers.rb b/db/migrate/003_create_beers.rb new file mode 100644 index 0000000..2ee7abc --- /dev/null +++ b/db/migrate/003_create_beers.rb @@ -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 diff --git a/test/fixtures/beers.yml b/test/fixtures/beers.yml new file mode 100644 index 0000000..0d596d7 --- /dev/null +++ b/test/fixtures/beers.yml @@ -0,0 +1,4 @@ +# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html +one: + id: 1 + title: 1 diff --git a/test/fixtures/pages.yml b/test/fixtures/pages.yml index d70fe73..0b78680 100644 --- a/test/fixtures/pages.yml +++ b/test/fixtures/pages.yml @@ -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 diff --git a/test/functional/beers_controller_test.rb b/test/functional/beers_controller_test.rb new file mode 100644 index 0000000..95cc082 --- /dev/null +++ b/test/functional/beers_controller_test.rb @@ -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 diff --git a/test/functional/pages_controller_test.rb b/test/functional/pages_controller_test.rb index eb473ed..0031676 100644 --- a/test/functional/pages_controller_test.rb +++ b/test/functional/pages_controller_test.rb @@ -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 diff --git a/test/unit/beer_test.rb b/test/unit/beer_test.rb new file mode 100644 index 0000000..25f4364 --- /dev/null +++ b/test/unit/beer_test.rb @@ -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