aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--site/app/models/agenda.rb12
-rw-r--r--site/app/models/vote.rb16
-rw-r--r--site/db/schema.rb7
-rw-r--r--site/spec/models/agenda_spec.rb5
-rw-r--r--site/spec/models/vote_spec.rb9
5 files changed, 41 insertions, 8 deletions
diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb
index c15b12e..ca40b57 100644
--- a/site/app/models/agenda.rb
+++ b/site/app/models/agenda.rb
@@ -90,7 +90,17 @@ class Agenda < ActiveRecord::Base
for voter in votes.keys
option = VotingOption.first :conditions => { :agenda_item_id => item.id, :description => votes[voter] }
user = ::User.find_by_irc_nick voter
- Vote.create! :voting_option => option, :user => user
+ old_vote = Vote.user_for_item(user.id, item.id).first
+ if old_vote.nil?
+ Vote.create! :voting_option => option, :user => user, :council_vote => true
+ else
+ # Result of Vote.user_for_item is read only so reload it
+ # Reload method won't work so use find.
+ old_vote = Vote.find(old_vote)
+ old_vote.voting_option = option
+ old_vote.council_vote = true
+ old_vote.save!
+ end
end
end
end
diff --git a/site/app/models/vote.rb b/site/app/models/vote.rb
index 5480f1b..c9695e9 100644
--- a/site/app/models/vote.rb
+++ b/site/app/models/vote.rb
@@ -4,6 +4,7 @@ class Vote < ActiveRecord::Base
hobo_model # Don't put anything above this
fields do
+ council_vote :boolean, :null => false, :default => false
timestamps
end
@@ -19,24 +20,33 @@ class Vote < ActiveRecord::Base
# --- Permissions --- #
def create_permitted?
+ return false if council_vote
user_is?(acting_user)
end
multi_permission(:update, :destroy) do
- user_is?(acting_user) and not user_changed?
+ return false if user_changed?
+ return false if council_vote
+ user_is?(acting_user)
end
def view_permitted?(field)
true
end
+ def council_vote_edit_permitted?
+ false
+ end
+
+ named_scope :user_for_item, lambda { |uid, iid| joins(:voting_option).where([
+ 'voting_options.agenda_item_id = ? AND votes.user_id = ?',
+ iid, uid]) }
protected
def user_voted_only_once
return if user.nil?
return if voting_option.nil?
return if voting_option.agenda_item.nil?
- other_votes = Vote.joins(:voting_option).where(['voting_options.agenda_item_id = ? AND votes.user_id = ?',
- voting_option.agenda_item_id, user_id])
+ other_votes = Vote.user_for_item(user_id, voting_option.agenda_item_id)
other_votes = other_votes.id_is_not(id) unless new_record?
if other_votes.count > 0
errors.add(:user, 'User can vote only once per agenda item.')
diff --git a/site/db/schema.rb b/site/db/schema.rb
index 39dd08b..c39cb60 100644
--- a/site/db/schema.rb
+++ b/site/db/schema.rb
@@ -10,7 +10,7 @@
#
# It's strongly recommended to check this file into your version control system.
-ActiveRecord::Schema.define(:version => 20110627151021) do
+ActiveRecord::Schema.define(:version => 20110706144851) do
create_table "agenda_items", :force => true do |t|
t.string "title"
@@ -101,8 +101,9 @@ ActiveRecord::Schema.define(:version => 20110627151021) do
create_table "votes", :force => true do |t|
t.datetime "created_at"
t.datetime "updated_at"
- t.integer "voting_option_id", :null => false
- t.integer "user_id", :null => false
+ t.integer "voting_option_id", :null => false
+ t.integer "user_id", :null => false
+ t.boolean "council_vote", :default => false, :null => false
end
add_index "votes", ["user_id"], :name => "index_votes_on_user_id"
diff --git a/site/spec/models/agenda_spec.rb b/site/spec/models/agenda_spec.rb
index f1de822..f0dc747 100644
--- a/site/spec/models/agenda_spec.rb
+++ b/site/spec/models/agenda_spec.rb
@@ -124,7 +124,9 @@ describe Agenda do
end
u = users_factory(:council, :council, :council)
- Vote.count.should be_zero
+ Factory(:vote, :user => u.first, :voting_option => a1.voting_options.first)
+
+ Vote.count.should be_equal(1)
results_hash = {
a1.title => { u[0].irc_nick => 'Yes', u[1].irc_nick => 'Yes', u[2].irc_nick => 'Yes'},
@@ -135,6 +137,7 @@ describe Agenda do
Agenda.process_results results_hash
Vote.count.should be_equal(9)
+ Vote.council_vote_is(true).count.should be_equal(9)
u[0].votes.*.voting_option.*.description.sort.should == ['Dunno', 'Yes', 'Yes']
u[1].votes.*.voting_option.*.description.sort.should == ['Dunno', 'No', 'Yes']
diff --git a/site/spec/models/vote_spec.rb b/site/spec/models/vote_spec.rb
index aa9f2e8..f2d529e 100644
--- a/site/spec/models/vote_spec.rb
+++ b/site/spec/models/vote_spec.rb
@@ -37,4 +37,13 @@ describe Vote do
o = Factory(:voting_option, :agenda_item => v.voting_option.agenda_item, :description => 'other option')
Vote.new(:user => v.user, :voting_option => o).should_not be_valid
end
+
+ it 'should prevent users from setting council_vote to true' do
+ for u in users_factory(AllRoles - [:guest])
+ v = Factory(:vote, :user => u, :council_vote => true)
+ v.should_not be_editable_by(u)
+ v.should_not be_updatable_by(u)
+ v.should_not be_destroyable_by(u)
+ end
+ end
end