/
votes_controller.rb
63 lines (56 loc) · 1.72 KB
/
votes_controller.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
class VotesController < ApplicationController
before_filter :login_required
before_filter :find_twonk
before_filter :check_for_vote, :only => [:new, :create]
def new
@vote = @twonk.votes.build(:user_id => session[:user])
@vote.positive = params[:positive] == "true"
end
def create
@vote = @twonk.votes.build(params[:vote].merge!(:user => current_user))
if @vote.save
if @vote.positive?
flash[:success] = "You think #{@twonk.name} is really a twonk."
else
flash[:success] = "You don't think #{@twonk.name} is really a twonk."
end
redirect_to twonk_path(@twonk)
else
flash[:failure] = "Your vote failed. Try voting again!"
render :action => "new"
end
end
def edit
@vote = Vote.find(params[:id])
check_for_ownership
end
def update
@vote = Vote.find(params[:id])
check_for_ownership
@vote.positive = params[:vote][:positive] == "true"
params[:vote].delete(:positive)
if @vote.update_attributes(params[:vote])
flash[:success] = "Your vote has been changed!"
redirect_to twonk_path(@twonk)
else
flash[:failure] = "Your vote could not be changed."
render :action => "edit"
end
end
private
def check_for_ownership
unless @vote.ip == current_user
flash[:failure] = "That vote does not belong to you, you twonk!"
redirect_to twonk_path(@twonk)
end
end
def check_for_vote
if @twonk.voters.include?(current_user)
flash[:notice] = "You have already voted for #{@twonk.name}, you twonk!"
redirect_to twonk_path(@twonk)
end
end
def find_twonk
@twonk = Twonk.find(params[:twonk_id]) unless params[:twonk_id].nil?
end
end