Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Can now vote and unvote for timeslots

  • Loading branch information...
commit 70bafbe41ca4548d17f7cafc698cf4e38a0d03a4 1 parent fc3f569
Joe Peck authored
View
3  app/assets/javascripts/timeslots.js.coffee
@@ -0,0 +1,3 @@
+# Place all the behaviors and hooks related to the matching controller here.
+# All this logic will automatically be available in application.js.
+# You can use CoffeeScript in this file: http://jashkenas.github.com/coffee-script/
View
3  app/assets/stylesheets/timeslots.css.scss
@@ -0,0 +1,3 @@
+// Place all the styles related to the timeslots controller here.
+// They will automatically be included in application.css.
+// You can use Sass (SCSS) here: http://sass-lang.com/
View
2  app/controllers/timeslots_controller.rb
@@ -0,0 +1,2 @@
+class TimeslotsController < ApplicationController
+end
View
79 app/controllers/votes_controller.rb
@@ -1,83 +1,20 @@
class VotesController < ApplicationController
- # GET /votes
- # GET /votes.json
- def index
- @votes = Vote.all
- respond_to do |format|
- format.html # index.html.erb
- format.json { render json: @votes }
- end
- end
-
- # GET /votes/1
- # GET /votes/1.json
- def show
- @vote = Vote.find(params[:id])
-
- respond_to do |format|
- format.html # show.html.erb
- format.json { render json: @vote }
- end
- end
-
- # GET /votes/new
- # GET /votes/new.json
- def new
- @vote = Vote.new
-
- respond_to do |format|
- format.html # new.html.erb
- format.json { render json: @vote }
- end
- end
-
- # GET /votes/1/edit
- def edit
- @vote = Vote.find(params[:id])
- end
-
- # POST /votes
- # POST /votes.json
def create
- @vote = Vote.new(params[:vote])
-
- respond_to do |format|
- if @vote.save
- format.html { redirect_to @vote, notice: 'Vote was successfully created.' }
- format.json { render json: @vote, status: :created, location: @vote }
- else
- format.html { render action: "new" }
- format.json { render json: @vote.errors, status: :unprocessable_entity }
- end
+ timeslot = Timeslot.find(params[:timeslot_id])
+ @vote = timeslot.votes.new(user: current_user, event: timeslot.event)
+
+ if @vote.save
+ redirect_to @vote.event, notice: 'Thanks for voting!'
+ else
+ redirect_to @vote.event, notice: 'Dang, your vote didn\'t work :('
end
end
- # PUT /votes/1
- # PUT /votes/1.json
- def update
- @vote = Vote.find(params[:id])
- respond_to do |format|
- if @vote.update_attributes(params[:vote])
- format.html { redirect_to @vote, notice: 'Vote was successfully updated.' }
- format.json { head :no_content }
- else
- format.html { render action: "edit" }
- format.json { render json: @vote.errors, status: :unprocessable_entity }
- end
- end
- end
-
- # DELETE /votes/1
- # DELETE /votes/1.json
def destroy
@vote = Vote.find(params[:id])
@vote.destroy
-
- respond_to do |format|
- format.html { redirect_to votes_url }
- format.json { head :no_content }
- end
+ redirect_to @vote.event
end
end
View
2  app/helpers/timeslots_helper.rb
@@ -0,0 +1,2 @@
+module TimeslotsHelper
+end
View
7 app/models/vote.rb
@@ -1,6 +1,11 @@
class Vote < ActiveRecord::Base
- attr_accessible :event_id, :timeslot_id, :user_id
+ attr_accessible :event_id, :timeslot_id, :user_id, :user, :timeslot, :event
+
belongs_to :user
belongs_to :event
belongs_to :timeslot
+
+ validates_presence_of :timeslot
+ validates_presence_of :user
+ validates_uniqueness_of :timeslot_id, scope: :user_id
end
View
17 app/views/events/show.html.erb
@@ -1,13 +1,28 @@
<h1><%= @event %></h1>
<h2><%= @event.starts_at %></h2>
+<% if current_user.blank? %>
+ <h3>Login to rock the vote!</h3>
+<% end %>
+
<% if @event.timeslots.empty? %>
No timeslots!
<% else %>
<ul class="timeslots">
<% @event.timeslots.each do |timeslot| %>
<%# TODO: Determine li class based on if current user has voted for that day %>
- <li><%= timeslot %></li>
+ <li>
+ <% if current_user %>
+ <% if vote = current_user.votes.find_by_timeslot_id(timeslot.id) %>
+ <%= timeslot %>
+ <%= link_to "[Unvote]", timeslot_vote_path(timeslot, vote), confirm: "You sure you want to unvote that?", method: 'delete' %>
+ <% else %>
+ <%= link_to timeslot, {controller: :votes, timeslot_id: timeslot.id}, method: 'post' %>
+ <% end %>
+ <% else %>
+ <%= timeslot %>
+ <% end %>
+ </li>
<% end %>
</ul>
<% end %>
View
4 config/routes.rb
@@ -12,7 +12,9 @@
controllers: {omniauth_callbacks: "omniauth_callbacks"}
resources :events
- resources :votes
+ resources :timeslots do
+ resources :votes
+ end
# The priority is based upon order of creation:
# first created -> highest priority.
View
5 spec/controllers/timeslots_controller_spec.rb
@@ -0,0 +1,5 @@
+require 'spec_helper'
+
+describe TimeslotsController do
+
+end
View
15 spec/helpers/timeslots_helper_spec.rb
@@ -0,0 +1,15 @@
+require 'spec_helper'
+
+# Specs in this file have access to a helper object that includes
+# the TimeslotsHelper. For example:
+#
+# describe TimeslotsHelper do
+# describe "string concat" do
+# it "concats two strings with spaces" do
+# helper.concat_strings("this","that").should == "this that"
+# end
+# end
+# end
+describe TimeslotsHelper do
+ pending "add some examples to (or delete) #{__FILE__}"
+end

0 comments on commit 70bafbe

Please sign in to comment.
Something went wrong with that request. Please try again.