diff --git a/app/controllers/breweries_controller.rb b/app/controllers/breweries_controller.rb new file mode 100644 index 0000000..4b04cdd --- /dev/null +++ b/app/controllers/breweries_controller.rb @@ -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 diff --git a/app/helpers/breweries_helper.rb b/app/helpers/breweries_helper.rb new file mode 100644 index 0000000..350d903 --- /dev/null +++ b/app/helpers/breweries_helper.rb @@ -0,0 +1,2 @@ +module BreweriesHelper +end diff --git a/app/models/brewery.rb b/app/models/brewery.rb new file mode 100644 index 0000000..5d55994 --- /dev/null +++ b/app/models/brewery.rb @@ -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 diff --git a/app/views/breweries/_brewery_form.rhtml b/app/views/breweries/_brewery_form.rhtml new file mode 100644 index 0000000..192cc50 --- /dev/null +++ b/app/views/breweries/_brewery_form.rhtml @@ -0,0 +1,21 @@ +

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

+

+ <%= text_field 'brewery', 'address_1' %>
<%= text_field 'brewery', 'address_2' %> +

+

+ <%= text_field 'brewery', 'city' %> +

+

+ <%= text_field 'brewery', 'state' %> +

+

+ <%= text_field 'brewery', 'postal_code' %> +

+

+ <%= country_select 'brewery', 'country', [ 'United States', 'United Kingdom', 'Germany' ] %> +

+

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

\ No newline at end of file diff --git a/app/views/breweries/edit.rhtml b/app/views/breweries/edit.rhtml new file mode 100644 index 0000000..466d212 --- /dev/null +++ b/app/views/breweries/edit.rhtml @@ -0,0 +1,15 @@ +

Editing brewery

+ +<%= error_messages_for :brewery %> + +<% form_for(:brewery, :url => brewery_path(@brewery.page.title_for_url), :html => { :method => :put }) do |f| %> +<%= render :partial => 'brewery_form' %> +

+ <%= submit_tag "Update" %> +

+<% end %> + + \ No newline at end of file diff --git a/app/views/breweries/index.rhtml b/app/views/breweries/index.rhtml new file mode 100644 index 0000000..c235423 --- /dev/null +++ b/app/views/breweries/index.rhtml @@ -0,0 +1,24 @@ +

Breweries

+ + + + + + + + +<% for brewery in @breweries %> + + + + + + +<% end %> +
Title
<%= brewery.title -%><%= link_to 'Show', brewery_path(brewery.page.title_for_url) %><%= link_to 'Edit', edit_brewery_path(brewery.page.title_for_url) %><%= link_to 'Destroy', brewery_path(brewery.page.title_for_url), :confirm => 'Are you sure?', :method => :delete %>
+ +
+ + \ No newline at end of file diff --git a/app/views/breweries/new.rhtml b/app/views/breweries/new.rhtml new file mode 100644 index 0000000..89ea2bc --- /dev/null +++ b/app/views/breweries/new.rhtml @@ -0,0 +1,14 @@ +

New brewery

+ +<%= error_messages_for :brewery %> + +<% form_for(:brewery, :url => breweries_path, :html => { :method => :post }) do |f| %> +<%= render :partial => 'brewery_form' %> +

+ <%= submit_tag "Create" %> +

+<% end %> + + \ No newline at end of file diff --git a/app/views/breweries/show.rhtml b/app/views/breweries/show.rhtml new file mode 100644 index 0000000..3f38afe --- /dev/null +++ b/app/views/breweries/show.rhtml @@ -0,0 +1,14 @@ +

<%= @brewery.title -%>

+ +
+ +
+ +<%= @page.html %> + + diff --git a/config/routes.rb b/config/routes.rb index 755064a..d00b5f5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -1,24 +1,6 @@ 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' diff --git a/db/migrate/004_create_breweries.rb b/db/migrate/004_create_breweries.rb new file mode 100644 index 0000000..195f3b3 --- /dev/null +++ b/db/migrate/004_create_breweries.rb @@ -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 diff --git a/test/fixtures/beers.yml b/test/fixtures/beers.yml index 0d596d7..933aa9b 100644 --- a/test/fixtures/beers.yml +++ b/test/fixtures/beers.yml @@ -2,3 +2,4 @@ one: id: 1 title: 1 + brewery_id: 1 \ No newline at end of file diff --git a/test/fixtures/breweries.yml b/test/fixtures/breweries.yml new file mode 100644 index 0000000..0d596d7 --- /dev/null +++ b/test/fixtures/breweries.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 0b78680..353add0 100644 --- a/test/fixtures/pages.yml +++ b/test/fixtures/pages.yml @@ -7,6 +7,8 @@ one: two: id: 2 title: my other title page + owner_id: 1 + owner_type: Brewery homepage: id: 3 title: HomePage diff --git a/test/functional/breweries_controller_test.rb b/test/functional/breweries_controller_test.rb new file mode 100644 index 0000000..34b80f4 --- /dev/null +++ b/test/functional/breweries_controller_test.rb @@ -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 diff --git a/test/unit/brewery_test.rb b/test/unit/brewery_test.rb new file mode 100644 index 0000000..de8b0a0 --- /dev/null +++ b/test/unit/brewery_test.rb @@ -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