PR comments applied and poll_question_answer default name changed in factory.

This commit is contained in:
iagirre
2017-10-11 09:42:52 +02:00
parent 44474e7686
commit 644d09ebd2
3 changed files with 12 additions and 9 deletions

View File

@@ -9,7 +9,6 @@ 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),

View File

@@ -10,6 +10,9 @@ class Poll::Question::Answer < ActiveRecord::Base
has_many :videos, class_name: 'Poll::Question::Answer::Video' has_many :videos, class_name: 'Poll::Question::Answer::Video'
validates :title, presence: true validates :title, presence: true
validates :given_order, presence: true, uniqueness: { scope: :question_id }
before_validation :set_order, on: :create
def description def description
super.try :html_safe super.try :html_safe
@@ -17,15 +20,16 @@ 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 = find(answer_id) find(answer_id).update_attribute(:given_order, (order + 1))
answer.update_attribute(:given_order, (order + 1))
answer.save
end end
end end
def set_order def set_order
last_position = Poll::Question::Answer.where(question_id: question_id).maximum("given_order") || 0 next_position = self.class.last_position(question_id) + 1
next_position = last_position + 1 self.given_order = next_position
update_attribute(:given_order, next_position) end
def self.last_position(question_id)
where(question_id: question_id).maximum("given_order") || 0
end end
end end

View File

@@ -503,8 +503,8 @@ FactoryGirl.define do
factory :poll_question_answer, class: 'Poll::Question::Answer' do factory :poll_question_answer, class: 'Poll::Question::Answer' do
association :question, factory: :poll_question association :question, factory: :poll_question
sequence(:title) { |n| "Question title #{n}" } sequence(:title) { |n| "Answer title #{n}" }
sequence(:description) { |n| "Question description #{n}" } sequence(:description) { |n| "Answer description #{n}" }
end end
factory :poll_booth, class: 'Poll::Booth' do factory :poll_booth, class: 'Poll::Booth' do