From 488b89ddfa238eb29cd83aba878dc14f1ce5a1fe Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Mon, 22 Aug 2016 16:24:00 -0500 Subject: [PATCH] loads input option partials on type change --- app/assets/javascripts/admin.js | 8 +++++++- app/controllers/admin/question_controller.rb | 5 +++++ app/views/admin/question/_form.html.erb | 12 +++++++----- app/views/admin/question/options.html.erb | 5 +++++ app/views/admin/question/view.html.erb | 2 ++ config/routes.rb | 15 ++++++++------- .../controllers/admin/question_controller_test.rb | 15 +++++++++++++++ 7 files changed, 49 insertions(+), 13 deletions(-) create mode 100644 app/views/admin/question/options.html.erb diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js index de154bc..7633280 100644 --- a/app/assets/javascripts/admin.js +++ b/app/assets/javascripts/admin.js @@ -1,10 +1,16 @@ $(function(){ - $("[data-id=input_option_adder]").on('click', function(){ + $("form").on('click', "[data-id=input_option_adder]", function(){ var $new_li = $(this).siblings('li').clone(); $new_li.attr('style', ''); $("[data-id=input_option_list]").append($new_li); $new_li.find('input').focus(); }); + $("#question_input_type").on('change', function(){ + var qid = $(this).attr('data-qid') === undefined ? '' : "/" + $(this).attr('data-qid'); + // /admin/question(/:question_id)/options/:input_type + $("[data-id=input-options-wrapper]").load("/admin/question" + qid + "/options/" + $(this).val()); + }); + }); diff --git a/app/controllers/admin/question_controller.rb b/app/controllers/admin/question_controller.rb index e31c6d2..c066353 100644 --- a/app/controllers/admin/question_controller.rb +++ b/app/controllers/admin/question_controller.rb @@ -43,6 +43,11 @@ module Admin end end + def options + @question = params[:question_id].present? ? Question.find(params[:question_id]) : Question.new + render layout: false + end + private def question_params diff --git a/app/views/admin/question/_form.html.erb b/app/views/admin/question/_form.html.erb index 8574c00..20a6cdf 100644 --- a/app/views/admin/question/_form.html.erb +++ b/app/views/admin/question/_form.html.erb @@ -27,13 +27,15 @@
<%= form.label :input_type, 'Input Type' %> - <%= form.select :input_type, question_type_options(question.input_type), include_blank: false %> + <%= form.select :input_type, question_type_options(question.input_type), { include_blank: false }, { data: { qid: question.id } } %>
- <%= fields_for @question do |fields| %> - <% partial = question.input_type.blank? ? 'admin/question/text' : "admin/question/#{question.input_type}" %> - <%= render partial: partial, locals: {question: question, fields: fields } %> - <% end %> +
+ <%= fields_for @question do |fields| %> + <% partial = question.input_type.blank? ? 'admin/question/text' : "admin/question/#{question.input_type}" %> + <%= render partial: partial, locals: {question: question, fields: fields } %> + <% end %> +
<%= form.submit %> <% end %> diff --git a/app/views/admin/question/options.html.erb b/app/views/admin/question/options.html.erb new file mode 100644 index 0000000..352995a --- /dev/null +++ b/app/views/admin/question/options.html.erb @@ -0,0 +1,5 @@ +<% + if lookup_context.exists?(params[:input_type], 'admin/question', true) + %><%= render partial: "admin/question/#{params[:input_type]}", locals: { question: @question } %><% + end +%> diff --git a/app/views/admin/question/view.html.erb b/app/views/admin/question/view.html.erb index 8cfce12..24bba3e 100644 --- a/app/views/admin/question/view.html.erb +++ b/app/views/admin/question/view.html.erb @@ -33,4 +33,6 @@ <% end %> <%= link_to('Edit', admin_edit_question_path(@question.to_i), { class: 'btn' }) %> + + <%= link_to('View Quiz', admin_quiz_path(@question.quiz_id), { class: 'btn' }) %> diff --git a/config/routes.rb b/config/routes.rb index 7f0cb37..2268dc6 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -21,13 +21,14 @@ Rails.application.routes.draw do post "/admin/user/:user_id/edit", to: "admin/user#update", as: :admin_update_user patch "/admin/user/:user_id/edit", to: "admin/user#update" - get "/admin/questions", to: "admin/question#index", as: :admin_questions - get "/admin/question/new", to: "admin/question#new", as: :admin_new_question - post "/admin/question/new", to: "admin/question#create", as: :admin_create_question - get "/admin/question/:question_id", to: "admin/question#view", as: :admin_question - get "/admin/question/:question_id/edit", to: "admin/question#edit", as: :admin_edit_question - post "/admin/question/:question_id/edit", to: "admin/question#update", as: :admin_update_question - patch "/admin/question/:question_id/edit", to: "admin/question#update" + get "/admin/questions", to: "admin/question#index", as: :admin_questions + get "/admin/question/new", to: "admin/question#new", as: :admin_new_question + post "/admin/question/new", to: "admin/question#create", as: :admin_create_question + get "/admin/question(/:question_id)/options/:input_type", to: "admin/question#options", as: :admin_question_option_form + get "/admin/question/:question_id", to: "admin/question#view", as: :admin_question + get "/admin/question/:question_id/edit", to: "admin/question#edit", as: :admin_edit_question + post "/admin/question/:question_id/edit", to: "admin/question#update", as: :admin_update_question + patch "/admin/question/:question_id/edit", to: "admin/question#update" ######################################################################################### diff --git a/test/controllers/admin/question_controller_test.rb b/test/controllers/admin/question_controller_test.rb index 6c5e4d7..a629bea 100644 --- a/test/controllers/admin/question_controller_test.rb +++ b/test/controllers/admin/question_controller_test.rb @@ -72,5 +72,20 @@ module Admin assert :success assert_match(/failed/i, session[:flash].values.join) end + + test "should gracefully fail input_type" do + get admin_question_option_form_url(input_type: 'fooBarBaz') + + assert :success + assigns :locals + end + + test "should return partial for new radio" do + get admin_question_option_form_url(input_type: 'radio') + + assert :success + assigns :locals + assert_select "input[id^=question_multi_choice_]" + end end end