From f05e5291b2830aa43eabe6fad3493b3c98704029 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:21:40 +0200 Subject: [PATCH 01/12] Replace valid_answers usage for question_answers on factories --- spec/factories.rb | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index f7b10bba1..d2bce146f 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -576,14 +576,14 @@ FactoryGirl.define do factory :poll_answer, class: 'Poll::Answer' do association :question, factory: :poll_question association :author, factory: [:user, :level_two] - answer { question.valid_answers.sample } + answer { question.question_answers.sample.title } end factory :poll_partial_result, class: 'Poll::PartialResult' do association :question, factory: :poll_question association :author, factory: :user origin { 'web' } - answer { question.valid_answers.sample } + answer { question.question_answers.sample.title } end factory :poll_recount, class: 'Poll::Recount' do From 9e22a1812f3d9ffa8f8a44ce3ca09b0988c428c8 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:22:15 +0200 Subject: [PATCH 02/12] Remove no longer needed valid_answers attribute from poll_question factory --- spec/factories.rb | 1 - 1 file changed, 1 deletion(-) diff --git a/spec/factories.rb b/spec/factories.rb index d2bce146f..3164599a5 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -503,7 +503,6 @@ FactoryGirl.define do poll association :author, factory: :user sequence(:title) { |n| "Question title #{n}" } - valid_answers { Faker::Lorem.words(3).join(', ') } end factory :poll_question_answer, class: 'Poll::Question::Answer' do From 60f2815010b0dd988b372534032e41ba394170b8 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:27:39 +0200 Subject: [PATCH 03/12] Add missing total_results check on poll result specs --- spec/features/admin/poll/polls_spec.rb | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index a9a33447f..829c28461 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -279,7 +279,8 @@ feature 'Admin polls' do create(:poll_recount, booth_assignment: booth_assignment_1, white_amount: 21, - null_amount: 44) + null_amount: 44, + total_amount: 66) visit admin_poll_path(poll) @@ -303,6 +304,7 @@ feature 'Admin polls' do within('#white_results') { expect(page).to have_content('21') } within('#null_results') { expect(page).to have_content('44') } + within('#total_results') { expect(page).to have_content('66') } end end end From 39f23d3b7bacffc8fb087c32179bbfb0dbd62c1d Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:28:38 +0200 Subject: [PATCH 04/12] Switch from valid_answers to question_answers on specs --- spec/features/admin/poll/polls_spec.rb | 17 +++++++++++------ spec/features/officing/results_spec.rb | 19 ++++++++++++------- spec/models/poll/answer_spec.rb | 11 +++++++---- spec/models/poll/partial_result_spec.rb | 14 +++++++++----- 4 files changed, 39 insertions(+), 22 deletions(-) diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index 829c28461..9c2d3fcf1 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -261,8 +261,13 @@ feature 'Admin polls' do booth_assignment_2 = create(:poll_booth_assignment, poll: poll) booth_assignment_3 = create(:poll_booth_assignment, poll: poll) - question_1 = create(:poll_question, poll: poll, valid_answers: "Yes,No") - question_2 = create(:poll_question, poll: poll, valid_answers: "Today,Tomorrow") + question_1 = create(:poll_question, poll: poll) + create(:poll_question_answer, title: 'Yes', question: question_1) + create(:poll_question_answer, title: 'No', question: question_1) + + question_2 = create(:poll_question, poll: poll) + create(:poll_question_answer, title: 'Today', question: question_2) + create(:poll_question_answer, title: 'Tomorrow', question: question_2) [booth_assignment_1, booth_assignment_2, booth_assignment_3].each do |ba| create(:poll_partial_result, @@ -287,17 +292,17 @@ feature 'Admin polls' do click_link "Results" expect(page).to have_content(question_1.title) - question_1.valid_answers.each_with_index do |answer, i| + question_1.question_answers.each_with_index do |answer, i| within("#question_#{question_1.id}_#{i}_result") do - expect(page).to have_content(answer) + expect(page).to have_content(answer.title) expect(page).to have_content([33, 0][i]) end end expect(page).to have_content(question_2.title) - question_2.valid_answers.each_with_index do |answer, i| + question_2.question_answers.each_with_index do |answer, i| within("#question_#{question_2.id}_#{i}_result") do - expect(page).to have_content(answer) + expect(page).to have_content(answer.title) expect(page).to have_content([0, 15][i]) end end diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index f7e1ad8ef..21cad8b77 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -7,8 +7,13 @@ feature 'Officing Results' do @officer_assignment = create(:poll_officer_assignment, :final, officer: @poll_officer) @poll = @officer_assignment.booth_assignment.poll @poll.update(ends_at: 1.day.ago) - @question_1 = create(:poll_question, poll: @poll, valid_answers: "Yes,No") - @question_2 = create(:poll_question, poll: @poll, valid_answers: "Today,Tomorrow") + @question_1 = create(:poll_question, poll: @poll) + create(:poll_question_answer, title: 'Yes', question: @question_1) + create(:poll_question_answer, title: 'No', question: @question_1) + @question_2 = create(:poll_question, poll: @poll) + create(:poll_question_answer, title: 'Today', question: @question_2) + create(:poll_question_answer, title: 'Tomorrow', question: @question_2) + login_as(@poll_officer.user) end @@ -83,7 +88,7 @@ feature 'Officing Results' do booth_assignment: @officer_assignment.booth_assignment, date: @poll.starts_at, question: @question_1, - answer: @question_1.valid_answers[0], + answer: @question_1.question_answers.first.title, author: @poll_officer.user, amount: 7777) @@ -143,13 +148,13 @@ feature 'Officing Results' do expect(page).to have_content(@officer_assignment.booth_assignment.booth.name) expect(page).to have_content(@question_1.title) - @question_1.valid_answers.each_with_index do |answer, i| - within("#question_#{@question_1.id}_#{i}_result") { expect(page).to have_content(answer) } + @question_1.question_answers.each_with_index do |answer, i| + within("#question_#{@question_1.id}_#{i}_result") { expect(page).to have_content(answer.title) } end expect(page).to have_content(@question_2.title) - @question_2.valid_answers.each_with_index do |answer, i| - within("#question_#{@question_2.id}_#{i}_result") { expect(page).to have_content(answer) } + @question_2.question_answers.each_with_index do |answer, i| + within("#question_#{@question_2.id}_#{i}_result") { expect(page).to have_content(answer.title) } end within('#white_results') { expect(page).to have_content('21') } diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index 8731445cc..fecf2f2d5 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -25,9 +25,12 @@ describe Poll::Answer do expect(answer).to_not be_valid end - it "should be valid for answers included in the Poll::Question's list" do - skip "review when removing valid_answers" - question = create(:poll_question, valid_answers: 'One, Two, Three') + it "should be valid for answers included in the Poll::Question's question_answers list" do + question = create(:poll_question) + create(:poll_question_answer, title: 'One', question: question) + create(:poll_question_answer, title: 'Two', question: question) + create(:poll_question_answer, title: 'Three', question: question) + expect(build(:poll_answer, question: question, answer: 'One')).to be_valid expect(build(:poll_answer, question: question, answer: 'Two')).to be_valid expect(build(:poll_answer, question: question, answer: 'Three')).to be_valid @@ -40,7 +43,7 @@ describe Poll::Answer do let(:author) { create(:user, :level_two) } let(:poll) { create(:poll) } - let(:question) { create(:poll_question, poll: poll, valid_answers: "Yes, No") } + let(:question) { create(:poll_question, poll: poll) } it "creates a poll_voter with user and poll data" do answer = create(:poll_answer, question: question, author: author, answer: "Yes") diff --git a/spec/models/poll/partial_result_spec.rb b/spec/models/poll/partial_result_spec.rb index 07e6a7355..316b17524 100644 --- a/spec/models/poll/partial_result_spec.rb +++ b/spec/models/poll/partial_result_spec.rb @@ -4,12 +4,16 @@ describe Poll::PartialResult do describe "validations" do it "validates that the answers are included in the Poll::Question's list" do - q = create(:poll_question, valid_answers: 'One, Two, Three') - expect(build(:poll_partial_result, question: q, answer: 'One')).to be_valid - expect(build(:poll_partial_result, question: q, answer: 'Two')).to be_valid - expect(build(:poll_partial_result, question: q, answer: 'Three')).to be_valid + question = create(:poll_question) + create(:poll_question_answer, title: 'One', question: question) + create(:poll_question_answer, title: 'Two', question: question) + create(:poll_question_answer, title: 'Three', question: question) - expect(build(:poll_partial_result, question: q, answer: 'Four')).to_not be_valid + expect(build(:poll_partial_result, question: question, answer: 'One')).to be_valid + expect(build(:poll_partial_result, question: question, answer: 'Two')).to be_valid + expect(build(:poll_partial_result, question: question, answer: 'Three')).to be_valid + + expect(build(:poll_partial_result, question: question, answer: 'Four')).to_not be_valid end end From fbcf8678c3c412c6d9eee22440d893142ec16b3c Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:28:59 +0200 Subject: [PATCH 05/12] Remove no longer needed question model tests for valid_answers --- spec/models/poll/question_spec.rb | 8 -------- 1 file changed, 8 deletions(-) diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb index 60f9d6716..8c8a94d35 100644 --- a/spec/models/poll/question_spec.rb +++ b/spec/models/poll/question_spec.rb @@ -3,13 +3,6 @@ require 'rails_helper' RSpec.describe Poll::Question, type: :model do let(:poll_question) { build(:poll_question) } - describe "#valid_answers" do - it "gets a comma-separated string, but returns an array" do - poll_question.valid_answers = "Yes, No" - expect(poll_question.valid_answers).to eq(["Yes", "No"]) - end - end - describe "#poll_question_id" do it "should be invalid if a poll is not selected" do poll_question.poll_id = nil @@ -27,7 +20,6 @@ RSpec.describe Poll::Question, type: :model do create_list(:geozone, 3) p = create(:proposal) poll_question.copy_attributes_from_proposal(p) - expect(poll_question.valid_answers).to eq(['Yes', 'No']) expect(poll_question.author).to eq(p.author) expect(poll_question.author_visible_name).to eq(p.author.name) expect(poll_question.proposal_id).to eq(p.id) From c6d1f727af6597781842a14a9368977bbc63ca26 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:29:40 +0200 Subject: [PATCH 06/12] Correct dev_seeds to correctly create Poll::Question::Answers instead of valid_answers, plus style fixes --- db/dev_seeds.rb | 27 +++++++++++++++------------ 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/db/dev_seeds.rb b/db/dev_seeds.rb index 6b80a0ec5..18bfe0dcb 100644 --- a/db/dev_seeds.rb +++ b/db/dev_seeds.rb @@ -73,7 +73,7 @@ def create_user(email, username = Faker::Name.name) confirmed_at: Time.current, terms_of_service: "1", gender: ['Male', 'Female'].sample, - date_of_birth: rand((Time.current - 80.years) .. (Time.current - 16.years)), + date_of_birth: rand((Time.current - 80.years)..(Time.current - 16.years)), public_activity: (rand(1..100) > 30) ) end @@ -127,7 +127,7 @@ end user = create_user("user#{i}@consul.dev") level = [1, 2, 3].sample if level >= 2 - user.update(residence_verified_at: Time.current, confirmed_phone: Faker::PhoneNumber.phone_number, document_number: Faker::Number.number(10), document_type: "1", geozone: Geozone.reorder("RANDOM()").first) + user.update(residence_verified_at: Time.current, confirmed_phone: Faker::PhoneNumber.phone_number, document_number: Faker::Number.number(10), document_type: "1", geozone: Geozone.reorder("RANDOM()").first) end if level == 3 user.update(verified_at: Time.current, document_number: Faker::Number.number(10)) @@ -319,7 +319,7 @@ end end 100.times do - voter = not_org_users.level_two_or_three_verified.reorder("RANDOM()").first + voter = not_org_users.level_two_or_three_verified.reorder("RANDOM()").first proposal = Proposal.reorder("RANDOM()").first proposal.vote_by(voter: voter, vote: true) end @@ -504,7 +504,7 @@ Proposal.last(3).each do |proposal| "banner-img banner-img-three"].sample, target_url: Rails.application.routes.url_helpers.proposal_path(proposal), post_started_at: rand((Time.current - 1.week)..(Time.current - 1.day)), - post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)), + post_ended_at: rand((Time.current - 1.day)..(Time.current + 1.week)), created_at: rand((Time.current - 1.week)..Time.current)) end @@ -561,13 +561,11 @@ print "Creating Poll Questions" author = User.reorder("RANDOM()").first description = "

#{Faker::Lorem.paragraphs.join('

')}

" open_at = rand(2.months.ago..2.months.from_now) - answers = Faker::Lorem.words((2..4).to_a.sample).map { |answer| answer.capitalize } question = Poll::Question.create!(author: author, title: Faker::Lorem.sentence(3).truncate(60), - valid_answers: answers.join(', '), poll: poll) - answers.each do |answer| - Poll::Question::Answer.create!(question: question, title: answer, description: Faker::ChuckNorris.fact) + Faker::Lorem.words((2..4).to_a.sample).each do |answer| + Poll::Question::Answer.create!(question: question, title: answer.capitalize, description: Faker::ChuckNorris.fact) end end @@ -599,7 +597,10 @@ print "Creating Poll Questions from Proposals" 3.times do proposal = Proposal.reorder("RANDOM()").first poll = Poll.current.first - question = Poll::Question.create(valid_answers: "Yes, No", poll: poll) + question = Poll::Question.create(poll: poll) + Faker::Lorem.words((2..4).to_a.sample).each do |answer| + Poll::Question::Answer.create!(question: question, title: answer.capitalize, description: Faker::ChuckNorris.fact) + end question.copy_attributes_from_proposal(proposal) question.save! end @@ -610,7 +611,10 @@ print "Creating Successful Proposals" 10.times do proposal = Proposal.reorder("RANDOM()").first poll = Poll.current.first - question = Poll::Question.create(valid_answers: "Yes, No", poll: poll) + question = Poll::Question.create(poll: poll) + Faker::Lorem.words((2..4).to_a.sample).each do |answer| + Poll::Question::Answer.create!(question: question, title: answer.capitalize, description: Faker::ChuckNorris.fact) + end question.copy_attributes_from_proposal(proposal) question.save! end @@ -656,8 +660,7 @@ print "Creating legislation processes" allegations_phase_enabled: true, draft_publication_enabled: true, result_publication_enabled: true, - published: true - ) + published: true) end ::Legislation::Process.all.each do |process| From 0a5ee3f8619da23036b81b9fd8ae6de2f31d5a79 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:30:21 +0200 Subject: [PATCH 07/12] Remove valid_answers usage on Poll Question model --- app/models/poll/question.rb | 5 ----- 1 file changed, 5 deletions(-) diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 90e528bd0..066d223d9 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -39,17 +39,12 @@ class Poll::Question < ActiveRecord::Base author_visible_name => 'C' } end - def valid_answers - (super.try(:split, ',')&.compact || []).map(&:strip) - end - def copy_attributes_from_proposal(proposal) if proposal.present? self.author = proposal.author self.author_visible_name = proposal.author.name self.proposal_id = proposal.id self.title = proposal.title - self.valid_answers = I18n.t('poll_questions.default_valid_answers') end end From 8d2945e8a2227cc98b19f80222fd40cb40f33749 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:31:34 +0200 Subject: [PATCH 08/12] Fix answer attribute validation to check question's question_answers inclusion --- app/models/poll/answer.rb | 4 +--- app/models/poll/partial_result.rb | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index 4484060dd..ce4bc37af 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -9,9 +9,7 @@ class Poll::Answer < ActiveRecord::Base validates :author, presence: true validates :answer, presence: true - # temporary skipping validation, review when removing valid_answers - # validates :answer, inclusion: { in: ->(a) { a.question.valid_answers }}, - # unless: ->(a) { a.question.blank? } + validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }} scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } diff --git a/app/models/poll/partial_result.rb b/app/models/poll/partial_result.rb index 12b16aa3a..a44b61cad 100644 --- a/app/models/poll/partial_result.rb +++ b/app/models/poll/partial_result.rb @@ -10,7 +10,7 @@ class Poll::PartialResult < ActiveRecord::Base validates :question, presence: true validates :author, presence: true validates :answer, presence: true - validates :answer, inclusion: {in: ->(a) { a.question.valid_answers }} + validates :answer, inclusion: {in: ->(a) { a.question.question_answers.pluck(:title) }} validates :origin, inclusion: {in: VALID_ORIGINS} scope :by_author, ->(author_id) { where(author_id: author_id) } From e9294f2a222b7de240d6804181a74e34737abbdc Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:32:15 +0200 Subject: [PATCH 09/12] Switch from valid_answers to question_answers usage for Poll Questions --- app/controllers/admin/poll/questions_controller.rb | 3 +-- app/controllers/officing/results_controller.rb | 2 +- app/views/admin/poll/results/index.html.erb | 6 +++--- app/views/officing/results/index.html.erb | 6 +++--- app/views/officing/results/new.html.erb | 4 ++-- config/locales/en/activerecord.yml | 1 - config/locales/en/general.yml | 1 - config/locales/es/activerecord.yml | 1 - config/locales/es/general.yml | 1 - 9 files changed, 10 insertions(+), 15 deletions(-) diff --git a/app/controllers/admin/poll/questions_controller.rb b/app/controllers/admin/poll/questions_controller.rb index 5cf587735..64ecf4009 100644 --- a/app/controllers/admin/poll/questions_controller.rb +++ b/app/controllers/admin/poll/questions_controller.rb @@ -15,7 +15,6 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController def new @polls = Poll.all - @question.valid_answers = I18n.t('poll_questions.default_valid_answers') proposal = Proposal.find(params[:proposal_id]) if params[:proposal_id].present? @question.copy_attributes_from_proposal(proposal) end @@ -56,7 +55,7 @@ class Admin::Poll::QuestionsController < Admin::Poll::BaseController private def question_params - params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id, :valid_answers, :video_url) + params.require(:poll_question).permit(:poll_id, :title, :question, :proposal_id, :video_url) end def search_params diff --git a/app/controllers/officing/results_controller.rb b/app/controllers/officing/results_controller.rb index 23ef0b038..e3f0bb27c 100644 --- a/app/controllers/officing/results_controller.rb +++ b/app/controllers/officing/results_controller.rb @@ -51,7 +51,7 @@ class Officing::ResultsController < Officing::BaseController results.each_pair do |answer_index, count| next if count.blank? - answer = question.valid_answers[answer_index.to_i] + answer = question.question_answers.where(given_order: answer_index.to_i + 1).first.title go_back_to_new if question.blank? partial_result = ::Poll::PartialResult.find_or_initialize_by(booth_assignment_id: @officer_assignment.booth_assignment_id, diff --git a/app/views/admin/poll/results/index.html.erb b/app/views/admin/poll/results/index.html.erb index c65b3ead7..ab4e94857 100644 --- a/app/views/admin/poll/results/index.html.erb +++ b/app/views/admin/poll/results/index.html.erb @@ -37,11 +37,11 @@ - <% question.valid_answers.each_with_index do |answer, i| %> + <% question.question_answers.each_with_index do |answer, i| %> <% by_answer = by_question[question.id].present? ? by_question[question.id].group_by(&:answer) : {} %> - <%= answer %> - <%= by_answer[answer].present? ? by_answer[answer].sum(&:amount) : 0 %> + <%= answer.title %> + <%= by_answer[answer.title].present? ? by_answer[answer.title].sum(&:amount) : 0 %> <% end %> diff --git a/app/views/officing/results/index.html.erb b/app/views/officing/results/index.html.erb index 924d7fc13..67b6ece0f 100644 --- a/app/views/officing/results/index.html.erb +++ b/app/views/officing/results/index.html.erb @@ -40,11 +40,11 @@ - <% question.valid_answers.each_with_index do |answer, i| %> + <% question.question_answers.each_with_index do |answer, i| %> <% by_answer = by_question[question.id].present? ? by_question[question.id].group_by(&:answer) : {} %> - <%= answer %> - <%= by_answer[answer].present? ? by_answer[answer].first.amount : 0 %> + <%= answer.title %> + <%= by_answer[answer.title].present? ? by_answer[answer.title].first.amount : 0 %> <% end %> diff --git a/app/views/officing/results/new.html.erb b/app/views/officing/results/new.html.erb index fa5f449fb..2f05ca748 100644 --- a/app/views/officing/results/new.html.erb +++ b/app/views/officing/results/new.html.erb @@ -27,9 +27,9 @@

<%= question.title %>

- <% question.valid_answers.each_with_index do |answer, i| %> + <% question.question_answers.each_with_index do |answer, i| %>
- + <%= text_field_tag "questions[#{question.id}][#{i}]", answer_result_value(question.id, i), placeholder: "0" %>
<% end %> diff --git a/config/locales/en/activerecord.yml b/config/locales/en/activerecord.yml index bf2cbb712..8ef98b4d1 100644 --- a/config/locales/en/activerecord.yml +++ b/config/locales/en/activerecord.yml @@ -156,7 +156,6 @@ en: description: "Description" poll/question: title: "Question" - valid_answers: "Posibles answers" summary: "Summary" description: "Description" external_url: "Link to additional documentation" diff --git a/config/locales/en/general.yml b/config/locales/en/general.yml index 7b3766471..ec1e1e767 100644 --- a/config/locales/en/general.yml +++ b/config/locales/en/general.yml @@ -500,7 +500,6 @@ en: videos: "External video" poll_questions: create_question: "Create question" - default_valid_answers: "Yes, No" show: vote_answer: "Vote %{answer}" voted: "You have voted %{answer}" diff --git a/config/locales/es/activerecord.yml b/config/locales/es/activerecord.yml index f61e3bd53..03de3965d 100644 --- a/config/locales/es/activerecord.yml +++ b/config/locales/es/activerecord.yml @@ -150,7 +150,6 @@ es: description: "Descripción" poll/question: title: "Pregunta" - valid_answers: "Posibles respuestas" summary: "Resumen" description: "Descripción" external_url: "Enlace a documentación adicional" diff --git a/config/locales/es/general.yml b/config/locales/es/general.yml index 0d2d11a52..c1e71cb33 100644 --- a/config/locales/es/general.yml +++ b/config/locales/es/general.yml @@ -500,7 +500,6 @@ es: videos: "Vídeo externo" poll_questions: create_question: "Crear pregunta para votación" - default_valid_answers: "Sí, No" show: vote_answer: "Votar %{answer}" voted: "Has votado %{answer}" From c044fb293c159d6a0f94f6ccca65e1ac448bcfe6 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 01:59:30 +0200 Subject: [PATCH 10/12] Create poll_question trait with answers --- spec/factories.rb | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/spec/factories.rb b/spec/factories.rb index 3164599a5..b98fc7adb 100644 --- a/spec/factories.rb +++ b/spec/factories.rb @@ -503,6 +503,13 @@ FactoryGirl.define do poll association :author, factory: :user sequence(:title) { |n| "Question title #{n}" } + + trait :with_answers do + after(:create) do |question, _evaluator| + create(:poll_question_answer, question: question, title: "Yes") + create(:poll_question_answer, question: question, title: "No") + end + end end factory :poll_question_answer, class: 'Poll::Question::Answer' do @@ -573,13 +580,13 @@ FactoryGirl.define do end factory :poll_answer, class: 'Poll::Answer' do - association :question, factory: :poll_question + association :question, factory: [:poll_question, :with_answers] association :author, factory: [:user, :level_two] answer { question.question_answers.sample.title } end factory :poll_partial_result, class: 'Poll::PartialResult' do - association :question, factory: :poll_question + association :question, factory: [:poll_question, :with_answers] association :author, factory: :user origin { 'web' } answer { question.question_answers.sample.title } From 08823153a9f590225142301226ec97162fb50d16 Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 02:03:58 +0200 Subject: [PATCH 11/12] Correct answer inclusion validation for no question scenario --- app/models/poll/answer.rb | 3 ++- app/models/poll/partial_result.rb | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/app/models/poll/answer.rb b/app/models/poll/answer.rb index ce4bc37af..78122188a 100644 --- a/app/models/poll/answer.rb +++ b/app/models/poll/answer.rb @@ -9,7 +9,8 @@ class Poll::Answer < ActiveRecord::Base validates :author, presence: true validates :answer, presence: true - validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }} + validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }}, + unless: ->(a) { a.question.blank? } scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } diff --git a/app/models/poll/partial_result.rb b/app/models/poll/partial_result.rb index a44b61cad..0550650a5 100644 --- a/app/models/poll/partial_result.rb +++ b/app/models/poll/partial_result.rb @@ -10,8 +10,9 @@ class Poll::PartialResult < ActiveRecord::Base validates :question, presence: true validates :author, presence: true validates :answer, presence: true - validates :answer, inclusion: {in: ->(a) { a.question.question_answers.pluck(:title) }} - validates :origin, inclusion: {in: VALID_ORIGINS} + validates :answer, inclusion: { in: ->(a) { a.question.question_answers.pluck(:title) }}, + unless: ->(a) { a.question.blank? } + validates :origin, inclusion: { in: VALID_ORIGINS } scope :by_author, ->(author_id) { where(author_id: author_id) } scope :by_question, ->(question_id) { where(question_id: question_id) } From 36610f0c1a3bbe328f5e35179682291ea1513fba Mon Sep 17 00:00:00 2001 From: Bertocq Date: Wed, 18 Oct 2017 02:06:34 +0200 Subject: [PATCH 12/12] Use poll question trait with_answers to generate yes/no answers --- spec/models/poll/answer_spec.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/spec/models/poll/answer_spec.rb b/spec/models/poll/answer_spec.rb index fecf2f2d5..33cf833b6 100644 --- a/spec/models/poll/answer_spec.rb +++ b/spec/models/poll/answer_spec.rb @@ -43,7 +43,7 @@ describe Poll::Answer do let(:author) { create(:user, :level_two) } let(:poll) { create(:poll) } - let(:question) { create(:poll_question, poll: poll) } + let(:question) { create(:poll_question, :with_answers, poll: poll) } it "creates a poll_voter with user and poll data" do answer = create(:poll_answer, question: question, author: author, answer: "Yes")