diff --git a/app/models/poll/question/answer.rb b/app/models/poll/question/answer.rb index 56655e457..48abdd9b8 100644 --- a/app/models/poll/question/answer.rb +++ b/app/models/poll/question/answer.rb @@ -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 diff --git a/db/dev_seeds/polls.rb b/db/dev_seeds/polls.rb index a7884d570..1026cb805 100644 --- a/db/dev_seeds/polls.rb +++ b/db/dev_seeds/polls.rb @@ -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 = "
#{Faker::Lorem.paragraphs.join('
')}
" 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 = "#{Faker::ChuckNorris.fact}
" - 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 = "#{Faker::ChuckNorris.fact}
" + 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 = "#{Faker::ChuckNorris.fact}
" - 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 = "#{Faker::ChuckNorris.fact}
" + 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 diff --git a/spec/factories/polls.rb b/spec/factories/polls.rb index 648a84afa..88e3b51aa 100644 --- a/spec/factories/polls.rb +++ b/spec/factories/polls.rb @@ -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 diff --git a/spec/features/officing/results_spec.rb b/spec/features/officing/results_spec.rb index 376629314..e46bef692 100644 --- a/spec/features/officing/results_spec.rb +++ b/spec/features/officing/results_spec.rb @@ -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 diff --git a/spec/features/polls/answers_spec.rb b/spec/features/polls/answers_spec.rb index 6daffc0eb..8e5bb785e 100644 --- a/spec/features/polls/answers_spec.rb +++ b/spec/features/polls/answers_spec.rb @@ -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 diff --git a/spec/features/polls/polls_spec.rb b/spec/features/polls/polls_spec.rb index 64d92eb14..69fbd68e5 100644 --- a/spec/features/polls/polls_spec.rb +++ b/spec/features/polls/polls_spec.rb @@ -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