adding breweries

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@23 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2007-11-15 05:35:29 +00:00
parent 92dee7b971
commit 290add240e
15 changed files with 313 additions and 19 deletions

View File

@ -0,0 +1,90 @@
class BreweriesController < ApplicationController
append_before_filter :get_brewery_and_page, :only => [ :show, :edit, :update,
:destroy ]
# GET /breweries
# GET /breweries.xml
def index
@pages, @breweries = paginate :breweries, :include => 'page',
:order => 'breweries.title ASC', :per_page => 50
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @breweries.to_xml }
end
end
# GET /breweries/1
# GET /breweries/1.xml
def show
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @brewery.to_xml }
end
end
# GET /breweries/new
def new
@brewery = Brewery.new
@page = Page.new
end
# GET /breweries/1;edit
def edit
end
# POST /breweries
# POST /breweries.xml
def create
@brewery = Brewery.new(params[:brewery])
@page = Page.new(params[:page])
@brewery.page = @page
respond_to do |format|
if @brewery.save
flash[:notice] = 'Brewery was successfully created.'
format.html { redirect_to brewery_url(@brewery.page.title_for_url) }
format.xml { head :created,
:location => brewery_url(@brewery.page.title_for_url) }
else
format.html { render :action => "new" }
format.xml { render :xml => @brewery.errors.to_xml }
end
end
end
# PUT /breweries/1
# PUT /breweries/1.xml
def update
respond_to do |format|
@page.attributes = params[:page]
@brewery.attributes = params[:brewery]
if @brewery.save
flash[:notice] = 'Brewery was successfully updated.'
format.html { redirect_to brewery_url(@brewery.page.title_for_url) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @brewery.errors.to_xml }
end
end
end
# DELETE /breweries/1
# DELETE /breweries/1.xml
def destroy
@brewery.destroy
respond_to do |format|
format.html { redirect_to breweries_url }
format.xml { head :ok }
end
end
protected
def get_brewery_and_page
@brewery = Brewery.find_by_title(Page.title_from_url(params[:id]),
:include => [ 'page' ])
raise ActiveRecord::RecordNotFound.new if @brewery.nil?
@page = @brewery.page
end
end

View File

@ -0,0 +1,2 @@
module BreweriesHelper
end

38
app/models/brewery.rb Normal file
View File

@ -0,0 +1,38 @@
##
# This model represents a company that produces Beer.
#
class Brewery < ActiveRecord::Base
has_many :beers
has_one :page, :foreign_key => 'owner_id', :dependent => :destroy,
:conditions => "pages.owner_type = 'Brewery'"
before_save :ensure_page_valid
after_save :save_page
protected
##
# This model always has a Page associated. Save it before this model to make
# sure that everything is kosher with the name and whatnot.
#
def save_page
page.save
end
##
# This will let the Page model keep track of names being unique and all.
# Might be nil, so let the after_save hook create it.
#
def ensure_page_valid
if page.nil?
self.errors.add(:page, "is missing")
else
page.owner = self
page.title = self.title
page.errors.each do |key, val|
self.errors.add(key, val)
end if !page.valid?
end
return false if self.errors.size > 0
true
end
end

View File

@ -0,0 +1,21 @@
<p>
<label for="brewery_title">Title</label> <%= text_field 'brewery', 'title' %>
</p>
<p>
<label for="brewery_address_1">Address</label> <%= text_field 'brewery', 'address_1' %><br /><%= text_field 'brewery', 'address_2' %>
</p>
<p>
<label for="brewery_city">City</label> <%= text_field 'brewery', 'city' %>
</p>
<p>
<label for="brewery_state">State</label> <%= text_field 'brewery', 'state' %>
</p>
<p>
<label for="brewery_postal_code">Postal Code</label> <%= text_field 'brewery', 'postal_code' %>
</p>
<p>
<label for="brewery_county">Country</label> <%= country_select 'brewery', 'country', [ 'United States', 'United Kingdom', 'Germany' ] %>
</p>
<p>
<label for="page_redcloth">Description</label> <%= text_area 'page', 'redcloth' %>
</p>

View File

@ -0,0 +1,15 @@
<h1>Editing brewery</h1>
<%= error_messages_for :brewery %>
<% form_for(:brewery, :url => brewery_path(@brewery.page.title_for_url), :html => { :method => :put }) do |f| %>
<%= render :partial => 'brewery_form' %>
<p>
<%= submit_tag "Update" %>
</p>
<% end %>
<div id="related_links">
<%= link_to 'Show', brewery_path(@brewery.page.title_for_url) %> |
<%= link_to 'Breweries', breweries_path %>
</div>

View File

@ -0,0 +1,24 @@
<h1>Breweries</h1>
<table>
<tr>
<th>Title</th>
<th></th>
<th></th>
<th></th>
</tr>
<% for brewery in @breweries %>
<tr>
<td><%= brewery.title -%></td>
<td><%= link_to 'Show', brewery_path(brewery.page.title_for_url) %></td>
<td><%= link_to 'Edit', edit_brewery_path(brewery.page.title_for_url) %></td>
<td><%= link_to 'Destroy', brewery_path(brewery.page.title_for_url), :confirm => 'Are you sure?', :method => :delete %></td>
</tr>
<% end %>
</table>
<br />
<div id="related_links">
<%= link_to 'New brewery', new_brewery_path %>
</div>

View File

@ -0,0 +1,14 @@
<h1>New brewery</h1>
<%= error_messages_for :brewery %>
<% form_for(:brewery, :url => breweries_path, :html => { :method => :post }) do |f| %>
<%= render :partial => 'brewery_form' %>
<p>
<%= submit_tag "Create" %>
</p>
<% end %>
<div id="related_links">
<%= link_to 'Breweries', breweries_path %>
</div>

View File

@ -0,0 +1,14 @@
<h1><%= @brewery.title -%></h1>
<div id="attributes">
<ul>
<li>Available Beers: <%= @brewery.beers.size -%></li>
</ul>
</div>
<%= @page.html %>
<div id="related_links">
<%= link_to 'Edit', edit_brewery_path(@brewery.page.title_for_url) %> |
<%= link_to 'Breweries', breweries_path %>
</div>

View File

@ -1,24 +1,6 @@
ActionController::Routing::Routes.draw do |map| ActionController::Routing::Routes.draw do |map|
map.resources :beers, :pages map.resources :beers, :breweries, :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
# Sample of named route:
# map.purchase 'products/:id/purchase', :controller => 'catalog', :action => 'purchase'
# This route can be invoked with purchase_url(:id => product.id)
# You can have the root of your site routed by hooking up ''
# -- just remember to delete public/index.html.
# map.connect '', :controller => "welcome"
# Allow downloading Web Service WSDL as a file with an extension
# instead of a file named 'wsdl'
# Install the default route as the lowest priority.
map.connect ':controller/:action/:id.:format' map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id' map.connect ':controller/:action/:id'

View File

@ -0,0 +1,20 @@
class CreateBreweries < ActiveRecord::Migration
def self.up
create_table :breweries do |t|
t.column :title, :string
t.column :address_1, :string
t.column :address_2, :string
t.column :city, :string
t.column :state, :string
t.column :postal_code, :string
t.column :country, :string
end
add_column :beers, :brewery_id, :integer
add_index :beers, :brewery_id
end
def self.down
drop_table :breweries
remove_column :beers, :brewery_id
end
end

View File

@ -2,3 +2,4 @@
one: one:
id: 1 id: 1
title: 1 title: 1
brewery_id: 1

4
test/fixtures/breweries.yml vendored Normal file
View File

@ -0,0 +1,4 @@
# Read about fixtures at http://ar.rubyonrails.org/classes/Fixtures.html
one:
id: 1
title: 1

View File

@ -7,6 +7,8 @@ one:
two: two:
id: 2 id: 2
title: my other title page title: my other title page
owner_id: 1
owner_type: Brewery
homepage: homepage:
id: 3 id: 3
title: HomePage title: HomePage

View File

@ -0,0 +1,57 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'breweries_controller'
# Re-raise errors caught by the controller.
class BreweriesController; def rescue_action(e) raise e end; end
class BreweriesControllerTest < Test::Unit::TestCase
fixtures :breweries
def setup
@controller = BreweriesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_get_index
get :index
assert_response :success
assert assigns(:breweries)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_brewery
old_count = Brewery.count
post :create, :brewery => { :title => 'title' }
assert_equal old_count+1, Brewery.count
assert_redirected_to brewery_path(assigns(:brewery).page.title_for_url)
end
def test_should_show_brewery
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_brewery
put :update, :id => 1, :brewery => { :title => '1' }
assert_redirected_to brewery_path(assigns(:brewery).page.title_for_url)
end
def test_should_destroy_brewery
old_count = Brewery.count
delete :destroy, :id => 1
assert_equal old_count-1, Brewery.count
assert_redirected_to breweries_path
end
end

10
test/unit/brewery_test.rb Normal file
View File

@ -0,0 +1,10 @@
require File.dirname(__FILE__) + '/../test_helper'
class BreweryTest < Test::Unit::TestCase
fixtures :breweries
# Replace this with your real tests.
def test_truth
assert true
end
end