diff options
-rw-r--r-- | app/models/widget_vote.rb | 1 | ||||
-rw-r--r-- | spec/models/widget_vote_spec.rb | 16 |
2 files changed, 17 insertions, 0 deletions
diff --git a/app/models/widget_vote.rb b/app/models/widget_vote.rb index 33697f0fb..021c38b30 100644 --- a/app/models/widget_vote.rb +++ b/app/models/widget_vote.rb @@ -15,4 +15,5 @@ class WidgetVote < ActiveRecord::Base attr_accessible :cookie validates :cookie, :length => { :is => 20 } + validates_uniqueness_of :cookie, :scope => :info_request_id end diff --git a/spec/models/widget_vote_spec.rb b/spec/models/widget_vote_spec.rb index f1e30ed22..db5e4385d 100644 --- a/spec/models/widget_vote_spec.rb +++ b/spec/models/widget_vote_spec.rb @@ -32,6 +32,22 @@ describe WidgetVote do widget_vote.should be_valid end + it 'enforces uniqueness of cookie per info request' do + info_request = FactoryGirl.create(:info_request) + widget_vote = info_request.widget_votes.create(:cookie => 'x' * 20) + duplicate_vote = info_request.widget_votes.build(:cookie => 'x' * 20) + duplicate_vote.should_not be_valid + duplicate_vote.errors[:cookie].should == ["has already been taken"] + end + + it 'allows the same cookie to be used across info requests' do + info_request = FactoryGirl.create(:info_request) + second_info_request = FactoryGirl.create(:info_request) + widget_vote = info_request.widget_votes.create(:cookie => 'x' * 20) + second_request_vote = second_info_request.widget_votes.build(:cookie => 'x' * 20) + second_request_vote.should be_valid + end + end end |