admin questions work

This commit is contained in:
Mark Moser 2016-08-19 16:02:18 -05:00
parent 63701c8247
commit 6bbbaf4580
27 changed files with 349 additions and 51 deletions

View File

@ -0,0 +1,10 @@
$(function(){
$("[data-id=input_option_adder]").on('click', 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();
});
});

View File

@ -21,3 +21,6 @@
//= require summary-edit
//= require textarea-limit
//= require live-coder
//= require admin

View File

@ -1,21 +1,52 @@
module Admin
class QuestionController < AdminController
def index
@questions = Question.includes(:quiz).order("quizzes.name", { active: :desc }, :sort)
end
def new
@question = Question.new(active: true)
@quizzes = Quiz.all
end
def create
@quizzes = Quiz.all
@question = Question.create(question_params)
if @question.persisted?
redirect_to admin_questions_path, flash: { notice: "Sucessfully created question" }
else
flash[:error] = "Failed to save question."
render :new
end
end
def view
@question = Question.includes(:quiz).find(params[:question_id])
end
def edit
@quizzes = Quiz.all
@question = Question.includes(:quiz).find(params[:question_id])
end
def update
@quizzes = Quiz.all
@question = Question.find(params[:question_id])
if @question.update_attributes(question_params)
redirect_to admin_question_path(@question.to_i),
flash: { notice: "Sucessfully updated question" }
else
flash[:error] = "Failed to update question."
render :edit
end
end
private
def question_params
params.require(:question).permit(:quiz_id, :question, :category, :input_type, :input_options, :sort)
end
end
end

View File

@ -17,4 +17,13 @@ module ApplicationHelper
%w(Admin admin)
], disabled: "-", selected: (val.blank? ? '' : val))
end
def question_type_options val
options_for_select([
%w(Text text),
%w(Radio radio),
%w(Checkbox checkbox),
%w(Coder live_code)
], selected: (val.blank? ? '' : val))
end
end

View File

@ -3,4 +3,9 @@ class Question < ApplicationRecord
has_many :answers
belongs_to :quiz
validates_presence_of :quiz_id
validates_presence_of :question
validates_presence_of :category
validates_presence_of :input_type
end

View File

@ -2,7 +2,7 @@
content_for :section_title, "Admin Dashboard"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<section>
<h1>Quizzes</h1>
<%= render partial: 'admin/quiz/table_list', locals: { quizzes: @quizzes } %>

View File

@ -0,0 +1,18 @@
<strong>Checkbox Options</strong>
<ul data-id="input_option_list">
<% question.input_options.each do | option | %>
<li>
<%= text_field_tag 'question[input_options][]', option, { disabled: (disable ||= false), data: { last: option } } %>
</li>
<% end %>
</ul>
<% unless (disable ||= false) %>
<div class="form-group">
<div class="btn tertiary-btn" data-id="input_option_adder"> Add option </div>
<li style="display: none;">
<%= text_field_tag 'question[input_options][]', nil, { disabled: (disable ||= false), data: { last: nil } } %>
</li>
</div>
<% end %>

View File

@ -0,0 +1,49 @@
<% if flash[:error].present? %>
<div class="error">
<%= flash[:error] %>
<p>
<% question.errors.messages.each do |k,v| %>
<%= "#{k.to_s} #{v.join(' and ')}" %><br />
<% end %>
</p>
</div>
<% end %>
<%= form_for question, url: action do |form| %>
<div class="form-group">
<%= form.label :quiz_id, 'Quiz' %>
<%= form.select :quiz_id, options_for_select(@quizzes.map{ |q| [q.name, q.id] }, question.quiz_id), include_blank: (@quizzes.size > 1) %>
</div>
<div class="form-group">
<%= form.label :category, 'Category' %>
<%= form.text_field :category %>
</div>
<div class="form-group">
<%= form.label :sort, 'Sort' %>
<%= form.text_field :sort %>
</div>
<div class="form-group">
<%= form.check_box :active %>
<%= form.label :active, 'Active' %>
</div>
<div class="form-group">
<%= form.label :question, "Question" %>
<%= form.text_area :question %>
</div>
<div class="form-group">
<%= form.label :input_type, 'Input Type' %>
<%= form.select :input_type, question_type_options(question.input_type), include_blank: false %>
</div>
<%= 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 %>

View File

@ -0,0 +1,29 @@
<%
return nil
# don't load this partial yet. Will finish in issue #16
# https://gitlab.perficientxd.com/pdr/skill-assessment-app/issues/16
options = { 'text' => '', 'html' => '', 'css' => '', 'js' => '' }
%>
<div data-id="live-coder-answer">
<div class="code-input">
<label for="question_input_options_html">HTML</label>
<%= text_area_tag 'question[input_options][html]', options['html'], { data: {id: 'code-html', last: options['html']}, class: 'code-answer code-html' } %>
</div>
<div class="code-input">
<label for="question_input_options_css">CSS</label>
<%= text_area_tag 'question[input_options][css]', options['css'], { data: {id: 'code-css', last: options['css']}, class: 'code-answer code-css' } %>
</div>
<div class="code-input">
<label for="question_input_options_js">JS</label>
<%= text_area_tag 'question[input_options][js]', options['js'], { data: {id: 'code-js', last: options['js']}, class: 'code-answer code-js' } %>
</div>
<div class="results" data-id="results"></div>
</div>

View File

@ -0,0 +1,18 @@
<strong>Radio Options</strong>
<ul data-id="input_option_list">
<% question.input_options.each do | option | %>
<li>
<%= text_field_tag 'question[input_options][]', option, { disabled: (disable ||= false), data: { last: option } } %>
</li>
<% end %>
</ul>
<% unless (disable ||= false) %>
<div class="form-group">
<div class="btn tertiary-btn" data-id="input_option_adder"> Add option </div>
<li style="display: none;">
<%= text_field_tag 'question[input_options][]', nil, { disabled: (disable ||= false), data: { last: nil } } %>
</li>
</div>
<% end %>

View File

@ -0,0 +1,21 @@
<table cellspacing="0" cellpadding="0">
<tr>
<th>Sort</th>
<th>Question</th>
<th>Type</th>
<th>Category</th>
<th>Active</th>
<th></th>
</tr>
<% questions.each do |question| %>
<tr>
<td><%= question.sort %></td>
<td><%= question.question %></td>
<td><%= question.input_type %></td>
<td><%= question.category %></td>
<td><%= question.active unless question.active? %></td>
<td><%= link_to 'Edit', admin_edit_question_path(question.to_i), { class: 'btn tertiary-btn' } %></td>
</tr>
<% end %>
</table>

View File

View File

@ -1,2 +1,9 @@
<h1>Admin::Questions#edit</h1>
<p>Find me in app/views/admin/questions/edit.html.erb</p>
<%
content_for :section_title, "Questions"
%>
<main class="admin_tpl">
<h1><%= @question.quiz.name %></h1>
<%= render partial: 'form', locals: {question: @question, action: admin_update_question_path } %>
</main>

View File

@ -0,0 +1,12 @@
<%
content_for :section_title, "Questions"
%>
<main class="admin_tpl">
<% quizzes = @questions.group_by{ |q| q.quiz.name } %>
<% quizzes.each do |quiz, questions| %>
<h1><%= quiz %></h1>
<%= render partial: 'admin/question/table_list', locals: { questions: questions } %>
<%= link_to('Edit Quiz', admin_quiz_path(questions.first.quiz.to_i), { class: 'btn' }) %>
<% end %>
</main>

View File

@ -1,2 +1,7 @@
<h1>Admin::Questions#new</h1>
<p>Find me in app/views/admin/questions/new.html.erb</p>
<%
content_for :section_title, "New Question"
%>
<main class="admin_tpl">
<%= render partial: 'form', locals: {question: @question, action: admin_create_question_path } %>
</main>

View File

@ -1,2 +1,36 @@
<h1>Admin::Questions#view</h1>
<p>Find me in app/views/admin/questions/view.html.erb</p>
<%
content_for :section_title, "Question for #{@question.quiz.name}"
%>
<main class="admin_tpl">
<table cellspacing="0" cellpadding="0">
<tr>
<th>Category</th>
<td><%= @question.category %></td>
</tr>
<tr>
<th>Type</th>
<td><%= @question.input_type %></td>
</tr>
<tr>
<th>Sort</th>
<td><%= @question.sort %></td>
</tr>
<tr>
<th></th>
<td>
<%= check_box_tag 'question_active', nil, @question.active?, {disabled: true} %>
<%= label_tag 'question_active', 'Active' %>
</td>
</tr>
</table>
<strong>Question</strong>
<p><%= @question.question %></p>
<%= fields_for @question do |fields| %>
<%= render partial: "admin/question/#{@question.input_type}", locals: {question: @question, disable: true, fields: fields } %>
<% end %>
<%= link_to('Edit', admin_edit_question_path(@question.to_i), { class: 'btn' }) %>
</main>

View File

@ -2,6 +2,6 @@
content_for :section_title, "Edit: #{@quiz.name}"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<%= render partial: 'form', locals: { quiz: @quiz, action: admin_update_quiz_path } %>
</main>

View File

@ -2,7 +2,7 @@
content_for :section_title, "Quizzes"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<%= render partial: 'admin/quiz/table_list', locals: { quizzes: @quizzes } %>
<%= link_to('New Quiz', admin_new_quiz_path, { class: 'btn' }) %>
</main>

View File

@ -2,6 +2,6 @@
content_for :section_title, "New Quiz"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<%= render partial: 'form', locals: { quiz: @quiz, action: admin_create_quiz_path } %>
</main>

View File

@ -2,9 +2,14 @@
content_for :section_title, "#{@quiz.name}"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<p><%= @quiz.name %></p>
<p><%= @quiz.dept %></p>
<p><%= @quiz.unit %></p>
<%= link_to('Edit', admin_edit_quiz_path(@quiz.to_i), { class: 'btn' }) %>
</main>
<main class="summary_tpl">
<%= render partial: 'admin/question/table_list', locals: { questions: @quiz.questions, disable: true } %>
<%= link_to('New Question', admin_new_question_path, { class: 'btn' }) %>
</main>

View File

@ -2,6 +2,6 @@
content_for :section_title, "Edit: #{@user.name}"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<%= render partial: 'form', locals: {user: @user, action: admin_update_user_path } %>
</main>

View File

@ -2,7 +2,7 @@
content_for :section_title, "Users"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<h1>Users</h1>
<%= render partial: 'admin/user/table_list', locals: { users: @users } %>
<%= link_to('New User', admin_new_user_path, { class: 'btn' }) %>

View File

@ -2,6 +2,6 @@
content_for :section_title, "New User"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<%= render partial: 'form', locals: {user: @user, action: admin_create_user_path } %>
</main>

View File

@ -2,7 +2,7 @@
content_for :section_title, "#{@user.name}"
%>
<main class="summary_tpl">
<main class="admin_tpl">
<p><%= @user.name %></p>
<p><%= mail_to(@user.email) %></p>
<p><%= @user.role %></p>

View File

@ -35,6 +35,7 @@
</div>
<footer>
<div class="footer_title">&nbsp;</div>
<div class="pd_logo"><%= image_tag("perficientdigital.png", alt:"Perficient Digital") %></div>
<div class="footer_yellow-bar slantleft slantright">&nbsp;</div>
</footer>

View File

@ -37,7 +37,7 @@
</div>
<div id="answer<%= question.question_id %>" data-id="live-coder-answer" style="display: none;">
<label for="answer_live_code">Enter answer here</label>
<label for="answer_live_code_text">Enter answer here</label>
<%= text_area_tag 'answer[live_code][text]', (answers['text']), { disabled: true, data: {last: answers['text']}} %>
<div class="code-input">

View File

@ -1,35 +1,76 @@
# require 'test_helper'
#
# module Admin
# class QuestionControllerTest < ActionDispatch::IntegrationTest
# test "should get index" do
# get admin_questions_url
# assert_response :success
# end
#
# test "should get new" do
# get admin_new_question_url
# assert_response :success
# end
#
# test "should post create" do
# post admin_create_question_url
# assert_response :success
# end
#
# test "should get view" do
# get admin_question_url questions(:fed5).to_i
# assert_response :success
# end
#
# test "should get edit" do
# get admin_edit_question_url questions(:fed5).to_i
# assert_response :success
# end
#
# test "should post update question" do
# post admin_update_question_url questions(:fed5).to_i
# assert_response :success
# end
# end
# end
require 'test_helper'
module Admin
class QuestionControllerTest < ActionDispatch::IntegrationTest
def setup
post admin_auth_url, params: { auth:
{ email: 'alan.admin@mailinator.com', password: 'password' } }
end
test "should get index" do
get admin_questions_url
assert_response :success
assert assigns :questions
end
test "should get new" do
get admin_new_question_url
assert_response :success
assert assigns :question
end
test "should fail create" do
assert_difference("Question.count", 0) do
post admin_create_question_url, params: { question: { question: 'foo bar baz' } }
end
assert :success
assert_match(/failed/i, session[:flash].values.join)
end
test "should fail to create without quiz id" do
assert_difference("Question.count", 0) do
post admin_create_question_url, params: { question:
{ question: 'foo bar baz', category: 'ops', input_type: 'text' } }
end
assert :success
assert_match(/failed/i, session[:flash].values.join)
end
test "should post create" do
assert_difference("Question.count", 1) do
post admin_create_question_url, params: { question:
{ quiz_id: quizzes(:fed).to_i, question: 'foo bar baz', category: 'ops', input_type: 'text' } }
end
assert_redirected_to admin_questions_url
end
test "should get view" do
get admin_question_url questions(:fed5).to_i
assert_response :success
assert assigns :question
end
test "should get edit" do
get admin_edit_question_url questions(:fed5).to_i
assert_response :success
assert assigns :question
end
test "should post update quiz" do
question = questions(:fed9)
post admin_update_question_url(question.to_i), params: { question:
{ quiz_id: quizzes(:fed).to_i, question: 'foo bar baz', category: 'ops', input_type: 'text' } }
assert_redirected_to admin_question_path(question.to_i)
get admin_question_path question.to_i
assert_select 'p', 'foo bar baz'
end
test "should fail to update question" do
question = questions(:fed9)
post admin_update_question_url(question.to_i), params: { question: { question: nil } }
assert :success
assert_match(/failed/i, session[:flash].values.join)
end
end
end