forked from consuldemocracy/consuldemocracy
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
11 changed files
with
231 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
(function () { | ||
"use strict"; | ||
App.Polls = { | ||
text_input_listener: function () { | ||
$(".text-input-form textarea").on("input", function (event) { | ||
let submitButton = $(event.target).next(); | ||
if (submitButton.hasClass("answered")) { | ||
submitButton.removeClass("answered"); | ||
submitButton.addClass("secondary hollow"); | ||
submitButton.val("Enviar"); | ||
} | ||
}); | ||
}, | ||
initialize: function () { | ||
$(".zoom-link").on("click", function (event) { | ||
var answer; | ||
answer = $(event.target).closest("div.answer"); | ||
|
||
if ($(answer).hasClass("medium-6")) { | ||
$(answer).removeClass("medium-6"); | ||
$(answer).addClass("answer-divider"); | ||
if (!$(answer).hasClass("first")) { | ||
$(answer).insertBefore($(answer).prev("div.answer")); | ||
} | ||
} else { | ||
$(answer).addClass("medium-6"); | ||
$(answer).removeClass("answer-divider"); | ||
if (!$(answer).hasClass("first")) { | ||
$(answer).insertAfter($(answer).next("div.answer")); | ||
} | ||
} | ||
}); | ||
App.Polls.text_input_listener(); | ||
}, | ||
}; | ||
}).call(this); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
57 changes: 57 additions & 0 deletions
57
app/components/custom/polls/questions/answers_component.html.erb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,57 @@ | ||
<div class="poll-question-answers"> | ||
<% if question.vote_type == "open" %> | ||
<% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> | ||
<%= form_tag answer_question_path(question), remote: true, :"data-turbolinks"=>"true", id: "question-#{question.id}", class: "text-input-form" do %> | ||
<%= text_area_tag :answer, open_answer, rows: 4, required: true %> | ||
<% if open_answer.empty? %> | ||
<%= submit_tag("Enviar", :class => "button secondary hollow") %> | ||
<% else %> | ||
<%= submit_tag("Enviado", :class => "button answered") %> | ||
<% end %> | ||
<% end %> | ||
<% elsif !user_signed_in? %> | ||
<%= form_tag answer_question_path(question), remote: true, class: "text-input-form" do %> | ||
<%= text_area_tag :answer, nil, rows: 4, required: true, disabled: true %> | ||
<%= submit_tag("Enviar", :class => "button secondary hollow") %> | ||
<% end %> | ||
<% else %> | ||
<%= text_area_tag :answer, open_answer, rows: 4, required: true, disabled: true %> | ||
<% end %> | ||
<% end %> | ||
|
||
<% if can?(:answer, question) && !question.poll.voted_in_booth?(current_user) %> | ||
<% question_answers.each do |question_answer| %> | ||
<% if already_answered?(question_answer) %> | ||
<%= button_to question_answer_path(question, user_answer(question_answer)), | ||
method: :delete, | ||
remote: true, | ||
title: t("poll_questions.show.voted", answer: question_answer.title), | ||
class: "button answered", | ||
"aria-pressed": true do %> | ||
<%= question_answer.title %> | ||
<% end %> | ||
<% else %> | ||
<%= button_to answer_question_path(question, answer: question_answer.title), | ||
remote: true, | ||
title: t("poll_questions.show.vote_answer", answer: question_answer.title), | ||
class: "button secondary hollow", | ||
"aria-pressed": false, | ||
disabled: disable_answer?(question_answer) do %> | ||
<%= question_answer.title %> | ||
<% end %> | ||
<% end %> | ||
<% end %> | ||
<% elsif !user_signed_in? %> | ||
<% question_answers.each do |question_answer| %> | ||
<%= link_to question_answer.title, new_user_session_path, class: "button secondary hollow" %> | ||
<% end %> | ||
<% elsif !current_user.level_two_or_three_verified? %> | ||
<% question_answers.each do |question_answer| %> | ||
<%= link_to question_answer.title, verification_path, class: "button secondary hollow" %> | ||
<% end %> | ||
<% else %> | ||
<% question_answers.each do |question_answer| %> | ||
<span class="button secondary hollow disabled"><%= question_answer.title %></span> | ||
<% end %> | ||
<% end %> | ||
</div> |
37 changes: 37 additions & 0 deletions
37
app/components/custom/polls/questions/answers_component.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
class Polls::Questions::AnswersComponent < ApplicationComponent | ||
attr_reader :question | ||
delegate :can?, :current_user, :user_signed_in?, to: :helpers | ||
|
||
def initialize(question) | ||
@question = question | ||
end | ||
|
||
def already_answered?(question_answer) | ||
user_answer(question_answer).present? | ||
end | ||
|
||
def question_answers | ||
question.question_answers | ||
end | ||
|
||
def open_answer | ||
if question.answers.nil? || question.answers.empty? | ||
return '' | ||
end | ||
question.answers.by_author(current_user).first.answer | ||
end | ||
|
||
def user_answer(question_answer) | ||
user_answers.find_by(answer: question_answer.title) | ||
end | ||
|
||
def disable_answer?(question_answer) | ||
question.multiple? && user_answers.count == question.max_votes | ||
end | ||
|
||
private | ||
|
||
def user_answers | ||
@user_answers ||= question.answers.by_author(current_user) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
require_dependency Rails.root.join("app", "models", "concerns", "questionable").to_s | ||
|
||
module Questionable | ||
private | ||
|
||
def find_by_attributes(user, title) | ||
case vote_type | ||
when "unique", nil | ||
{ author: user } | ||
when "multiple" | ||
{ author: user, answer: title } | ||
when "open" | ||
{ author: user } | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
class Poll::Answer < ApplicationRecord | ||
belongs_to :question, -> { with_hidden }, inverse_of: :answers | ||
belongs_to :author, -> { with_hidden }, class_name: "User", inverse_of: :poll_answers | ||
|
||
delegate :poll, :poll_id, to: :question | ||
|
||
validates :question, presence: true | ||
validates :author, presence: true | ||
validates :answer, presence: true | ||
validate :max_votes | ||
|
||
validate :answer_inclusion_if_not_open | ||
|
||
scope :by_author, ->(author_id) { where(author_id: author_id) } | ||
scope :by_question, ->(question_id) { where(question_id: question_id) } | ||
|
||
def answer_inclusion_if_not_open | ||
if question.present? && question.vote_type != 'open' | ||
unless question.possible_answers.include?(answer) | ||
errors.add(:answer, 'is not included in the list of possible answers') | ||
end | ||
end | ||
end | ||
|
||
def save_and_record_voter_participation | ||
transaction do | ||
touch if persisted? | ||
save! | ||
Poll::Voter.find_or_create_by!(user: author, poll: poll, origin: "web") | ||
end | ||
end | ||
|
||
def destroy_and_remove_voter_participation | ||
transaction do | ||
destroy! | ||
|
||
if author.poll_answers.where(question_id: poll.question_ids).none? | ||
Poll::Voter.find_by(user: author, poll: poll, origin: "web").destroy! | ||
end | ||
end | ||
end | ||
|
||
private | ||
|
||
def max_votes | ||
return if !question || question&.unique? || question.vote_type == 'open' || persisted? | ||
|
||
author.lock! | ||
|
||
if question.answers.by_author(author).count >= question.max_votes | ||
errors.add(:answer, "Maximum number of votes per user exceeded") | ||
end | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
class VotationType < ApplicationRecord | ||
belongs_to :questionable, polymorphic: true | ||
|
||
QUESTIONABLE_TYPES = %w[Poll::Question].freeze | ||
|
||
enum vote_type: %w[unique multiple open] | ||
|
||
validates :questionable, presence: true | ||
validates :questionable_type, inclusion: { in: ->(*) { QUESTIONABLE_TYPES }} | ||
validates :max_votes, presence: true, if: :max_votes_required? | ||
|
||
private | ||
|
||
def max_votes_required? | ||
multiple? | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters