loads input option partials on type change
This commit is contained in:
parent
bdec83b3b6
commit
488b89ddfa
@ -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());
|
||||||
|
});
|
||||||
|
|
||||||
});
|
});
|
||||||
|
@ -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
|
||||||
|
@ -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>
|
||||||
|
|
||||||
<%= fields_for @question do |fields| %>
|
<div class="form-group" data-id="input-options-wrapper">
|
||||||
<% partial = question.input_type.blank? ? 'admin/question/text' : "admin/question/#{question.input_type}" %>
|
<%= fields_for @question do |fields| %>
|
||||||
<%= render partial: partial, locals: {question: question, fields: fields } %>
|
<% partial = question.input_type.blank? ? 'admin/question/text' : "admin/question/#{question.input_type}" %>
|
||||||
<% end %>
|
<%= render partial: partial, locals: {question: question, fields: fields } %>
|
||||||
|
<% end %>
|
||||||
|
</div>
|
||||||
|
|
||||||
<%= form.submit %>
|
<%= form.submit %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
5
app/views/admin/question/options.html.erb
Normal file
5
app/views/admin/question/options.html.erb
Normal 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
|
||||||
|
%>
|
@ -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>
|
||||||
|
@ -21,13 +21,14 @@ Rails.application.routes.draw do
|
|||||||
post "/admin/user/:user_id/edit", to: "admin/user#update", as: :admin_update_user
|
post "/admin/user/:user_id/edit", to: "admin/user#update", as: :admin_update_user
|
||||||
patch "/admin/user/:user_id/edit", to: "admin/user#update"
|
patch "/admin/user/:user_id/edit", to: "admin/user#update"
|
||||||
|
|
||||||
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", to: "admin/question#view", as: :admin_question
|
get "/admin/question(/:question_id)/options/:input_type", to: "admin/question#options", as: :admin_question_option_form
|
||||||
get "/admin/question/:question_id/edit", to: "admin/question#edit", as: :admin_edit_question
|
get "/admin/question/:question_id", to: "admin/question#view", as: :admin_question
|
||||||
post "/admin/question/:question_id/edit", to: "admin/question#update", as: :admin_update_question
|
get "/admin/question/:question_id/edit", to: "admin/question#edit", as: :admin_edit_question
|
||||||
patch "/admin/question/:question_id/edit", to: "admin/question#update"
|
post "/admin/question/:question_id/edit", to: "admin/question#update", as: :admin_update_question
|
||||||
|
patch "/admin/question/:question_id/edit", to: "admin/question#update"
|
||||||
|
|
||||||
#########################################################################################
|
#########################################################################################
|
||||||
|
|
||||||
|
@ -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
|
||||||
|
Loading…
Reference in New Issue
Block a user