From 12fabe2fb9d706880598bc9f87deade186ef7f25 Mon Sep 17 00:00:00 2001 From: Jannik Streek Date: Tue, 25 Mar 2014 15:59:10 +0100 Subject: [PATCH] finalized voting design + some design fixes + improved code --- app/assets/images/icons/poll.png | Bin 187 -> 0 bytes app/assets/javascripts/app/views/poll.js | 47 +++++++++++++++--- .../javascripts/app/views/publisher_view.js | 4 +- app/assets/stylesheets/poll.css.scss | 10 ++-- .../stylesheets/publisher_blueprint.css.scss | 13 ++++- app/assets/templates/poll_tpl.jst.hbs | 35 +++++++++---- .../poll_participations_controller.rb | 10 ++-- app/models/poll.rb | 9 ++-- app/presenters/post_presenter.rb | 11 +++- .../publisher/_publisher_blueprint.html.haml | 10 ++-- config/locales/javascript/javascript.en.yml | 4 +- 11 files changed, 112 insertions(+), 41 deletions(-) delete mode 100644 app/assets/images/icons/poll.png diff --git a/app/assets/images/icons/poll.png b/app/assets/images/icons/poll.png deleted file mode 100644 index 026a12a2b28c952e0df0159fd73bbf66b8dbbb84..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 187 zcmeAS@N?(olHy`uVBq!ia0vp^qChOj!3HFa+MYK6DVB6cUq=Rp^(V|(yIunMk|nMY zCBgY=CFO}lsSJ)O`AMk?p1FzXsX?iUDV2pMQ*D5Xd_7$pLn>}1{rUgjo_Qa;gO}jPb&8J ix%|s8Yh`F)U}lher}N)Cdt)=uU -

{{t "poll.result" votes=poll.participation_count}}

+

{{t "poll.count" votes=poll.participation_count}}

{{poll.question}}
- {{#poll.poll_answers}} -
- -
+ {{#unless already_participated_in_poll}} +
+ {{#poll.poll_answers}} + + {{answer}} +
+ {{/poll.poll_answers}} + +
+

+
{{t "poll.toggle_result"}}
+

+ {{/unless}} +
+
{{t "poll.result"}}
+ {{#poll.poll_answers}} +
-
+
{{answer}} -
-
-
- {{/poll.poll_answers}} - +

+
+ {{/poll.poll_answers}} +
+ {{/if}} \ No newline at end of file diff --git a/app/controllers/poll_participations_controller.rb b/app/controllers/poll_participations_controller.rb index b2d435bb8..89f6854f5 100644 --- a/app/controllers/poll_participations_controller.rb +++ b/app/controllers/poll_participations_controller.rb @@ -3,15 +3,15 @@ class PollParticipationsController < ApplicationController before_filter :authenticate_user! def create - answer = PollAnswer.find(params[:poll_answer_id]) - poll_participation = current_user.participate_in_poll!(target, answer) if target rescue ActiveRecord::RecordInvalid - if poll_participation + begin + answer = PollAnswer.find(params[:poll_answer_id]) + poll_participation = current_user.participate_in_poll!(target, answer) if target respond_to do |format| format.html { redirect_to :back } format.mobile { redirect_to stream_path } - format.json { render :nothing => true, :status => 201 } + format.json { render json: poll_participation, :status => 201 } end - else + rescue ActiveRecord::RecordInvalid respond_to do |format| format.html { redirect_to :back } format.mobile { redirect_to stream_path } diff --git a/app/models/poll.rb b/app/models/poll.rb index 1450701b4..d43fcec77 100644 --- a/app/models/poll.rb +++ b/app/models/poll.rb @@ -13,8 +13,6 @@ class Poll < ActiveRecord::Base delegate :author, :author_id, :public?, :subscribers, to: :status_message validate :enough_poll_answers - - #TODO check if user has the right to vote self.include_root_in_json = false @@ -28,12 +26,15 @@ class Poll < ActiveRecord::Base :post_id => self.status_message.id, :question => self.question, :poll_answers => self.poll_answers, - :participation_count => self.participation_count - #TODO already participated? + :participation_count => self.participation_count, } end def participation_count poll_answers.sum("vote_count") end + + def already_participated?(user) + poll_participations.where(:author_id => user.person.id).present? + end end diff --git a/app/presenters/post_presenter.rb b/app/presenters/post_presenter.rb index 7e0d4789d..d797da86c 100644 --- a/app/presenters/post_presenter.rb +++ b/app/presenters/post_presenter.rb @@ -35,7 +35,8 @@ class PostPresenter :root => root, :title => title, :address => @post.address, - :poll => @post.poll, + :poll => @post.poll(), + :already_participated_in_poll => already_participated_in_poll, :interactions => { :likes => [user_like].compact, @@ -73,6 +74,14 @@ class PostPresenter @current_user.present? end + private + + def already_participated_in_poll + if @post.poll + @post.poll.already_participated?(current_user) + end + end + end class PostInteractionPresenter diff --git a/app/views/publisher/_publisher_blueprint.html.haml b/app/views/publisher/_publisher_blueprint.html.haml index daefa53c2..f12881770 100644 --- a/app/views/publisher/_publisher_blueprint.html.haml +++ b/app/views/publisher/_publisher_blueprint.html.haml @@ -28,8 +28,7 @@ %span#publisher-images %span.markdownIndications != t('shared.publisher.formatWithMarkdown', markdown_link: link_to(t('help.markdown'), 'https://diasporafoundation.org/formatting', target: :blank)) - #poll_creator.btn{:title => t('.create_poll')} - = image_tag 'icons/poll.png', :alt => t('.create_poll').titleize, :class => 'publisher_image' + %a{:id => "poll_creator", :href => "#", :class => "entypo bar-graph gray small publisher_image"} #locator.btn{:title => t('shared.publisher.get_location')} = image_tag 'icons/marker.png', :alt => t('shared.publisher.get_location').titleize, :class => 'publisher_image' #file-upload.btn{:title => t('shared.publisher.upload_photos')} @@ -39,12 +38,13 @@ %br #poll_creator_wrapper = t('shared.publisher.poll.add_a_poll') - %input{:id => 'poll_question', :placeholder => t('shared.publisher.poll.question'), :name => 'poll_question'} + #poll_question_wrapper + %input{:id => 'poll_question', :placeholder => t('shared.publisher.poll.question'), :name => 'poll_question'} .poll_answer %span{:class => 'poll_answer_input_wrapper'} %input{:class => 'poll_answer_input', :placeholder => t('shared.publisher.poll.option'), :name => 'poll_answers[]'} - %a{:class => 'remove_poll_answer'} - = t('shared.publisher.poll.remove_poll_answer') + %a{:class => 'remove_poll_answer', :title => t('shared.publisher.poll.remove_poll_answer')} + .icons-deletelabel #add_poll_answer_wrapper #add_poll_answer{:class => 'button creation'} = t('shared.publisher.poll.add_poll_answer') diff --git a/config/locales/javascript/javascript.en.yml b/config/locales/javascript/javascript.en.yml index ac6fd0bf8..4f6f09424 100644 --- a/config/locales/javascript/javascript.en.yml +++ b/config/locales/javascript/javascript.en.yml @@ -175,4 +175,6 @@ en: poll: vote: "Vote" - result: "<%=votes%> votes so far" + result: "Result" + count: "<%=votes%> votes so far" + toggle_result: "Toggle result" \ No newline at end of file