loads input option partials on type change

This commit is contained in:
Mark Moser 2016-08-22 16:24:00 -05:00
parent bdec83b3b6
commit 488b89ddfa
7 changed files with 49 additions and 13 deletions

View File

@ -1,10 +1,16 @@
$(function(){ $(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(); var $new_li = $(this).siblings('li').clone();
$new_li.attr('style', ''); $new_li.attr('style', '');
$("[data-id=input_option_list]").append($new_li); $("[data-id=input_option_list]").append($new_li);
$new_li.find('input').focus(); $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());
});
}); });

View File

@ -43,6 +43,11 @@ module Admin
end end
end end
def options
@question = params[:question_id].present? ? Question.find(params[:question_id]) : Question.new
render layout: false
end
private private
def question_params def question_params

View File

@ -27,13 +27,15 @@
<div class="form-group"> <div class="form-group">
<%= form.label :input_type, 'Input Type' %> <%= 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 } } %>
</div> </div>
<div class="form-group" data-id="input-options-wrapper">
<%= fields_for @question do |fields| %> <%= fields_for @question do |fields| %>
<% partial = question.input_type.blank? ? 'admin/question/text' : "admin/question/#{question.input_type}" %> <% partial = question.input_type.blank? ? 'admin/question/text' : "admin/question/#{question.input_type}" %>
<%= render partial: partial, locals: {question: question, fields: fields } %> <%= render partial: partial, locals: {question: question, fields: fields } %>
<% end %> <% end %>
</div>
<%= form.submit %> <%= form.submit %>
<% end %> <% end %>

View File

@ -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
%>

View File

@ -33,4 +33,6 @@
<% end %> <% end %>
<%= link_to('Edit', admin_edit_question_path(@question.to_i), { class: 'btn' }) %> <%= link_to('Edit', admin_edit_question_path(@question.to_i), { class: 'btn' }) %>
<%= link_to('View Quiz', admin_quiz_path(@question.quiz_id), { class: 'btn' }) %>
</main> </main>

View File

@ -24,6 +24,7 @@ Rails.application.routes.draw do
get "/admin/questions", to: "admin/question#index", as: :admin_questions get "/admin/questions", to: "admin/question#index", as: :admin_questions
get "/admin/question/new", to: "admin/question#new", as: :admin_new_question get "/admin/question/new", to: "admin/question#new", as: :admin_new_question
post "/admin/question/new", to: "admin/question#create", as: :admin_create_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", to: "admin/question#view", as: :admin_question
get "/admin/question/:question_id/edit", to: "admin/question#edit", as: :admin_edit_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 post "/admin/question/:question_id/edit", to: "admin/question#update", as: :admin_update_question

View File

@ -72,5 +72,20 @@ module Admin
assert :success assert :success
assert_match(/failed/i, session[:flash].values.join) assert_match(/failed/i, session[:flash].values.join)
end 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
end end