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 | |
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')
-rw-r--r-- | site/app/controllers/voting_options_controller.rb | 16 | ||||
-rw-r--r-- | site/app/models/agenda.rb | 12 | ||||
-rw-r--r-- | site/app/models/vote.rb | 21 | ||||
-rw-r--r-- | site/app/models/voting_option.rb | 10 | ||||
-rw-r--r-- | site/app/views/taglibs/cards.dryml | 11 |
5 files changed, 55 insertions, 15 deletions
diff --git a/site/app/controllers/voting_options_controller.rb b/site/app/controllers/voting_options_controller.rb index 951ddbb..14ec89f 100644 --- a/site/app/controllers/voting_options_controller.rb +++ b/site/app/controllers/voting_options_controller.rb @@ -3,4 +3,20 @@ class VotingOptionsController < ApplicationController hobo_model_controller auto_actions :all + + def community_vote + option = VotingOption.find(params[:id]) + unless option.nil? + if current_user.signed_up? + Vote.vote_for_option(current_user, option, false) + flash[:notice] = "You voted for #{option.description}" + else + flash[:notice] = "You must be logged in to vote" + end + redirect_to :controller => :agenda_items, :action => :show, :id => option.agenda_item_id + else + flash[:notice] = "No such voting option" + redirect_to :controller => :agendas, :action => :index + end + end end diff --git a/site/app/models/agenda.rb b/site/app/models/agenda.rb index ca40b57..1afed67 100644 --- a/site/app/models/agenda.rb +++ b/site/app/models/agenda.rb @@ -90,17 +90,7 @@ 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 - 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 + Vote.vote_for_option(user, option, true) end end end 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.') diff --git a/site/app/models/voting_option.rb b/site/app/models/voting_option.rb index b9c2226..9ece560 100644 --- a/site/app/models/voting_option.rb +++ b/site/app/models/voting_option.rb @@ -17,6 +17,16 @@ class VotingOption < ActiveRecord::Base description end + def community_votes + votes_for_this = votes.council_vote_is(false).count + votes_total = Vote.for_item(agenda_item.id).council_vote_is(false).count + + return "No community votes for this item yet." if votes_total.zero? + + votes_percentage = (100 * votes_for_this.to_f/votes_total).round + "#{votes_for_this} of #{votes_total} (#{votes_percentage}%) votes." + end + # --- Permissions --- # def create_permitted? diff --git a/site/app/views/taglibs/cards.dryml b/site/app/views/taglibs/cards.dryml index cb2a76d..7c2db42 100644 --- a/site/app/views/taglibs/cards.dryml +++ b/site/app/views/taglibs/cards.dryml @@ -6,3 +6,14 @@ </header:> </card> </def> + +<extend tag="card" for="VotingOption"> + <old-card merge> + <after-header:> + <body: param> + Community votes: <view:community_votes/> + <a href="&community_vote_path(this)" if="¤t_user.signed_up?">Vote for it</a> + </body:> + </after-header:> + </old-card> +</extend> |