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
281 additions
and
3 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
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
19 changes: 19 additions & 0 deletions
19
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
13 changes: 13 additions & 0 deletions
13
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,13 @@ | ||
class Polls::Questions::AnswersComponent < ApplicationComponent; end | ||
|
||
require_dependency Rails.root.join("app", "components", "polls", "questions", "answers_component").to_s | ||
|
||
|
||
class Polls::Questions::AnswersComponent < ApplicationComponent | ||
def open_answer | ||
if question.answers.nil? || question.answers.empty? | ||
return "" | ||
end | ||
question.answers.by_author(current_user).first.answer | ||
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,15 @@ | ||
require_dependency Rails.root.join("app", "models", "concerns", "questionable").to_s | ||
|
||
module Questionable | ||
alias_method :consul_find_by_attributes, :find_by_attributes | ||
|
||
private | ||
|
||
def find_by_attributes(user, title) | ||
if vote_type == "open" | ||
{ author: user } | ||
else | ||
consul_find_by_attributes(user, title) | ||
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
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
65 changes: 65 additions & 0 deletions
65
spec/components/custom/polls/questions/answers_component_spec.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,65 @@ | ||
require "rails_helper" | ||
|
||
describe Polls::Questions::AnswersComponent do | ||
include Rails.application.routes.url_helpers | ||
let(:poll) { create(:poll) } | ||
let(:question) { create(:poll_question_open, poll: poll) } | ||
let(:component) { Polls::Questions::AnswersComponent.new(question) } | ||
|
||
describe "open questions" do | ||
|
||
it "renders form to vote open question answers" do | ||
sign_in(create(:user, :verified)) | ||
|
||
I18n.with_locale(:es) { render_inline component } | ||
|
||
expect(page).to have_button "Enviar" | ||
expect(page).not_to have_button "Enviado" | ||
expect(page).to have_css "textarea[id='answer']" | ||
end | ||
|
||
it "renders custom button text when current user answers" do | ||
user = create(:user, :verified) | ||
create(:poll_answer, author: user, question: question, answer: "Yes") | ||
sign_in(user) | ||
|
||
I18n.with_locale(:es) { render_inline component } | ||
|
||
expect(page).not_to have_button "Enviar" | ||
expect(page).to have_button "Enviado" | ||
end | ||
|
||
it "when user is not signed in, renders submit button pointing to user sign in path" do | ||
I18n.with_locale(:es) { render_inline component } | ||
|
||
expect(page).to have_link "Enviar", href: new_user_session_path | ||
end | ||
|
||
it "when user is not verified, renders submit button pointing to user verification in path" do | ||
sign_in(create(:user)) | ||
|
||
I18n.with_locale(:es) { render_inline component } | ||
|
||
expect(page).to have_link "Enviar", href: verification_path | ||
end | ||
|
||
it "when user already voted in booth it renders disabled answers" do | ||
user = create(:user, :level_two) | ||
create(:poll_voter, :from_booth, poll: poll, user: user) | ||
sign_in(user) | ||
|
||
I18n.with_locale(:es) { render_inline component } | ||
|
||
expect(page).to have_css "textarea[disabled='disabled']" | ||
end | ||
|
||
it "user cannot vote when poll expired it renders disabled form" do | ||
question = create(:poll_question_open, poll: create(:poll, :expired)) | ||
sign_in(create(:user, :level_two)) | ||
|
||
I18n.with_locale(:es) { render_inline Polls::Questions::AnswersComponent.new(question) } | ||
|
||
expect(page).to have_css "textarea[disabled='disabled']" | ||
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
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,101 @@ | ||
require "rails_helper" | ||
|
||
describe Poll::Answer do | ||
describe "validations" do | ||
let(:answer) { build(:poll_open_answer) } | ||
|
||
it "is valid" do | ||
expect(answer).to be_valid | ||
end | ||
|
||
it "is not valid wihout a question" do | ||
answer.question = nil | ||
expect(answer).not_to be_valid | ||
end | ||
|
||
it "is not valid without an author" do | ||
answer.author = nil | ||
expect(answer).not_to be_valid | ||
end | ||
|
||
it "is not valid without an answer" do | ||
answer.answer = nil | ||
expect(answer).not_to be_valid | ||
end | ||
end | ||
|
||
describe "#save_and_record_voter_participation" do | ||
let(:author) { create(:user, :level_two) } | ||
let(:poll) { create(:poll) } | ||
let(:question) { create(:poll_question_open, poll: poll) } | ||
|
||
it "creates a poll_voter with user and poll data" do | ||
answer = create(:poll_open_answer, question: question, author: author, answer: "Yes") | ||
expect(answer.poll.voters).to be_blank | ||
|
||
answer.save_and_record_voter_participation | ||
expect(poll.reload.voters.size).to eq(1) | ||
voter = poll.voters.first | ||
|
||
expect(voter.document_number).to eq(answer.author.document_number) | ||
expect(voter.poll_id).to eq(answer.poll.id) | ||
expect(voter.officer_id).to be nil | ||
end | ||
|
||
it "updates a poll_voter with user and poll data" do | ||
answer = create(:poll_open_answer, question: question, author: author, answer: "Yes") | ||
answer.save_and_record_voter_participation | ||
|
||
expect(poll.reload.voters.size).to eq(1) | ||
|
||
# answer = create(:poll_open_answer, question: question, author: author, answer: "No") | ||
answer.answer = "no" | ||
answer.save_and_record_voter_participation | ||
|
||
expect(poll.reload.voters.size).to eq(1) | ||
|
||
voter = poll.voters.first | ||
expect(voter.document_number).to eq(answer.author.document_number) | ||
expect(voter.poll_id).to eq(answer.poll.id) | ||
end | ||
|
||
it "does not save the answer if the voter is invalid" do | ||
allow_any_instance_of(Poll::Voter).to receive(:valid?).and_return(false) | ||
answer = build(:poll_open_answer) | ||
|
||
expect do | ||
answer.save_and_record_voter_participation | ||
end.to raise_error(ActiveRecord::RecordInvalid) | ||
|
||
expect(answer).not_to be_persisted | ||
end | ||
end | ||
|
||
describe "#destroy_and_remove_voter_participation" do | ||
let(:poll) { create(:poll) } | ||
let(:question) { create(:poll_question_open, poll: poll) } | ||
|
||
it "destroys voter record and answer when it was the only user's answer" do | ||
answer = build(:poll_open_answer, question: question) | ||
answer.save_and_record_voter_participation | ||
|
||
expect { answer.destroy_and_remove_voter_participation } | ||
.to change { Poll::Answer.count }.by(-1) | ||
.and change { Poll::Voter.count }.by(-1) | ||
end | ||
|
||
it "destroys the answer but does not destroy the voter record when the user | ||
has answered other poll questions" do | ||
answer = build(:poll_open_answer, question: question) | ||
answer.save_and_record_voter_participation | ||
other_question = create(:poll_question_open, poll: poll) | ||
other_answer = build(:poll_open_answer, question: other_question, author: answer.author) | ||
other_answer.save_and_record_voter_participation | ||
|
||
expect(other_answer).to be_persisted | ||
expect { answer.destroy_and_remove_voter_participation } | ||
.to change { Poll::Answer.count }.by(-1) | ||
.and change { Poll::Voter.count }.by(0) | ||
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,33 @@ | ||
require "rails_helper" | ||
|
||
describe "Poll Votation Type" do | ||
let(:author) { create(:user, :level_two) } | ||
|
||
before do | ||
login_as(author) | ||
end | ||
|
||
scenario "Open answer" do | ||
question = create(:poll_question_open) | ||
|
||
visit poll_path(question.poll, locale: :es) | ||
|
||
expect(page).to have_content "Escribe aquí tu respuesta." | ||
expect(page).to have_content(question.title) | ||
expect(page).to have_button("Enviar") | ||
expect(page).not_to have_button("Enviado") | ||
|
||
within "#poll_question_#{question.id}_answers" do | ||
fill_in "answer", with: "Some text" | ||
click_button "Enviar" | ||
|
||
expect(page).to have_button("Enviado") | ||
expect(page).not_to have_button("Enviar") | ||
|
||
fill_in "answer", with: "Some another text" | ||
|
||
expect(page).to have_button("Enviar") | ||
expect(page).not_to have_button("Enviado") | ||
end | ||
end | ||
end |