Spects added to test the order of answers. Default order for question_answers set.

This commit is contained in:
iagirre
2017-10-10 13:28:49 +02:00
parent 2b10b59e2a
commit 943c1f23af
6 changed files with 67 additions and 14 deletions

View File

@@ -9,6 +9,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
def create def create
@answer = ::Poll::Question::Answer.new(answer_params) @answer = ::Poll::Question::Answer.new(answer_params)
@answer.set_order
if @answer.save if @answer.save
redirect_to admin_question_path(@answer.question), redirect_to admin_question_path(@answer.question),
@@ -42,7 +43,7 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController
def order_answers def order_answers
::Poll::Question::Answer.order_answers(params[:ordered_list]) ::Poll::Question::Answer.order_answers(params[:ordered_list])
# redirect_to admin_question_path(params[:question_id]) # redirect_to admin_question_path(params[:question_id])
render :nothing => true render nothing: true
end end
private private

View File

@@ -10,7 +10,7 @@ class Poll::Question < ActiveRecord::Base
has_many :comments, as: :commentable has_many :comments, as: :commentable
has_many :answers, class_name: 'Poll::Answer' has_many :answers, class_name: 'Poll::Answer'
has_many :question_answers, class_name: 'Poll::Question::Answer' has_many :question_answers, -> { order 'given_order asc' }, class_name: 'Poll::Question::Answer'
has_many :partial_results has_many :partial_results
belongs_to :proposal belongs_to :proposal

View File

@@ -17,9 +17,15 @@ class Poll::Question::Answer < ActiveRecord::Base
def self.order_answers(ordered_array) def self.order_answers(ordered_array)
ordered_array.each_with_index do |answer_id, order| ordered_array.each_with_index do |answer_id, order|
answer = self.find(answer_id) answer = find(answer_id)
answer.update_attribute(:given_order, (order + 1)) answer.update_attribute(:given_order, (order + 1))
answer.save answer.save
end end
end end
def set_order
last_position = Poll::Question::Answer.where(question_id: question_id).maximum("given_order") || 0
next_position = last_position + 1
update_attribute(:given_order, next_position)
end
end end

View File

@@ -4,7 +4,7 @@ feature 'Answers' do
background do background do
admin = create(:administrator) admin = create(:administrator)
login_as (admin.user) login_as admin.user
end end
scenario 'Create' do scenario 'Create' do
@@ -24,9 +24,27 @@ feature 'Answers' do
expect(page).to have_content(description) expect(page).to have_content(description)
end end
scenario 'Create second answer and place after the first one' do
question = create(:poll_question)
answer = create(:poll_question_answer, title: 'First', question: question, given_order: 1)
title = 'Second'
description = "Description"
visit admin_question_path(question)
click_link 'Add answer'
fill_in 'poll_question_answer_title', with: title
fill_in 'poll_question_answer_description', with: description
click_button 'Save'
expect(page.body.index('First')).to be < page.body.index('Second')
end
scenario 'Update' do scenario 'Update' do
question = create(:poll_question) question = create(:poll_question)
answer = create(:poll_question_answer, question: question, title: "Answer title") answer = create(:poll_question_answer, question: question, title: "Answer title", given_order: 1)
answer2 = create(:poll_question_answer, question: question, title: "Another title", given_order: 2)
visit admin_answer_path(answer) visit admin_answer_path(answer)
@@ -46,6 +64,8 @@ feature 'Answers' do
expect(page).to have_content(new_title) expect(page).to have_content(new_title)
expect(page).to_not have_content(old_title) expect(page).to_not have_content(old_title)
expect(page.body.index(new_title)).to be < page.body.index(answer2.title)
end end
end end

View File

@@ -9,13 +9,15 @@ feature 'Answers' do
scenario "Index" do scenario "Index" do
question = create(:poll_question) question = create(:poll_question)
answer1 = create(:poll_question_answer, question: question) answer1 = create(:poll_question_answer, question: question, given_order: 1)
answer2 = create(:poll_question_answer, question: question) answer2 = create(:poll_question_answer, question: question, given_order: 2)
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)
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
expect(page).to have_content answer1.description expect(page).to have_content answer1.description

View File

@@ -78,6 +78,30 @@ feature 'Polls' do
expect(page).to have_content(proposal_question.title) expect(page).to have_content(proposal_question.title)
end end
scenario "Question answers appear in the given order" do
question = create(:poll_question, poll: poll)
answer1 = create(:poll_question_answer, title: 'First', question: question, given_order: 1)
answer2 = create(:poll_question_answer, title: 'Second', question: question, given_order: 2)
visit poll_path(poll)
within("div#poll_question_#{question.id}") do
expect(page.body.index(answer1.title)).to be < page.body.index(answer2.title)
end
end
scenario "More info answers appear in the given order" do
question = create(:poll_question, poll: poll)
answer1 = create(:poll_question_answer, title: 'First', question: question, given_order: 1)
answer2 = create(:poll_question_answer, title: 'Second', question: question, given_order: 2)
visit poll_path(poll)
within('div.poll-more-info-answers') do
expect(page.body.index(answer1.title)).to be < page.body.index(answer2.title)
end
end
scenario 'Non-logged in users' do scenario 'Non-logged in users' do
question = create(:poll_question, poll: poll) question = create(:poll_question, poll: poll)
answer1 = create(:poll_question_answer, question: question, title: 'Han Solo') answer1 = create(:poll_question_answer, question: question, title: 'Han Solo')