adding beer experiences

git-svn-id: http://svn.barleysodas.com/barleysodas/trunk@122 0f7b21a7-9e3a-4941-bbeb-ce5c7c368fa7
master
andrew 2008-02-03 00:57:35 +00:00
parent 3138566051
commit d613bf10cd
19 changed files with 269 additions and 10 deletions

View File

@ -9,9 +9,7 @@ class BeersController < ApplicationController
@secondary_title = 'Browsing all beers'
@pages, @beers = paginate :beers, :include => 'page', :per_page => per_page,
:order => 'beers.title ASC'
if @beers.empty?
flash.now[:notice] = 'There are no beers yet.'
end
flash.now[:notice] = 'There are no beers yet.' if @beers.empty?
@tags = Page.tags(:limit => 25, :order => "name DESC",
:owner_type => 'Beer')
respond_to do |format|
@ -23,6 +21,8 @@ class BeersController < ApplicationController
# GET /beers/1
# GET /beers/1.xml
def show
person = People.find(session[:people_id], :include => [ 'experiences' ])
@experience = person.experiences.detect { |e| e.beer_id == @beer.id }
respond_to do |format|
format.html # show.rhtml
format.xml { render :xml => @beer.to_xml }

View File

@ -0,0 +1,71 @@
class ExperiencesController < ApplicationController
append_before_filter :fetch_experience, :only => [ :edit, :update, :destroy ]
# GET /experiences
def index
redirect_to experience_url(:id => session[:people_title_for_url])
end
# GET /experiences/1
# GET /experiences/1.xml
def show
@people = People.find_by_title(Page.title_from_url(params[:id]))
cond_ary = [ 'experiences.people_id = :people_id' ]
cond_var = { :people_id => @people.id }
conditions = [ cond_ary.join(' AND '), cond_var ]
@total_count = Experience.count(conditions)
@pages, @experiences = paginate :experiences,
:include => [ 'beer' ], :order => [ 'beers.title ASC' ],
:per_page => per_page, :conditions => conditions
brewery_ids = @experiences.collect { |e| e.beer.brewery_id }
@breweries = Brewery.find(brewery_ids, :order => 'title ASC')
flash.now[:notice] = 'No experience yet.' if @experiences.empty?
respond_to do |format|
format.html # index.rhtml
format.xml { render :xml => @experiences.to_xml }
end
end
# GET /experiences/1;edit
def edit
end
# POST /experiences
def create
@experience = Experience.new(params[:experience])
@experience.people_id = session[:people_id]
if @experience.save
render :inline => "<%= remove_experience_link(@experience) -%>"
else
render :inline => "<%= error_messages_for(@experience) -%>",
:status => 500
end
end
# PUT /experiences/1
def update
if @experience.update_attributes(params[:experience])
render :inline => "<%= add_experience_link -%>"
else
render :inline => "<%= error_messages_for(@experience) -%>",
:status => 500
end
end
# DELETE /experiences/1
def destroy
# not sure if this is necessary, but i'll include it for completeness.
if @experience.destroy
render :inline => "<%= add_experience_link -%>"
else
render :nothing => true, :status => 500
end
end
protected
def fetch_experience
@experience = Experience.find(params[:id])
raise ActiveRecord::RecordNotFound.new unless @experience
end
end

View File

@ -12,4 +12,23 @@ module BeersHelper
link_to 'Edit Beer', edit_beer_path(beer.page.title_for_url),
{ :title => "Edit #{beer.title}" }
end
##
# Shows an add Experience link.
#
def add_experience_link
link_to_function("#{image_tag('list-add.png')} Beverage Experience",
"lightboxes['experienceDialog'].open()")
end
##
# Shows the remove Experience link.
#
def remove_experience_link(experience)
link_to_remote "#{image_tag('list-remove.png')} Beverage Experience",
:url => experience_url(:id => experience.id, :format => :xml),
:method => :delete, :confirm => 'Are you sure?',
:update => 'experience_container',
:success => "new Effect.Highlight('experience_container', {duration: 1.5})"
end
end

View File

@ -0,0 +1,8 @@
module ExperiencesHelper
include BeersHelper
include BreweriesHelper
def experience_rating(experience)
"<span id='rating_div_#{experience.id}' class='rating_container'></span>"
end
end

View File

@ -7,6 +7,8 @@ class Beer < ActiveRecord::Base
belongs_to :style
validates_presence_of :style_id
has_many_tagged_images
has_many :experiences, :dependent => :destroy
has_many :people, :through => :experiences
##
# Returns a list of attributes for the Page partial.

5
app/models/experience.rb Normal file
View File

@ -0,0 +1,5 @@
class Experience < ActiveRecord::Base
belongs_to :people
belongs_to :beer
validates_presence_of :people_id, :beer_id
end

View File

@ -12,6 +12,8 @@ class People < ActiveRecord::Base
validates_uniqueness_of :title
has_many :friends, :foreign_key => :source_id, :dependent => :destroy
has_many :actual_friends, :through => :friends, :source => :destination
has_many :experiences, :dependent => :destroy
has_many :beers, :through => :experiences
make_authenticatable
validates_length_of :password, :minimum => 8, :if => :password_required?,

View File

@ -1,10 +1,40 @@
<%= javascript_include_tag('control.rating.js') -%>
<%= tagged_image_browser(@beer) %>
<%= render :partial => 'pages/page' %>
<% content_for :script do -%>
after_opens['experienceDialog'] = function(){
var r = new Control.Rating('rating_div',
{
min: 1,
max: 10,
multiple: true,
input: 'experience_rating'
});
r.setValue(5);
}
<% end -%>
<% lightbox :title => 'Add a beverage experience', :window_id => 'experienceDialog' do -%>
<p>How do you rate this experience? (1 for bad, 10 for great)</p>
<%= hidden_field_tag 'experience_rating' %>
<%= hidden_field_tag 'beer_id', @beer.id %>
<span id="rating_div" class="rating_container"></span>
<br /><br />
<div class="dialogControls">
<%= link_to_remote 'Save', :url => experiences_path(:format => :xml),
:with => "'experience[rating]='+escape($('experience_rating').value)+'&experience[beer_id]='+escape($('beer_id').value)",
:success => "Control.Modal.close();new Effect.Highlight('experience_container', {duration: 1.5})",
:update => 'experience_container' -%> <%= link_to_function 'Cancel', 'Control.Modal.close()' -%>
</div>
<% end -%>
<% content_for :sidebar do -%>
<%= new_beer_link -%><br />
<%= edit_beer_link(@beer) -%><br />
<%= link_to 'Destroy', beer_path(@beer.page.title_for_url), :confirm => 'Are you sure?', :method => :delete %><br />
<% unless @beer.tagged_images.empty? -%><%= tagged_image_browser_link -%><br /><% end -%>
<% unless @beer.tagged_images.empty? -%><%= tagged_image_browser_link(@beer) -%><br /><% end %>
<span id="experience_container"><% if @experience -%><%= remove_experience_link(@experience) -%><% else -%><%= add_experience_link -%><% end %></span><br />
<strong><%= pluralize(@beer.people.size, 'people') -%></strong> have experienced this beer.<br />
<% end -%>

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 />
<% if logged_in? -%><%= link_to_unless_current 'My Beer Experiences', experience_path(:id => session[:people_title_for_url]) -%><br /><% end %>
<%= link_to_unless_current 'Beverage Styles', styles_path -%><br />
<%= link_to_unless_current 'Browse Breweries', breweries_path -%><br />
<%= link_to_unless_current 'BarleySodas People', peoples_path -%><br />

View File

@ -22,6 +22,6 @@
<% content_for 'sidebar' do -%>
<% if @page.allow_discussions? or !@page.discussions.empty? -%>
<%= link_to "Discuss (#{@page.discussions.size})", discussion_path(@page) -%><br />
<%= link_to "Discuss", discussion_path(@page) -%> (<%= @page.discussions.size -%>)<br />
<% end -%>
<% end -%>

View File

@ -9,5 +9,6 @@
<% unless @people.tagged_images.empty? -%><%= tagged_image_browser_link -%><br /><% end -%>
<%= show_friends_link(@people) -%><br />
<% unless @people.id == session[:people_id] or @people.friend_of?(session[:people_id]) -%><%= add_friend_link(@people) -%><% end -%>
<% if @people.friend_of?(session[:people_id]) -%><%= remove_friend_link(@people) -%><% end -%>
<% if @people.friend_of?(session[:people_id]) -%><%= remove_friend_link(@people) -%><br /><% end -%>
<%= link_to "#{pluralize(@people.beers.size, 'Beer')} Experience", experience_path(:id => @people.page.title_for_url) -%><br />
<% end -%>

View File

@ -1,9 +1,7 @@
ActionController::Routing::Routes.draw do |map|
map.resources :beers, :breweries, :pages, :discussions, :peoples, :roles,
:sessions, :styles, :galleries, :tag_images, :friends
:sessions, :styles, :galleries, :tag_images, :friends, :experiences
map.connect ':controller/:action/:id.:format'
map.connect ':controller/:action/:id'
map.connect '/', :controller => 'pages', :action => 'default_action'
end

View File

@ -0,0 +1,15 @@
class CreateExperiences < ActiveRecord::Migration
def self.up
create_table :experiences do |t|
t.column :people_id, :integer
t.column :beer_id, :integer
t.column :rating, :integer
end
add_index :experiences, :people_id
add_index :experiences, :beer_id
end
def self.down
drop_table :experiences
end
end

View File

@ -5,7 +5,8 @@ base_actions = ApplicationController.action_methods
# rather than defining them here.
controllers = [ PagesController, DiscussionsController, StylesController,
PeoplesController, BeersController, BreweriesController, RolesController,
GalleriesController, TagImagesController, FriendsController ]
GalleriesController, TagImagesController, FriendsController,
ExperiencesController ]
controllers.each do |c|
actions = c.action_methods - base_actions
cname = c.controller_name
@ -38,6 +39,11 @@ Permission.find(:all,
next if [ 'edit', 'create', 'destroy' ].include?(p.action)
r.permissions << p
end
Permission.find(:all,
:conditions => [ 'controller = ?', 'experiences' ]).each do |p|
next unless [ 'show' ].include?(p.action)
r.permissions << p
end
r2 = Role.admin_role
Permission.find(:all).each do |p|

View File

@ -7,6 +7,7 @@
@import 'content.css';
@import 'syntax.css';
@import 'lightboxes.css';
@import 'jsratings.css';
@media print { #sidebar { display: none; }
#content { float: none; width: 90%; }

View File

@ -0,0 +1,28 @@
.rating_container {
clear:both;
}
.rating_container a {
float:left;
display:block;
width:25px;
height:25px;
border:0;
background-image:url("/images/rating.gif");
}
.rating_container a.rating_off {
background-position:0 0px;
}
.rating_container a.rating_half {
background-position:0 -25px;
}
.rating_container a.rating_on {
background-position:0 -50px;
}
.rating_container a.rating_selected {
background-position:0 -75px;
}

5
test/fixtures/experiences.yml vendored Normal file
View File

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

View File

@ -0,0 +1,57 @@
require File.dirname(__FILE__) + '/../test_helper'
require 'experiences_controller'
# Re-raise errors caught by the controller.
class ExperiencesController; def rescue_action(e) raise e end; end
class ExperiencesControllerTest < Test::Unit::TestCase
fixtures :experiences
def setup
@controller = ExperiencesController.new
@request = ActionController::TestRequest.new
@response = ActionController::TestResponse.new
end
def test_should_get_index
get :index
assert_response :success
assert assigns(:experiences)
end
def test_should_get_new
get :new
assert_response :success
end
def test_should_create_experience
old_count = Experience.count
post :create, :experience => { }
assert_equal old_count+1, Experience.count
assert_redirected_to experience_path(assigns(:experience))
end
def test_should_show_experience
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_experience
put :update, :id => 1, :experience => { }
assert_redirected_to experience_path(assigns(:experience))
end
def test_should_destroy_experience
old_count = Experience.count
delete :destroy, :id => 1
assert_equal old_count-1, Experience.count
assert_redirected_to experiences_path
end
end

View File

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