From c73aae96639e2632b21e63d8b2e825beabc23aa1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 27 Dec 2018 18:06:13 +0100 Subject: [PATCH 1/8] Remove before validation callback This was breaking nested poll_questions_answers when submitting more than one new answer at a time. --- app/models/poll/question/answer.rb | 6 --- db/dev_seeds/polls.rb | 63 ++++++++++++++------------ spec/factories/polls.rb | 1 + spec/features/officing/results_spec.rb | 8 ++-- spec/features/polls/answers_spec.rb | 2 +- spec/features/polls/polls_spec.rb | 6 +-- 6 files changed, 42 insertions(+), 44 deletions(-) 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 From 9d9ad5003bbd5b7c7a9b57fdca4c621fe50caff8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 27 Dec 2018 18:13:32 +0100 Subject: [PATCH 2/8] Add given_order to related poll question answers forms Since given order is no longer being generated automatically we need to add it to related forms and to strong parameters methods --- app/controllers/admin/poll/questions/answers_controller.rb | 3 ++- app/controllers/dashboard/polls_controller.rb | 5 +++-- app/views/admin/poll/questions/answers/_form.html.erb | 3 +++ app/views/dashboard/polls/_question_answer_fields.html.erb | 1 + spec/features/admin/poll/questions/answers/answers_spec.rb | 2 +- 5 files changed, 10 insertions(+), 4 deletions(-) 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/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/_question_answer_fields.html.erb b/app/views/dashboard/polls/_question_answer_fields.html.erb index dd7e4e15a..e7bbce656 100644 --- a/app/views/dashboard/polls/_question_answer_fields.html.erb +++ b/app/views/dashboard/polls/_question_answer_fields.html.erb @@ -1,5 +1,6 @@
+ <%= f.hidden_field :given_order %>
<%= f.text_field :title %> diff --git a/spec/features/admin/poll/questions/answers/answers_spec.rb b/spec/features/admin/poll/questions/answers/answers_spec.rb index dbcf8cf5c..36185743c 100644 --- a/spec/features/admin/poll/questions/answers/answers_spec.rb +++ b/spec/features/admin/poll/questions/answers/answers_spec.rb @@ -65,7 +65,7 @@ feature 'Answers' do expect(page).to have_content(new_title) expect(page).not_to have_content(old_title) - expect(page.body.index(new_title)).to be < page.body.index(answer2.title) + expect(answer2.title).to appear_before(new_title) end end From 045c950c3bc0365a8e5c627829204680e5c2da66 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 27 Dec 2018 18:15:52 +0100 Subject: [PATCH 3/8] Add poll question answers script for nested fields Set poll questions answers given_order value dinamically with JS to allow to create multiple answers with correct given_order. --- app/assets/javascripts/answers.js.coffee | 9 +++++++++ app/assets/javascripts/application.js | 2 ++ 2 files changed, 11 insertions(+) create mode 100644 app/assets/javascripts/answers.js.coffee diff --git a/app/assets/javascripts/answers.js.coffee b/app/assets/javascripts/answers.js.coffee new file mode 100644 index 000000000..c36d09f12 --- /dev/null +++ b/app/assets/javascripts/answers.js.coffee @@ -0,0 +1,9 @@ +App.Answers = + + nestedAnswers: -> + $('.nested-answers').on 'cocoon:after-insert', (e, insertedItem) -> + nestedAnswersCount = $("input[type='hidden'][name$='[given_order]']").size() + $(insertedItem).find("input[type='hidden'][name$='[given_order]']").val(nestedAnswersCount) + + initialize: -> + App.Answers.nestedAnswers() diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index dd02ee6ee..ec9b05b7b 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -59,6 +59,7 @@ //= require markdown-it //= require markdown_editor //= require cocoon +//= require answers //= require legislation_admin //= require legislation //= require legislation_allegations @@ -87,6 +88,7 @@ //= require send_admin_notification_alert var initialize_modules = function() { + App.Answers.initialize(); App.Comments.initialize(); App.Users.initialize(); App.Votes.initialize(); From 0d3e20f370fbd5a12d06d1bb845974f69472aad6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 28 Dec 2018 11:57:45 +0100 Subject: [PATCH 4/8] Fix dashboard poll form nested questions and answers Nested remove association links were not working properly because of missing Cocoon needed CSS class --- app/views/dashboard/polls/_form.html.erb | 7 +++++-- .../dashboard/polls/_question_answer_fields.html.erb | 2 +- app/views/dashboard/polls/_question_fields.html.erb | 9 ++++++--- 3 files changed, 12 insertions(+), 6 deletions(-) diff --git a/app/views/dashboard/polls/_form.html.erb b/app/views/dashboard/polls/_form.html.erb index 0b856dd71..da0ab6e32 100644 --- a/app/views/dashboard/polls/_form.html.erb +++ b/app/views/dashboard/polls/_form.html.erb @@ -30,10 +30,13 @@ <%= render 'question_fields', f: question %> <% end %> - diff --git a/app/views/dashboard/polls/_question_answer_fields.html.erb b/app/views/dashboard/polls/_question_answer_fields.html.erb index e7bbce656..ff41fb479 100644 --- a/app/views/dashboard/polls/_question_answer_fields.html.erb +++ b/app/views/dashboard/polls/_question_answer_fields.html.erb @@ -1,4 +1,4 @@ -
+
<%= f.hidden_field :given_order %>
diff --git a/app/views/dashboard/polls/_question_fields.html.erb b/app/views/dashboard/polls/_question_fields.html.erb index 50d752185..671bfa50e 100644 --- a/app/views/dashboard/polls/_question_fields.html.erb +++ b/app/views/dashboard/polls/_question_fields.html.erb @@ -1,4 +1,4 @@ -
+
<%= f.hidden_field :author_id, value: f.object.author_id || current_user.id %> <%= f.hidden_field :proposal_id, value: f.object.proposal_id || proposal.id %>
@@ -17,10 +17,13 @@ <%= render 'question_answer_fields', f: answer %> <% end %> - From bc2d020d7e4814c007782be02bd176b750995be4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Fri, 28 Dec 2018 15:02:39 +0100 Subject: [PATCH 5/8] Add nested specs Check remove behavior of poll nested resources: Questions and Answers --- spec/features/dashboard/polls_spec.rb | 67 +++++++++++++++++++++++---- 1 file changed, 58 insertions(+), 9 deletions(-) diff --git a/spec/features/dashboard/polls_spec.rb b/spec/features/dashboard/polls_spec.rb index 2eafa6652..052766c16 100644 --- a/spec/features/dashboard/polls_spec.rb +++ b/spec/features/dashboard/polls_spec.rb @@ -52,7 +52,7 @@ feature 'Polls' do scenario 'Edit poll is allowed for upcoming polls' do poll = create(:poll, :incoming, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -68,7 +68,7 @@ feature 'Polls' do scenario 'Edit poll redirects back when invalid data', js: true do poll = create(:poll, :incoming, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -86,7 +86,7 @@ feature 'Polls' do scenario 'Edit poll is not allowed for current polls' do poll = create(:poll, :current, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -96,7 +96,7 @@ feature 'Polls' do scenario 'Edit poll is not allowed for expired polls' do poll = create(:poll, :expired, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -104,9 +104,58 @@ feature 'Polls' do end end + scenario 'Edit poll should allow to remove questions', :js do + poll = create(:poll, :incoming, related: proposal) + question1 = create(:poll_question, poll: poll) + question2 = create(:poll_question, poll: poll) + visit proposal_dashboard_polls_path(proposal) + within "div#poll_#{poll.id}" do + click_link 'Edit survey' + end + + within "#questions" do + expect(page).to have_css ".nested-fields", count: 2 + within first(".nested-fields") do + find('a.delete').click + end + expect(page).to have_css ".nested-fields", count: 1 + end + + click_button 'Update poll' + visit edit_proposal_dashboard_poll_path(proposal, poll) + + expect(page).to have_css ".nested-fields", count: 1 + end + + scenario 'Edit poll should allow to remove answers', :js do + poll = create(:poll, :incoming, related: proposal) + question = create(:poll_question, poll: poll) + answer1 = create(:poll_question_answer, question: question) + answer2 = create(:poll_question_answer, question: question) + visit proposal_dashboard_polls_path(proposal) + within "div#poll_#{poll.id}" do + click_link 'Edit survey' + end + + within "#questions #answers" do + expect(page).to have_css ".nested-fields", count: 2 + within first(".nested-fields") do + find('a.delete').click + end + expect(page).to have_css ".nested-fields", count: 1 + end + + click_button 'Update poll' + visit edit_proposal_dashboard_poll_path(proposal, poll) + + within "#questions #answers" do + expect(page).to have_css ".nested-fields", count: 1 + end + end + scenario 'View results not available for upcoming polls' do poll = create(:poll, :incoming, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -116,7 +165,7 @@ feature 'Polls' do scenario 'View results available for current polls' do poll = create(:poll, :current, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -126,7 +175,7 @@ feature 'Polls' do scenario 'View results available for expired polls' do poll = create(:poll, :expired, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -136,7 +185,7 @@ feature 'Polls' do scenario 'View results redirects to results in public zone', js: true do poll = create(:poll, :expired, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do @@ -150,7 +199,7 @@ feature 'Polls' do scenario 'Poll card' do poll = create(:poll, :expired, related: proposal) - + visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do From 5e98c23be5c24b5e5814a8e043a2664d339642ee Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 21 Mar 2019 10:48:25 +0100 Subject: [PATCH 6/8] Fix HTML markup We cannot use 'id' html attributes on nested answers because there will be many answers form each question so this would have generated invalid HTML. --- app/views/dashboard/polls/_form.html.erb | 9 ++---- .../polls/_question_answer_fields.html.erb | 2 +- .../dashboard/polls/_question_fields.html.erb | 9 ++---- spec/features/dashboard/polls_spec.rb | 30 +++++++++---------- 4 files changed, 22 insertions(+), 28 deletions(-) diff --git a/app/views/dashboard/polls/_form.html.erb b/app/views/dashboard/polls/_form.html.erb index da0ab6e32..fbdaf9265 100644 --- a/app/views/dashboard/polls/_form.html.erb +++ b/app/views/dashboard/polls/_form.html.erb @@ -25,18 +25,15 @@
-
+
<%= f.fields_for :questions do |question| %> <%= render 'question_fields', f: question %> <% end %> - diff --git a/app/views/dashboard/polls/_question_answer_fields.html.erb b/app/views/dashboard/polls/_question_answer_fields.html.erb index ff41fb479..4f3dfae57 100644 --- a/app/views/dashboard/polls/_question_answer_fields.html.erb +++ b/app/views/dashboard/polls/_question_answer_fields.html.erb @@ -1,4 +1,4 @@ -
+
<%= f.hidden_field :given_order %>
diff --git a/app/views/dashboard/polls/_question_fields.html.erb b/app/views/dashboard/polls/_question_fields.html.erb index 671bfa50e..d24fa511f 100644 --- a/app/views/dashboard/polls/_question_fields.html.erb +++ b/app/views/dashboard/polls/_question_fields.html.erb @@ -12,18 +12,15 @@
-
+
<%= f.fields_for :question_answers do |answer| %> <%= render 'question_answer_fields', f: answer %> <% end %> - diff --git a/spec/features/dashboard/polls_spec.rb b/spec/features/dashboard/polls_spec.rb index 052766c16..add203101 100644 --- a/spec/features/dashboard/polls_spec.rb +++ b/spec/features/dashboard/polls_spec.rb @@ -104,51 +104,51 @@ feature 'Polls' do end end - scenario 'Edit poll should allow to remove questions', :js do + scenario "Edit poll should allow to remove questions", :js do poll = create(:poll, :incoming, related: proposal) - question1 = create(:poll_question, poll: poll) - question2 = create(:poll_question, poll: poll) + create(:poll_question, poll: poll) + create(:poll_question, poll: poll) visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do - click_link 'Edit survey' + click_link "Edit survey" end - within "#questions" do + within ".js-questions" do expect(page).to have_css ".nested-fields", count: 2 within first(".nested-fields") do - find('a.delete').click + find("a.delete").click end expect(page).to have_css ".nested-fields", count: 1 end - click_button 'Update poll' + click_button "Update poll" visit edit_proposal_dashboard_poll_path(proposal, poll) expect(page).to have_css ".nested-fields", count: 1 end - scenario 'Edit poll should allow to remove answers', :js do + scenario "Edit poll should allow to remove answers", :js do poll = create(:poll, :incoming, related: proposal) question = create(:poll_question, poll: poll) - answer1 = create(:poll_question_answer, question: question) - answer2 = create(:poll_question_answer, question: question) + create(:poll_question_answer, question: question) + create(:poll_question_answer, question: question) visit proposal_dashboard_polls_path(proposal) within "div#poll_#{poll.id}" do - click_link 'Edit survey' + click_link "Edit survey" end - within "#questions #answers" do + within ".js-questions .js-answers" do expect(page).to have_css ".nested-fields", count: 2 within first(".nested-fields") do - find('a.delete').click + find("a.delete").click end expect(page).to have_css ".nested-fields", count: 1 end - click_button 'Update poll' + click_button "Update poll" visit edit_proposal_dashboard_poll_path(proposal, poll) - within "#questions #answers" do + within ".js-questions .js-answers" do expect(page).to have_css ".nested-fields", count: 1 end end From 71887f291142da192b05f3b455a5eaddfc03e78d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 21 Mar 2019 14:03:38 +0100 Subject: [PATCH 7/8] Initialize answers 'after-insert' callback to keep answers order Also provide a function to initialize new set of answers 'affer-insert' callback after adding new questions. --- app/assets/javascripts/answers.js.coffee | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/app/assets/javascripts/answers.js.coffee b/app/assets/javascripts/answers.js.coffee index c36d09f12..183e1c628 100644 --- a/app/assets/javascripts/answers.js.coffee +++ b/app/assets/javascripts/answers.js.coffee @@ -1,9 +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: -> - $('.nested-answers').on 'cocoon:after-insert', (e, insertedItem) -> - nestedAnswersCount = $("input[type='hidden'][name$='[given_order]']").size() - $(insertedItem).find("input[type='hidden'][name$='[given_order]']").val(nestedAnswersCount) + $('.js-answers').each (index, answers) -> + App.Answers.initializeAnswers(answers) initialize: -> App.Answers.nestedAnswers() From b417e9db31b83a1ce4e87c5a91f9ad892b31a322 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Sen=C3=A9n=20Rodero=20Rodr=C3=ADguez?= Date: Thu, 21 Mar 2019 14:04:57 +0100 Subject: [PATCH 8/8] Initialize new question answers coccon callback After adding new question we need to initialize new answers sets. --- app/assets/javascripts/application.js | 2 ++ app/assets/javascripts/questions.js.coffee | 8 ++++++++ 2 files changed, 10 insertions(+) create mode 100644 app/assets/javascripts/questions.js.coffee diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index ec9b05b7b..82b40edc9 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -60,6 +60,7 @@ //= require markdown_editor //= require cocoon //= require answers +//= require questions //= require legislation_admin //= require legislation //= require legislation_allegations @@ -89,6 +90,7 @@ 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()