diff --git a/app/assets/javascripts/answers.js.coffee b/app/assets/javascripts/answers.js.coffee new file mode 100644 index 000000000..183e1c628 --- /dev/null +++ b/app/assets/javascripts/answers.js.coffee @@ -0,0 +1,20 @@ +App.Answers = + + initializeAnswers: (answers) -> + $(answers).on 'cocoon:after-insert', (e, new_answer) -> + given_order = App.Answers.maxGivenOrder(answers) + 1 + $(new_answer).find("[name$='[given_order]']").val(given_order) + + maxGivenOrder: (answers) -> + max_order = 0 + $(answers).find("[name$='[given_order]']").each (index, answer) -> + value = parseFloat($(answer).val()) + max_order = if value > max_given_order then value else max_given_order + return max_given_order + + nestedAnswers: -> + $('.js-answers').each (index, answers) -> + App.Answers.initializeAnswers(answers) + + initialize: -> + App.Answers.nestedAnswers() diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index dd02ee6ee..82b40edc9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -59,6 +59,8 @@ //= require markdown-it //= require markdown_editor //= require cocoon +//= require answers +//= require questions //= require legislation_admin //= require legislation //= require legislation_allegations @@ -87,6 +89,8 @@ //= require send_admin_notification_alert var initialize_modules = function() { + App.Answers.initialize(); + App.Questions.initialize(); App.Comments.initialize(); App.Users.initialize(); App.Votes.initialize(); diff --git a/app/assets/javascripts/questions.js.coffee b/app/assets/javascripts/questions.js.coffee new file mode 100644 index 000000000..72023a841 --- /dev/null +++ b/app/assets/javascripts/questions.js.coffee @@ -0,0 +1,8 @@ +App.Questions = + + nestedQuestions: -> + $('.js-questions').on 'cocoon:after-insert', (e, new_question) -> + App.Answers.initializeAnswers($(new_question).find('.js-answers')) + + initialize: -> + App.Questions.nestedQuestions() diff --git a/app/controllers/admin/poll/questions/answers_controller.rb b/app/controllers/admin/poll/questions/answers_controller.rb index 225665b94..1d549ba3f 100644 --- a/app/controllers/admin/poll/questions/answers_controller.rb +++ b/app/controllers/admin/poll/questions/answers_controller.rb @@ -50,7 +50,8 @@ class Admin::Poll::Questions::AnswersController < Admin::Poll::BaseController def answer_params documents_attributes = [:id, :title, :attachment, :cached_attachment, :user_id, :_destroy] - attributes = [:title, :description, :question_id, documents_attributes: documents_attributes] + attributes = [:title, :description, :given_order, :question_id, + documents_attributes: documents_attributes] params.require(:poll_question_answer).permit(*attributes, *translation_params(Poll::Question::Answer)) end diff --git a/app/controllers/dashboard/polls_controller.rb b/app/controllers/dashboard/polls_controller.rb index 9f38c3459..40057230f 100644 --- a/app/controllers/dashboard/polls_controller.rb +++ b/app/controllers/dashboard/polls_controller.rb @@ -1,4 +1,4 @@ -class Dashboard::PollsController < Dashboard::BaseController +class Dashboard::PollsController < Dashboard::BaseController helper_method :poll def index @@ -62,7 +62,8 @@ class Dashboard::PollsController < Dashboard::BaseController end def question_answers_attributes - [:id, :_destroy, :title, :description, :question_id, documents_attributes: documents_attributes] + [:id, :_destroy, :title, :description, :given_order, :question_id, + documents_attributes: documents_attributes] end def documents_attributes 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/app/views/admin/poll/questions/answers/_form.html.erb b/app/views/admin/poll/questions/answers/_form.html.erb index 1ca05d9ab..912e6bd42 100644 --- a/app/views/admin/poll/questions/answers/_form.html.erb +++ b/app/views/admin/poll/questions/answers/_form.html.erb @@ -4,6 +4,9 @@ <%= render 'shared/errors', resource: @answer %> + <%= f.hidden_field :given_order, + value: @answer.persisted? ? @answer.given_order : @answer.class.last_position(@answer.question_id || @question.id) + 1 %> + <%= f.hidden_field :question_id, value: @answer.question_id || @question.id %> <%= f.translatable_text_field :title %> diff --git a/app/views/dashboard/polls/_form.html.erb b/app/views/dashboard/polls/_form.html.erb index 0b856dd71..fbdaf9265 100644 --- a/app/views/dashboard/polls/_form.html.erb +++ b/app/views/dashboard/polls/_form.html.erb @@ -25,12 +25,12 @@ -
+
<%= f.fields_for :questions do |question| %> <%= render 'question_fields', f: question %> <% end %> -