Skip to content

Commit

Permalink
Add public changes to create and vote Poll:Questions with votation type
Browse files Browse the repository at this point in the history
  • Loading branch information
abelardogilm authored and javierm committed Jun 12, 2019
1 parent 23d3683 commit c6e4b24
Show file tree
Hide file tree
Showing 22 changed files with 713 additions and 137 deletions.
8 changes: 8 additions & 0 deletions app/assets/javascripts/sortable.js.coffee
Original file line number Diff line number Diff line change
Expand Up @@ -7,3 +7,11 @@ App.Sortable =
url: $(".sortable").data("js-url"),
data: { ordered_list: new_order },
type: "POST"

$(".sortable-priotirized-votation").sortable
update: (event, ui) ->
new_order = $(this).sortable("toArray", { attribute: "data-answer-id" })
$.ajax
url: $(this).data("js-url"),
data: { ordered_list: new_order },
type: "POST"
96 changes: 92 additions & 4 deletions app/assets/stylesheets/participation.scss
Original file line number Diff line number Diff line change
Expand Up @@ -1745,8 +1745,12 @@
background: #fafafa;
border-bottom: 1px solid #eee;

.column:nth-child(odd) {
border-right: 2px solid $text;
.margin-bottom {
margin-bottom: 0;
}

.orbit-bullets {
margin-bottom: 0;
}

.answer-divider {
Expand All @@ -1756,14 +1760,35 @@
padding-bottom: $line-height;
}

.answer-left-divider {
border-left: solid 1px $text;
padding-left: rem-calc(10);
}

.margin-top {
margin-top: rem-calc(10);
}

.margin-bottom {
margin-bottom: rem-calc(20);
}

.answer-description {
height: 100%;
max-height: rem-calc(1000);

&.short {
height: rem-calc(300);
max-height: rem-calc(70);
overflow: hidden;
max-width: rem-calc(700);
}
}

.question-divider {
border-bottom: rgba(219, 219, 219, 0.62) solid 1px;
margin-bottom: 1rem;
padding: rem-calc(24);
}

}

.orbit-bullets button {
Expand Down Expand Up @@ -1991,6 +2016,69 @@
}
}

.icon-like,
.icon-unlike {
background: #fff;
border: 2px solid $text-light;
border-radius: rem-calc(3);
color: $text-light;
display: inline-block;
font-size: rem-calc(30);
line-height: rem-calc(30);
padding: rem-calc(3) rem-calc(6);
position: relative;

&:hover,
&:active {
color: #fff;
cursor: pointer;
opacity: 1 !important;
}
}

.active-like {
color: #fff;
cursor: pointer;
opacity: 1 !important;
background: $like;
border: 2px solid $like;
}

.active-unlike {
color: #fff;
cursor: pointer;
opacity: 1 !important;
background: $unlike;
border: 2px solid $unlike;
}

.icon-like {

&:hover,
&:active,
.picked {
background: $like;
border: 2px solid $like;
}
}

.icon-unlike {

&:hover,
&:active {
background: $unlike;
border: 2px solid $unlike;
}
}

.vote-align {
float: right;
}

.vote-divider {
border-bottom: 1px solid $text-light;
}

// 09. Polls results and stats
// ---------------------------

Expand Down
70 changes: 70 additions & 0 deletions app/controllers/polls/answers_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
class Polls::AnswersController < ApplicationController

load_and_authorize_resource :poll
load_and_authorize_resource :question, class: "Poll::Question"
authorize_resource :answer, class: "Poll::Answer"

def create
@question = Poll::Question.find_by(id: params[:id])
if @question.votation_type.open? && !check_question_answer_exist
@question.question_answers.create(
title: params[:answer],
given_order: @question.question_answers.count + 1,
hidden: false
)
flash.now[:notice] = t("dashboard.polls.index.succesfull")
else
flash.now[:alert] = "Unfortunately failed to sent"
end
load_for_answers
if @question.enum_type&.include?("answer_couples")
last_pair ||= generate_and_store_new_pair(@question)
@last_pair_question_answers = {@question.id => last_pair}
end
render "polls/questions/answer", format: :js
end

def delete
@question = Poll::Question.find_by(id: params[:id])
!@question.answers.find_by(author: current_user, answer: params[:answer]).destroy
@question.question_answers.each do |question_answer|
question_answer.set_most_voted
end
question_answers
load_for_answers
if @question.enum_type&.include?("answer_couples")
last_pair ||= generate_and_store_new_pair(@question)
@last_pair_question_answers = {@question.id => last_pair}
end
render "polls/questions/answer", format: :js
end

private

def check_question_answer_exist
exist = false
@question.question_answers.each do |question_answer|
break if exist
exist = true if question_answer.title == params[:answer]
end
exist
end

def load_for_answers
@page = params[:page].present? ? params[:page] : 1
question_answers
@answers_by_question_id = {@question.id => @question.answers
.by_author(current_user)
.order(:order)
.pluck(:answer)}
end

def question_answers
if @question.is_positive_negative?
@answers = @question.question_answers.visibles.page(params[:page])
else
@answers = @question.question_answers.visibles
end
end

end
77 changes: 69 additions & 8 deletions app/controllers/polls/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,18 +3,79 @@ class Polls::QuestionsController < ApplicationController
load_and_authorize_resource :poll
load_and_authorize_resource :question, class: "Poll::Question"

has_orders %w{most_voted newest oldest}, only: :show
has_orders %w[most_voted newest oldest], only: :show

def answer
answer = @question.answers.find_or_initialize_by(author: current_user)
token = params[:token]
answer = store_answer
vote_stored(answer, params[:answer], params[:token]) if answer.present?
load_for_answers
if @question.enum_type&.include?("answer_couples")
last_pair ||= generate_and_store_new_pair(@question)
@last_pair_question_answers = {@question.id => last_pair}
end
end

answer.answer = params[:answer]
answer.touch if answer.persisted?
answer.save!
answer.record_voter_participation(token)
def load_answers
load_for_answers
render action: "answer.js.erb"
end

@answers_by_question_id = { @question.id => params[:answer] }
def prioritized_answers
unless params[:ordered_list].empty?
params[:ordered_list].each_with_index do |answer, i|
answer_obj = @question.votation_type.answer(current_user,
answer,
order: i + 1)
vote_stored(answer_obj, answer, params[:tooken]) if answer_obj.present?
end
@question.votation_type.update_priorized_values(current_user.id)
end
load_for_answers
render action: "answer.js.erb"
end

private

def load_for_answers
@page = params[:page].present? ? params[:page] : 1
question_answers
@answers_by_question_id = {@question.id => @question.answers
.by_author(current_user)
.order(:order)
.pluck(:answer)}
end

def vote_stored(answer, new_answer, token)
answer.answer = new_answer
answer.touch if answer.persisted?
answer.save!
answer.record_voter_participation(token)
@question.question_answers.visibles.where(question_id: @question).each do |question_answer|
question_answer.set_most_voted
end
end

def store_answer
if @question.votation_type.nil?
answer = @question.answers.find_or_initialize_by(author: current_user)
else
answer = @question.votation_type.answer(current_user,
params[:answer],
positive: params[:positive])
end
answer
end

def generate_and_store_new_pair(question)
Poll::PairAnswer.generate_pair(question, current_user)
end

def question_answers
if @question.is_positive_negative?
@answers = @question.question_answers.visibles.page(@page)
else
@answers = @question.question_answers.visibles
end
end

end
36 changes: 33 additions & 3 deletions app/controllers/polls_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,27 @@ def index
def show
@questions = @poll.questions.for_render.sort_for_list
@token = poll_voter_token(@poll, current_user)
@poll_questions_answers = Poll::Question::Answer.where(question: @poll.questions)
@poll_questions_answers = Poll::Question::Answer.visibles
.where(question: @poll.questions)
.where.not(description: "").order(:given_order)

@answers_by_question_id = {}
poll_answers = ::Poll::Answer.by_question(@poll.question_ids).by_author(current_user.try(:id))
poll_answers.each do |answer|
@answers_by_question_id[answer.question_id] = answer.answer

@last_pair_question_answers = {}
@questions.each do |question|
@answers_by_question_id[question.id] = question.answers.by_author(current_user).pluck(:answer)

if question.enum_type&.include?("answer_couples")
last_pair = question.pair_answers.by_author(current_user).first
last_pair ||= generate_and_store_new_pair(question)
@last_pair_question_answers[question.id] = last_pair
end

if question.enum_type&.include?("answer_set_closed") ||
question.enum_type&.include?("answer_set_open")
votation_answer_sets(question)
end
end

@commentable = @poll
Expand All @@ -42,6 +56,18 @@ def results

private

def votation_answer_sets(question)
if question.votation_type.votation_set_answers.by_author(current_user).empty?
question.question_answers&.sample(question.max_groups_answers).each do |question_answer|
answer = VotationSetAnswer.new(answer: question_answer.title,
votation_type: question.votation_type,
author: current_user)
question.votation_type.votation_set_answers << answer
end
!question.save
end
end

def load_poll
@poll = Poll.where(slug: params[:id]).first || Poll.where(id: params[:id]).first
end
Expand All @@ -50,4 +76,8 @@ def load_active_poll
@active_poll = ActivePoll.first
end

def generate_and_store_new_pair(question)
Poll::PairAnswer.generate_pair(question, current_user)
end

end
4 changes: 4 additions & 0 deletions app/helpers/polls_helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,8 @@ def info_menu?
def show_polls_description?
@active_poll.present? && @current_filter == "current"
end

def stored_positive_negative_value(question, answer)
question.answers.find_by(author_id: current_user.id, answer: answer.title).positive
end
end
10 changes: 8 additions & 2 deletions app/models/abilities/common.rb
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,18 @@ def initialize(user)

can :create, DirectMessage
can :show, DirectMessage, sender_id: user.id
can :answer, Poll do |poll|

can [:load_answers], Poll::Question
can [:answer], Poll do |poll|
poll.answerable_by?(user)
end
can :answer, Poll::Question do |question|
can [:answer, :prioritized_answers], Poll::Question do |question|
question.answerable_by?(user)
end

can [:create, :delete], Poll::Answer do |answer|
answer.question.answerable_by?(user)
end
end

can [:create, :show], ProposalNotification, proposal: { author_id: user.id }
Expand Down

0 comments on commit c6e4b24

Please sign in to comment.