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:
Senén Rodero Rodríguez
2018-12-27 18:06:13 +01:00
parent 7b2258aeb0
commit c73aae9663
6 changed files with 42 additions and 44 deletions

View File

@@ -17,8 +17,6 @@ class Poll::Question::Answer < ActiveRecord::Base
validates :title, presence: true
validates :given_order, presence: true, uniqueness: { scope: :question_id }
before_validation :set_order, on: :create
def description
self[:description].try :html_safe
end
@@ -29,10 +27,6 @@ class Poll::Question::Answer < ActiveRecord::Base
end
end
def set_order
self.given_order = self.class.last_position(question_id) + 1
end
def self.last_position(question_id)
where(question_id: question_id).maximum('given_order') || 0
end

View File

@@ -56,11 +56,12 @@ section "Creating Poll Questions & Answers" do
end
end
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>"
answer = Poll::Question::Answer.new(question: question,
title: title.capitalize,
description: description)
description: description,
given_order: index + 1)
I18n.available_locales.map do |locale|
Globalize.with_locale(locale) do
answer.title = "#{title} (#{locale})"
@@ -200,20 +201,7 @@ section "Creating Poll Questions from Proposals" do
3.times do
proposal = Proposal.all.sample
poll = Poll.current.first
question = Poll::Question.create(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 = Poll::Question.new(poll: poll)
question.copy_attributes_from_proposal(proposal)
title = question.title
I18n.available_locales.map do |locale|
@@ -222,6 +210,20 @@ section "Creating Poll Questions from Proposals" do
end
end
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
@@ -229,20 +231,7 @@ section "Creating Successful Proposals" do
10.times do
proposal = Proposal.all.sample
poll = Poll.current.first
question = Poll::Question.create(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 = Poll::Question.new(poll: poll)
question.copy_attributes_from_proposal(proposal)
title = question.title
I18n.available_locales.map do |locale|
@@ -251,5 +240,19 @@ section "Creating Successful Proposals" do
end
end
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

View File

@@ -47,6 +47,7 @@ FactoryBot.define do
association :question, factory: :poll_question
sequence(:title) { |n| "Answer title #{n}" }
sequence(:description) { |n| "Answer description #{n}" }
sequence(:given_order) { |n| n }
end
factory :poll_answer_video, class: 'Poll::Question::Answer::Video' do

View File

@@ -8,11 +8,11 @@ feature 'Officing Results', :with_frozen_time do
@poll = @officer_assignment.booth_assignment.poll
@poll.update(ends_at: 1.day.ago)
@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)
create(:poll_question_answer, title: "Yes", question: @question_1, given_order: 1)
create(:poll_question_answer, title: "No", question: @question_1, given_order: 2)
@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)
create(:poll_question_answer, title: "Today", question: @question_2, given_order: 1)
create(:poll_question_answer, title: "Tomorrow", question: @question_2, given_order: 2)
login_as(@poll_officer.user)
end

View File

@@ -16,7 +16,7 @@ feature 'Answers' do
visit admin_question_path(question)
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
expect(page).to have_content answer1.title

View File

@@ -28,7 +28,7 @@ feature 'Polls' do
scenario "Proposal polls won't be listed" do
proposal = create(:proposal)
_poll = create(:poll, related: proposal)
_poll = create(:poll, related: proposal)
visit polls_path
expect(page).to have_content('There are no open votings')
@@ -164,7 +164,7 @@ feature 'Polls' do
visit poll_path(poll)
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
@@ -176,7 +176,7 @@ feature 'Polls' do
visit poll_path(poll)
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