diff --git a/app/controllers/beers_controller.rb b/app/controllers/beers_controller.rb index 2095adb..62823bb 100644 --- a/app/controllers/beers_controller.rb +++ b/app/controllers/beers_controller.rb @@ -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 } diff --git a/app/controllers/experiences_controller.rb b/app/controllers/experiences_controller.rb new file mode 100644 index 0000000..87fc9ee --- /dev/null +++ b/app/controllers/experiences_controller.rb @@ -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 diff --git a/app/helpers/beers_helper.rb b/app/helpers/beers_helper.rb index 102abfa..d96bedf 100644 --- a/app/helpers/beers_helper.rb +++ b/app/helpers/beers_helper.rb @@ -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 diff --git a/app/helpers/experiences_helper.rb b/app/helpers/experiences_helper.rb new file mode 100644 index 0000000..b083725 --- /dev/null +++ b/app/helpers/experiences_helper.rb @@ -0,0 +1,8 @@ +module ExperiencesHelper + include BeersHelper + include BreweriesHelper + + def experience_rating(experience) + "" + end +end diff --git a/app/models/beer.rb b/app/models/beer.rb index 8ae9f04..58e5820 100644 --- a/app/models/beer.rb +++ b/app/models/beer.rb @@ -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. diff --git a/app/models/experience.rb b/app/models/experience.rb new file mode 100644 index 0000000..3f83f7f --- /dev/null +++ b/app/models/experience.rb @@ -0,0 +1,5 @@ +class Experience < ActiveRecord::Base + belongs_to :people + belongs_to :beer + validates_presence_of :people_id, :beer_id +end diff --git a/app/models/people.rb b/app/models/people.rb index e3111b1..18b8541 100644 --- a/app/models/people.rb +++ b/app/models/people.rb @@ -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?, diff --git a/app/views/beers/show.rhtml b/app/views/beers/show.rhtml index e7d9e15..b0a525c 100644 --- a/app/views/beers/show.rhtml +++ b/app/views/beers/show.rhtml @@ -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 -%> +
How do you rate this experience? (1 for bad, 10 for great)
+ <%= hidden_field_tag 'experience_rating' %> + <%= hidden_field_tag 'beer_id', @beer.id %> + +