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

View File

@@ -10,7 +10,7 @@ class Poll::Question < ActiveRecord::Base
has_many :comments, as: :commentable
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
belongs_to :proposal

View File

@@ -17,9 +17,15 @@ class Poll::Question::Answer < ActiveRecord::Base
def self.order_answers(ordered_array)
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.save
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

View File

@@ -4,7 +4,7 @@ feature 'Answers' do
background do
admin = create(:administrator)
login_as (admin.user)
login_as admin.user
end
scenario 'Create' do
@@ -24,9 +24,27 @@ feature 'Answers' do
expect(page).to have_content(description)
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
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)
@@ -46,6 +64,8 @@ feature 'Answers' do
expect(page).to have_content(new_title)
expect(page).to_not have_content(old_title)
expect(page.body.index(new_title)).to be < page.body.index(answer2.title)
end
end

View File

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

View File

@@ -78,6 +78,30 @@ feature 'Polls' do
expect(page).to have_content(proposal_question.title)
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
question = create(:poll_question, poll: poll)
answer1 = create(:poll_question_answer, question: question, title: 'Han Solo')