From 0611e0f4eaacef3ef0b5cd87e73b33ec1b0ca598 Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Thu, 14 Sep 2017 09:01:56 -0400 Subject: [PATCH 1/2] Validate presence of 'poll_id' attribute on Poll::Question model Fixes #1831 On branch aperez-validate-poll-question-is-selected Changes to be committed: modified: app/models/poll/question.rb modified: spec/models/poll/question_spec.rb modified: spec/features/admin/poll/polls_spec.rb --- app/models/poll/question.rb | 1 + spec/features/admin/poll/polls_spec.rb | 2 +- spec/models/poll/question_spec.rb | 14 ++++++++++++++ 3 files changed, 16 insertions(+), 1 deletion(-) diff --git a/app/models/poll/question.rb b/app/models/poll/question.rb index 8e02dcfe2..a46f28a8c 100644 --- a/app/models/poll/question.rb +++ b/app/models/poll/question.rb @@ -20,6 +20,7 @@ class Poll::Question < ActiveRecord::Base validates :title, presence: true validates :author, presence: true + validates :poll_id, presence: true validates :title, length: { minimum: 4 } validates :description, length: { maximum: Poll::Question.description_max_length } diff --git a/spec/features/admin/poll/polls_spec.rb b/spec/features/admin/poll/polls_spec.rb index ee90d2ada..13ec0db58 100644 --- a/spec/features/admin/poll/polls_spec.rb +++ b/spec/features/admin/poll/polls_spec.rb @@ -183,7 +183,7 @@ feature 'Admin polls' do scenario 'Add question to poll', :js do poll = create(:poll) - question = create(:poll_question, poll: nil, title: 'Should we rebuild the city?') + question = create(:poll_question, title: 'Should we rebuild the city?') visit admin_poll_path(poll) diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb index 057dfebca..db6a0f762 100644 --- a/spec/models/poll/question_spec.rb +++ b/spec/models/poll/question_spec.rb @@ -9,6 +9,20 @@ RSpec.describe Poll::Question, type: :model do end end + describe "#poll_question_id" do + it "should be invalid if a poll is not selected" do + q = create(:poll_question) + q.poll_id = nil + expect(q).to_not be_valid + end + + it "should be valid if a poll is selected" do + q = create(:poll_question) + q.poll_id = 1 + expect(q).to be_valid + end + end + describe "#copy_attributes_from_proposal" do it "copies the attributes from the proposal" do create_list(:geozone, 3) From 15ccef8c97feba7eee396430e25e7a08488c36a1 Mon Sep 17 00:00:00 2001 From: Angel Perez Date: Thu, 14 Sep 2017 09:07:50 -0400 Subject: [PATCH 2/2] Poll::Question model spec refactor to improve DRYness On branch aperez-validate-poll-question-is-selected Changes to be committed: modified: spec/models/poll/question_spec.rb --- spec/models/poll/question_spec.rb | 28 +++++++++++++--------------- 1 file changed, 13 insertions(+), 15 deletions(-) diff --git a/spec/models/poll/question_spec.rb b/spec/models/poll/question_spec.rb index db6a0f762..60f9d6716 100644 --- a/spec/models/poll/question_spec.rb +++ b/spec/models/poll/question_spec.rb @@ -1,25 +1,24 @@ 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 - q = create(:poll_question, valid_answers: "Yes, No") - expect(q.valid_answers).to eq(["Yes", "No"]) + 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 - q = create(:poll_question) - q.poll_id = nil - expect(q).to_not be_valid + poll_question.poll_id = nil + expect(poll_question).to_not be_valid end it "should be valid if a poll is selected" do - q = create(:poll_question) - q.poll_id = 1 - expect(q).to be_valid + poll_question.poll_id = 1 + expect(poll_question).to be_valid end end @@ -27,13 +26,12 @@ RSpec.describe Poll::Question, type: :model do it "copies the attributes from the proposal" do create_list(:geozone, 3) p = create(:proposal) - q = create(:poll_question) - q.copy_attributes_from_proposal(p) - expect(q.valid_answers).to eq(['Yes', 'No']) - expect(q.author).to eq(p.author) - expect(q.author_visible_name).to eq(p.author.name) - expect(q.proposal_id).to eq(p.id) - expect(q.title).to eq(p.title) + 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) + expect(poll_question.title).to eq(p.title) end end