adding styles for beers

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@78 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2008-01-07 05:46:43 +00:00
parent fb11eb1d47
commit 160561844b
16 changed files with 253 additions and 4 deletions

View File

@ -108,8 +108,8 @@ class ApplicationController < ActionController::Base
obj = Class.class_eval(obj_name.camelize).find(:first,
:conditions => [ cond_ary.join(' AND '), cond_var ])
# allow the chance to make a new one if you GET the URL
if request.get? and obj.nil?
# allow the chance to make a new one if you GET the URL, but not for Style
if request.get? and obj.nil? and obj_type != 'styles'
flash[:info] = "The #{obj_name} was not found, would you like to make it?"
redirect_to :action => 'new', :new_title => tfu
return

View File

@ -43,6 +43,7 @@ class BeersController < ApplicationController
allow_page_discussions
@page.attributes = params[:page]
@beer.attributes = params[:beer]
@beer.page = @page
brewery = Brewery.find_by_title(params[:brewery][:title]) rescue nil
@beer.brewery = brewery
respond_to do |format|
@ -95,6 +96,7 @@ class BeersController < ApplicationController
@beer = Beer.new
@beer.title = params[:new_title] if params[:new_title]
@page = Page.new
@beer.page = @page
end
def edit_stuff

View File

@ -0,0 +1,98 @@
class StylesController < ApplicationController
append_before_filter :fetch_model,
:only => [ :show, :edit, :update, :destroy ]
# GET /styles
# GET /styles.xml
def index
@content_title = 'Beverage Styles'
@secondary_title = 'Major Style Categories'
@styles = Style.major_styles
@tags = Page.tags(:limit => 25, :order => "name DESC",
:owner_type => 'Style')
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @styles.to_xml }
end
end
# GET /styles/1
# GET /styles/1.xml
def show
@children = @style.children
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @style.to_xml }
end
end
# GET /styles/new
def new
new_stuff
end
# GET /styles/1;edit
def edit
edit_stuff
end
# POST /styles
# POST /styles.xml
def create
new_stuff
@style.attributes = params[:style]
@page.attributes = params[:page]
respond_to do |format|
if @style.save
flash[:notice] = 'Style was successfully created.'
format.html { redirect_to style_url(@style.page.title_for_url) }
format.xml { head :created, :location => style_url(@style) }
else
format.html { render :action => "new" }
format.xml { render :xml => @style.errors.to_xml }
end
end
end
# PUT /styles/1
# PUT /styles/1.xml
def update
edit_stuff
@style.attributes = params[:style]
@page.attributes = params[:page]
@style.page = @page
respond_to do |format|
if @style.save
flash[:notice] = 'Style was successfully updated.'
format.html { redirect_to style_url(@style.page.title_for_url) }
format.xml { head :ok }
else
format.html { render :action => "edit" }
format.xml { render :xml => @style.errors.to_xml }
end
end
end
# DELETE /styles/1
# DELETE /styles/1.xml
def destroy
@style.destroy
respond_to do |format|
format.html { redirect_to styles_url }
format.xml { head :ok }
end
end
protected
def new_stuff
@secondary_title = 'New Style'
@style = Style.new
@page = Page.new
@style.page = @page
end
def edit_stuff
@secondary_title = 'Update Style'
end
end

View File

@ -0,0 +1,15 @@
module StylesHelper
def new_style_link
link_to 'New Style', new_style_path, { :title => 'Create a new style' }
end
def show_style_link(style)
link_to style.title, style_path(style.page.title_for_url),
{ :title => style.title }
end
def edit_style_link(style)
link_to 'Edit Style', edit_style_path(style.page.title_for_url),
{ :title => "Edit #{style.title}" }
end
end

View File

@ -4,6 +4,8 @@
class Beer < ActiveRecord::Base
belongs_to :brewery
has_one_tuxwiki_page :owner_class => 'Beer'
belongs_to :style
validates_presence_of :style_id
##
# Returns a list of attributes for the Page partial.
@ -20,6 +22,7 @@ class Beer < ActiveRecord::Base
unless final_gravity.to_s.empty?
pattr << "Final Gravity: #{final_gravity}"
end
pattr << "Style: #{style.title}"
pattr
end
end

View File

@ -10,6 +10,9 @@
<p>
<label for="beer_final_gravity">Final Gravity</label> <%= text_field 'beer', 'final_gravity' %>
</p>
<p>
<label for="beer_style_id">Style</label> <%= select 'beer', 'style_id', Style.for_select, { :selected => @beer.style_id } %>
</p>
<p>
<label for="brewery_title">Brewery</label> <%= text_field_with_auto_complete('brewery', 'title', {}, { :url => { :controller => 'autocomplete', :action => 'index' } }) %>
</p>

View File

@ -34,6 +34,7 @@
<div id="sidebar">
<%= link_to_unless_current 'Browse The Beer Wiki', pages_path -%><br />
<%= link_to_unless_current 'Browse Beers', beers_path -%><br />
<%= link_to_unless_current 'Beverage Styles', styles_path -%><br />
<%= link_to_unless_current 'Browse Breweries', breweries_path -%><br />
<%= link_to_unless_current 'Discussions', discussions_path -%><br />
<%= link_to_unless_current 'Peoples', peoples_path -%><br />

View File

@ -0,0 +1,20 @@
<p>
<label for="style_title">Title</label> <%= text_field 'style', 'title' %>
</p>
<p>
<label for="style_position">Position</label> <%= text_field 'style', 'position' %>
</p>
<p>
<label for="style_originating_location">Originating Location</label> <%= text_field 'style', 'originating_location' %>
</p>
<p>
<label for="style_gravity_bottom">Lowest Gravity</label> <%= text_field 'style', 'gravity_bottom' %>
</p>
<p>
<label for="style_gravity_top">Highest Gravity</label> <%= text_field 'style', 'gravity_top' %>
</p>
<p>
<label for="style_parent_id">Style Parent</label> <%= select 'style', 'parent_id', Style.for_select, { :include_blank => true, :selected => @style.parent_id.to_s } %>
</p>
<%= render :partial => 'pages/page_form' %>

View File

@ -0,0 +1,13 @@
<%= error_messages_for :style %>
<% form_for(:style, :url => style_path(@style.page.title_for_url), :html => { :method => :put }) do |f| %>
<%= render :partial => 'style_form' %>
<p>
<%= submit_tag "Update" %>
</p>
<% end %>
<% content_for :sidebar do -%>
<%= new_style_link -%><br />
<%= show_style_link(@style) -%<br />
<% end -%>

View File

@ -0,0 +1,10 @@
<% if @styles.empty? -%>
<h1>No styles found.</h1>
<% else -%>
<ol><%= @styles.collect { |s| '<li>' + link_to(s.title, style_path(:id => s.page.title_for_url)) + '</li>' } %></ol>
<% end -%>
<% content_for :sidebar do -%>
<%= new_style_link -%><br />
<%= render :partial => 'shared/tag_cloud' %>
<% end -%>

View File

@ -0,0 +1,8 @@
<%= error_messages_for :style %>
<% form_for(:style, :url => styles_path) do |f| %>
<%= render :partial => 'style_form' %>
<p>
<%= submit_tag "Create" %>
</p>
<% end %>

View File

@ -0,0 +1,11 @@
<%= render :partial => 'pages/page' %>
<% unless @style.children.empty? -%>
<ol><%= @style.children.collect { |x| '<li>' + show_style_link(x) + '</li>' } -%></ol>
<% end -%>
<% content_for :sidebar do -%>
<%= new_style_link -%><br />
<%= edit_style_link(@style) -%><br />
<%= link_to 'Destroy', style_path(@style.page.title_for_url), :confirm => 'Are you sure?', :method => :delete -%><br />
<% end -%>

View File

@ -1,6 +1,6 @@
ActionController::Routing::Routes.draw do |map|
map.resources :beers, :breweries, :pages, :discussions, :peoples, :roles,
:sessions, :help
:sessions, :help, :styles
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'

View File

@ -5,7 +5,9 @@ class CreateBeers < ActiveRecord::Migration
t.column :abv, :float
t.column :original_gravity, :float
t.column :final_gravity, :float
t.column :style_id, :integer
end
add_index :beers, :style_id
end
def self.down

View File

@ -4,7 +4,8 @@ base_actions = ApplicationController.action_methods
# i should probably figure out all of the children of ApplicationController
# rather than defining them here.
controllers = [ PagesController, HelpController, DiscussionsController,
PeoplesController, BeersController, BreweriesController, RolesController ]
PeoplesController, BeersController, BreweriesController, RolesController,
StylesController ]
controllers.each do |c|
actions = c.action_methods - base_actions
cname = c.controller_name
@ -22,6 +23,11 @@ Permission.find(:all,
next if [ 'new', 'create', 'edit', 'update', 'destroy' ].include?(p.action)
r.permissions << p
end
Permission.find(:all,
:conditions => [ 'controller = ?', 'styles' ]).each do |p|
next if [ 'new', 'create', 'edit', 'update', 'destroy' ].include?(p.action)
r.permissions << p
end
r2 = Role.admin_role
Permission.find(:all).each do |p|

View File

@ -0,0 +1,57 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'styles_controller'
# Re-raise errors caught by the controller.
class StylesController; def rescue_action(e) raise e end; end
class StylesControllerTest < Test::Unit::TestCase
fixtures :styles
def setup
@controller = StylesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_get_index
get :index
assert_response :success
assert assigns(:styles)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_style
old_count = Style.count
post :create, :style => { }
assert_equal old_count+1, Style.count
assert_redirected_to style_path(assigns(:style))
end
def test_should_show_style
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_style
put :update, :id => 1, :style => { }
assert_redirected_to style_path(assigns(:style))
end
def test_should_destroy_style
old_count = Style.count
delete :destroy, :id => 1
assert_equal old_count-1, Style.count
assert_redirected_to styles_path
end
end