Remove before validation callback
This was breaking nested poll_questions_answers when submitting more than one new answer at a time.
This commit is contained in:
@@ -17,8 +17,6 @@ class Poll::Question::Answer < ActiveRecord::Base
|
|||||||
validates :title, presence: true
|
validates :title, presence: true
|
||||||
validates :given_order, presence: true, uniqueness: { scope: :question_id }
|
validates :given_order, presence: true, uniqueness: { scope: :question_id }
|
||||||
|
|
||||||
before_validation :set_order, on: :create
|
|
||||||
|
|
||||||
def description
|
def description
|
||||||
self[:description].try :html_safe
|
self[:description].try :html_safe
|
||||||
end
|
end
|
||||||
@@ -29,10 +27,6 @@ class Poll::Question::Answer < ActiveRecord::Base
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
def set_order
|
|
||||||
self.given_order = self.class.last_position(question_id) + 1
|
|
||||||
end
|
|
||||||
|
|
||||||
def self.last_position(question_id)
|
def self.last_position(question_id)
|
||||||
where(question_id: question_id).maximum('given_order') || 0
|
where(question_id: question_id).maximum('given_order') || 0
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -56,11 +56,12 @@ section "Creating Poll Questions & Answers" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
question.save!
|
question.save!
|
||||||
Faker::Lorem.words((2..4).to_a.sample).each do |title|
|
Faker::Lorem.words((2..4).to_a.sample).each_with_index do |title, index|
|
||||||
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
|
description = "<p>#{Faker::Lorem.paragraphs.join('</p><p>')}</p>"
|
||||||
answer = Poll::Question::Answer.new(question: question,
|
answer = Poll::Question::Answer.new(question: question,
|
||||||
title: title.capitalize,
|
title: title.capitalize,
|
||||||
description: description)
|
description: description,
|
||||||
|
given_order: index + 1)
|
||||||
I18n.available_locales.map do |locale|
|
I18n.available_locales.map do |locale|
|
||||||
Globalize.with_locale(locale) do
|
Globalize.with_locale(locale) do
|
||||||
answer.title = "#{title} (#{locale})"
|
answer.title = "#{title} (#{locale})"
|
||||||
@@ -200,20 +201,7 @@ section "Creating Poll Questions from Proposals" do
|
|||||||
3.times do
|
3.times do
|
||||||
proposal = Proposal.all.sample
|
proposal = Proposal.all.sample
|
||||||
poll = Poll.current.first
|
poll = Poll.current.first
|
||||||
question = Poll::Question.create(poll: poll)
|
question = Poll::Question.new(poll: poll)
|
||||||
Faker::Lorem.words((2..4).to_a.sample).each do |title|
|
|
||||||
description = "<p>#{Faker::ChuckNorris.fact}</p>"
|
|
||||||
answer = Poll::Question::Answer.new(question: question,
|
|
||||||
title: title.capitalize,
|
|
||||||
description: description)
|
|
||||||
I18n.available_locales.map do |locale|
|
|
||||||
Globalize.with_locale(locale) do
|
|
||||||
answer.title = "#{title} (#{locale})"
|
|
||||||
answer.description = "#{description} (#{locale})"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
answer.save!
|
|
||||||
end
|
|
||||||
question.copy_attributes_from_proposal(proposal)
|
question.copy_attributes_from_proposal(proposal)
|
||||||
title = question.title
|
title = question.title
|
||||||
I18n.available_locales.map do |locale|
|
I18n.available_locales.map do |locale|
|
||||||
@@ -222,6 +210,20 @@ section "Creating Poll Questions from Proposals" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
question.save!
|
question.save!
|
||||||
|
Faker::Lorem.words((2..4).to_a.sample).each_with_index do |title, index|
|
||||||
|
description = "<p>#{Faker::ChuckNorris.fact}</p>"
|
||||||
|
answer = Poll::Question::Answer.new(question: question,
|
||||||
|
title: title.capitalize,
|
||||||
|
description: description,
|
||||||
|
given_order: index + 1)
|
||||||
|
I18n.available_locales.map do |locale|
|
||||||
|
Globalize.with_locale(locale) do
|
||||||
|
answer.title = "#{title} (#{locale})"
|
||||||
|
answer.description = "#{description} (#{locale})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
answer.save!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -229,20 +231,7 @@ section "Creating Successful Proposals" do
|
|||||||
10.times do
|
10.times do
|
||||||
proposal = Proposal.all.sample
|
proposal = Proposal.all.sample
|
||||||
poll = Poll.current.first
|
poll = Poll.current.first
|
||||||
question = Poll::Question.create(poll: poll)
|
question = Poll::Question.new(poll: poll)
|
||||||
Faker::Lorem.words((2..4).to_a.sample).each do |title|
|
|
||||||
description = "<p>#{Faker::ChuckNorris.fact}</p>"
|
|
||||||
answer = Poll::Question::Answer.new(question: question,
|
|
||||||
title: title.capitalize,
|
|
||||||
description: description)
|
|
||||||
I18n.available_locales.map do |locale|
|
|
||||||
Globalize.with_locale(locale) do
|
|
||||||
answer.title = "#{title} (#{locale})"
|
|
||||||
answer.description = "#{description} (#{locale})"
|
|
||||||
end
|
|
||||||
end
|
|
||||||
answer.save!
|
|
||||||
end
|
|
||||||
question.copy_attributes_from_proposal(proposal)
|
question.copy_attributes_from_proposal(proposal)
|
||||||
title = question.title
|
title = question.title
|
||||||
I18n.available_locales.map do |locale|
|
I18n.available_locales.map do |locale|
|
||||||
@@ -251,5 +240,19 @@ section "Creating Successful Proposals" do
|
|||||||
end
|
end
|
||||||
end
|
end
|
||||||
question.save!
|
question.save!
|
||||||
|
Faker::Lorem.words((2..4).to_a.sample).each_with_index do |title, index|
|
||||||
|
description = "<p>#{Faker::ChuckNorris.fact}</p>"
|
||||||
|
answer = Poll::Question::Answer.new(question: question,
|
||||||
|
title: title.capitalize,
|
||||||
|
description: description,
|
||||||
|
given_order: index + 1)
|
||||||
|
I18n.available_locales.map do |locale|
|
||||||
|
Globalize.with_locale(locale) do
|
||||||
|
answer.title = "#{title} (#{locale})"
|
||||||
|
answer.description = "#{description} (#{locale})"
|
||||||
|
end
|
||||||
|
end
|
||||||
|
answer.save!
|
||||||
|
end
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -47,6 +47,7 @@ FactoryBot.define do
|
|||||||
association :question, factory: :poll_question
|
association :question, factory: :poll_question
|
||||||
sequence(:title) { |n| "Answer title #{n}" }
|
sequence(:title) { |n| "Answer title #{n}" }
|
||||||
sequence(:description) { |n| "Answer description #{n}" }
|
sequence(:description) { |n| "Answer description #{n}" }
|
||||||
|
sequence(:given_order) { |n| n }
|
||||||
end
|
end
|
||||||
|
|
||||||
factory :poll_answer_video, class: 'Poll::Question::Answer::Video' do
|
factory :poll_answer_video, class: 'Poll::Question::Answer::Video' do
|
||||||
|
|||||||
@@ -8,11 +8,11 @@ feature 'Officing Results', :with_frozen_time do
|
|||||||
@poll = @officer_assignment.booth_assignment.poll
|
@poll = @officer_assignment.booth_assignment.poll
|
||||||
@poll.update(ends_at: 1.day.ago)
|
@poll.update(ends_at: 1.day.ago)
|
||||||
@question_1 = create(:poll_question, poll: @poll)
|
@question_1 = create(:poll_question, poll: @poll)
|
||||||
create(:poll_question_answer, title: 'Yes', question: @question_1)
|
create(:poll_question_answer, title: "Yes", question: @question_1, given_order: 1)
|
||||||
create(:poll_question_answer, title: 'No', question: @question_1)
|
create(:poll_question_answer, title: "No", question: @question_1, given_order: 2)
|
||||||
@question_2 = create(:poll_question, poll: @poll)
|
@question_2 = create(:poll_question, poll: @poll)
|
||||||
create(:poll_question_answer, title: 'Today', question: @question_2)
|
create(:poll_question_answer, title: "Today", question: @question_2, given_order: 1)
|
||||||
create(:poll_question_answer, title: 'Tomorrow', question: @question_2)
|
create(:poll_question_answer, title: "Tomorrow", question: @question_2, given_order: 2)
|
||||||
|
|
||||||
login_as(@poll_officer.user)
|
login_as(@poll_officer.user)
|
||||||
end
|
end
|
||||||
|
|||||||
@@ -16,7 +16,7 @@ feature 'Answers' do
|
|||||||
visit admin_question_path(question)
|
visit admin_question_path(question)
|
||||||
|
|
||||||
expect(page).to have_css(".poll_question_answer", count: 2)
|
expect(page).to have_css(".poll_question_answer", count: 2)
|
||||||
expect(page.body.index(answer1.title)).to be < page.body.index(answer2.title)
|
expect(answer2.title).to appear_before(answer1.title)
|
||||||
|
|
||||||
within("#poll_question_answer_#{answer1.id}") do
|
within("#poll_question_answer_#{answer1.id}") do
|
||||||
expect(page).to have_content answer1.title
|
expect(page).to have_content answer1.title
|
||||||
|
|||||||
@@ -28,7 +28,7 @@ feature 'Polls' do
|
|||||||
|
|
||||||
scenario "Proposal polls won't be listed" do
|
scenario "Proposal polls won't be listed" do
|
||||||
proposal = create(:proposal)
|
proposal = create(:proposal)
|
||||||
_poll = create(:poll, related: proposal)
|
_poll = create(:poll, related: proposal)
|
||||||
|
|
||||||
visit polls_path
|
visit polls_path
|
||||||
expect(page).to have_content('There are no open votings')
|
expect(page).to have_content('There are no open votings')
|
||||||
@@ -164,7 +164,7 @@ feature 'Polls' do
|
|||||||
visit poll_path(poll)
|
visit poll_path(poll)
|
||||||
|
|
||||||
within("div#poll_question_#{question.id}") do
|
within("div#poll_question_#{question.id}") do
|
||||||
expect(page.body.index(answer1.title)).to be < page.body.index(answer2.title)
|
expect(answer2.title).to appear_before(answer1.title)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
@@ -176,7 +176,7 @@ feature 'Polls' do
|
|||||||
visit poll_path(poll)
|
visit poll_path(poll)
|
||||||
|
|
||||||
within('div.poll-more-info-answers') do
|
within('div.poll-more-info-answers') do
|
||||||
expect(page.body.index(answer1.title)).to be < page.body.index(answer2.title)
|
expect(answer2.title).to appear_before(answer1.title)
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user