diff options
author | Joachim Filip Ignacy Bartosik <jbartosik@gmail.com> | 2011-06-30 16:43:15 +0200 |
---|---|---|
committer | Joachim Filip Ignacy Bartosik <jbartosik@gmail.com> | 2011-07-06 16:49:52 +0200 |
commit | ef70c52fea7c44b08ee7bec9a1657403cf47d283 (patch) | |
tree | 1488cab5a968c9408e32a4e2346f2dbc5032ec65 /site/app/models/vote.rb | |
parent | Distinguish votes made during council voting and community voice (diff) | |
download | council-webapp-ef70c52fea7c44b08ee7bec9a1657403cf47d283.tar.gz council-webapp-ef70c52fea7c44b08ee7bec9a1657403cf47d283.tar.bz2 council-webapp-ef70c52fea7c44b08ee7bec9a1657403cf47d283.zip |
Show community votes properly
Diffstat (limited to 'site/app/models/vote.rb')
-rw-r--r-- | site/app/models/vote.rb | 21 |
1 files changed, 17 insertions, 4 deletions
diff --git a/site/app/models/vote.rb b/site/app/models/vote.rb index c9695e9..9307cce 100644 --- a/site/app/models/vote.rb +++ b/site/app/models/vote.rb @@ -38,15 +38,28 @@ class Vote < ActiveRecord::Base 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]) } + scope :for_item, lambda { |iid| joins(:voting_option).where([ + 'voting_options.agenda_item_id = ?', iid]) } + + def self.vote_for_option(user, option, council_vote) + item = option.agenda_item + old_vote = Vote.for_item(item.id).user_is(user.id).first + if old_vote.nil? + Vote.create! :voting_option => option, :user => user, :council_vote => council_vote + else + old_vote = Vote.find(old_vote) + old_vote.voting_option = option + old_vote.council_vote = council_vote + old_vote.save! + end + end + 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.user_for_item(user_id, voting_option.agenda_item_id) + other_votes = Vote.for_item(voting_option.agenda_item_id).user_id_is(user_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.') |