diff --git a/app/controllers/twonks_controller.rb b/app/controllers/twonks_controller.rb index 2476434..5e32583 100644 --- a/app/controllers/twonks_controller.rb +++ b/app/controllers/twonks_controller.rb @@ -14,9 +14,9 @@ def new def create @twonk = current_user.nominations.new(params[:twonk]) - @twonk.votes_count = 1 + puts current_user if @twonk.save - @vote = @twonk.votes.create(:user => current_user, :comment => params[:vote][:comment]) + @vote = @twonk.votes.create(params[:vote].merge(:ip => current_user)) flash[:success] = "Twonk has been nominated!" redirect_to twonk_path(@twonk) else @@ -41,10 +41,14 @@ def update def show @twonk = Twonk.find(params[:id], :include => [:for_votes, :against_votes, :votes]) + rescue ActiveRecord::RecordNotFound + not_found end def destroy @twonk.destroy + flash[:success] = "#{@twonk} has been untwonked!" + redirect_to twonks_path end def check_for_ownership @@ -52,6 +56,13 @@ def check_for_ownership unless @twonk.nominated_by == current_user flash[:notice] = "That is not your twonk to change!" redirect_back_or_default(twonks_path) - end + end + rescue ActiveRecord::RecordNotFound + not_found + end + + def not_found + flash[:failure] = "The twonk you were looking for no longer exists, or never existed." + redirect_to twonks_path end end diff --git a/app/controllers/votes_controller.rb b/app/controllers/votes_controller.rb index 7280ec6..ef6d97f 100644 --- a/app/controllers/votes_controller.rb +++ b/app/controllers/votes_controller.rb @@ -44,7 +44,7 @@ def update private def check_for_ownership - unless @vote.user == current_user + unless @vote.ip == current_user flash[:failure] = "That vote does not belong to you, you twonk!" redirect_to twonk_path(@twonk) end diff --git a/app/models/ip.rb b/app/models/ip.rb new file mode 100644 index 0000000..d148523 --- /dev/null +++ b/app/models/ip.rb @@ -0,0 +1,3 @@ +class Ip < ActiveRecord::Base + has_many :nominations, :class_name => "Twonk", :foreign_key => "nominated_by_id" +end diff --git a/app/models/twonk.rb b/app/models/twonk.rb index f03ec1c..84d6e69 100644 --- a/app/models/twonk.rb +++ b/app/models/twonk.rb @@ -2,10 +2,13 @@ class Twonk < ActiveRecord::Base has_many :votes has_many :for_votes, :class_name => "Vote", :conditions => "positive = TRUE" has_many :against_votes, :class_name => "Vote", :conditions => "positive = FALSE" - has_many :voters, :through => :votes, :source => :user - belongs_to :nominated_by, :class_name => "User" + has_many :voters, :through => :votes, :source => :ip + belongs_to :nominated_by, :class_name => "Ip", :counter_cache => "nomination_count" attr_accessible :name, :location validates_presence_of :name, :location validates_uniqueness_of :name, :scope => :location + def to_s + name + end end diff --git a/app/models/vote.rb b/app/models/vote.rb index a7b4e2e..c13d50d 100644 --- a/app/models/vote.rb +++ b/app/models/vote.rb @@ -1,15 +1,9 @@ class Vote < ActiveRecord::Base belongs_to :twonk - belongs_to :user, :counter_cache => "nomination_count" -# acts_as_snook :author_field => :user, :body_field => :comment - + belongs_to :ip before_save :change_vote_count def change_vote_count (positive? ? twonk.increment!("votes_count") : twonk.decrement!("votes_count")) if positive_changed? || new_record? end - - def url - - end end diff --git a/app/views/layouts/application.html.erb b/app/views/layouts/application.html.erb index 4689318..e6a6d6c 100644 --- a/app/views/layouts/application.html.erb +++ b/app/views/layouts/application.html.erb @@ -5,15 +5,9 @@