From 6bbbaf4580cc4ec20cad714fa45f5e7c2a1f5113 Mon Sep 17 00:00:00 2001 From: Mark Moser Date: Fri, 19 Aug 2016 16:02:18 -0500 Subject: [PATCH] admin questions work --- app/assets/javascripts/admin.js | 10 ++ app/assets/javascripts/application.js | 3 + app/controllers/admin/question_controller.rb | 31 +++++ app/helpers/application_helper.rb | 9 ++ app/models/question.rb | 5 + app/views/admin/dashboard.html.erb | 2 +- app/views/admin/question/_checkbox.html.erb | 18 +++ app/views/admin/question/_form.html.erb | 49 ++++++++ app/views/admin/question/_live_code.html.erb | 29 +++++ app/views/admin/question/_radio.html.erb | 18 +++ app/views/admin/question/_table_list.html.erb | 21 ++++ app/views/admin/question/_text.html.erb | 0 app/views/admin/question/edit.html.erb | 11 +- app/views/admin/question/index.html.erb | 12 ++ app/views/admin/question/new.html.erb | 9 +- app/views/admin/question/view.html.erb | 38 +++++- app/views/admin/quiz/edit.html.erb | 2 +- app/views/admin/quiz/index.html.erb | 2 +- app/views/admin/quiz/new.html.erb | 2 +- app/views/admin/quiz/view.html.erb | 7 +- app/views/admin/user/edit.html.erb | 2 +- app/views/admin/user/index.html.erb | 2 +- app/views/admin/user/new.html.erb | 2 +- app/views/admin/user/view.html.erb | 2 +- app/views/layouts/admin.html.erb | 1 + app/views/quiz/_live_code.html.erb | 2 +- .../admin/question_controller_test.rb | 111 ++++++++++++------ 27 files changed, 349 insertions(+), 51 deletions(-) create mode 100644 app/assets/javascripts/admin.js create mode 100644 app/views/admin/question/_checkbox.html.erb create mode 100644 app/views/admin/question/_form.html.erb create mode 100644 app/views/admin/question/_live_code.html.erb create mode 100644 app/views/admin/question/_radio.html.erb create mode 100644 app/views/admin/question/_table_list.html.erb create mode 100644 app/views/admin/question/_text.html.erb diff --git a/app/assets/javascripts/admin.js b/app/assets/javascripts/admin.js new file mode 100644 index 0000000..de154bc --- /dev/null +++ b/app/assets/javascripts/admin.js @@ -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(); + }); + +}); diff --git a/app/assets/javascripts/application.js b/app/assets/javascripts/application.js index e716f6f..3cfbc1b 100644 --- a/app/assets/javascripts/application.js +++ b/app/assets/javascripts/application.js @@ -21,3 +21,6 @@ //= require summary-edit //= require textarea-limit //= require live-coder + + +//= require admin diff --git a/app/controllers/admin/question_controller.rb b/app/controllers/admin/question_controller.rb index 2d49a92..33ecdd1 100644 --- a/app/controllers/admin/question_controller.rb +++ b/app/controllers/admin/question_controller.rb @@ -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 diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index cd6c340..876a05c 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -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 diff --git a/app/models/question.rb b/app/models/question.rb index 3abd437..9258649 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -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 diff --git a/app/views/admin/dashboard.html.erb b/app/views/admin/dashboard.html.erb index b69057c..c19052d 100644 --- a/app/views/admin/dashboard.html.erb +++ b/app/views/admin/dashboard.html.erb @@ -2,7 +2,7 @@ content_for :section_title, "Admin Dashboard" %> -
+

Quizzes

<%= render partial: 'admin/quiz/table_list', locals: { quizzes: @quizzes } %> diff --git a/app/views/admin/question/_checkbox.html.erb b/app/views/admin/question/_checkbox.html.erb new file mode 100644 index 0000000..7c3db76 --- /dev/null +++ b/app/views/admin/question/_checkbox.html.erb @@ -0,0 +1,18 @@ +Checkbox Options + +
    + <% question.input_options.each do | option | %> +
  • + <%= text_field_tag 'question[input_options][]', option, { disabled: (disable ||= false), data: { last: option } } %> +
  • + <% end %> +
+ +<% unless (disable ||= false) %> +
+
Add option
+
  • + <%= text_field_tag 'question[input_options][]', nil, { disabled: (disable ||= false), data: { last: nil } } %> +
  • +
    +<% end %> diff --git a/app/views/admin/question/_form.html.erb b/app/views/admin/question/_form.html.erb new file mode 100644 index 0000000..a3afa87 --- /dev/null +++ b/app/views/admin/question/_form.html.erb @@ -0,0 +1,49 @@ +<% if flash[:error].present? %> +
    + <%= flash[:error] %> +

    + <% question.errors.messages.each do |k,v| %> + <%= "#{k.to_s} #{v.join(' and ')}" %>
    + <% end %> +

    +
    +<% end %> + +<%= form_for question, url: action do |form| %> +
    + <%= 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) %> +
    + +
    + <%= form.label :category, 'Category' %> + <%= form.text_field :category %> +
    + +
    + <%= form.label :sort, 'Sort' %> + <%= form.text_field :sort %> +
    + +
    + <%= form.check_box :active %> + <%= form.label :active, 'Active' %> +
    + +
    + <%= form.label :question, "Question" %> + <%= form.text_area :question %> +
    + +
    + <%= form.label :input_type, 'Input Type' %> + <%= form.select :input_type, question_type_options(question.input_type), include_blank: false %> +
    + + <%= 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/_live_code.html.erb b/app/views/admin/question/_live_code.html.erb new file mode 100644 index 0000000..e2820f8 --- /dev/null +++ b/app/views/admin/question/_live_code.html.erb @@ -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' => '' } +%> + + +
    +
    + + <%= text_area_tag 'question[input_options][html]', options['html'], { data: {id: 'code-html', last: options['html']}, class: 'code-answer code-html' } %> +
    + +
    + + <%= text_area_tag 'question[input_options][css]', options['css'], { data: {id: 'code-css', last: options['css']}, class: 'code-answer code-css' } %> +
    + +
    + + <%= text_area_tag 'question[input_options][js]', options['js'], { data: {id: 'code-js', last: options['js']}, class: 'code-answer code-js' } %> +
    + +
    +
    + diff --git a/app/views/admin/question/_radio.html.erb b/app/views/admin/question/_radio.html.erb new file mode 100644 index 0000000..6343cdc --- /dev/null +++ b/app/views/admin/question/_radio.html.erb @@ -0,0 +1,18 @@ +Radio Options + +
      + <% question.input_options.each do | option | %> +
    • + <%= text_field_tag 'question[input_options][]', option, { disabled: (disable ||= false), data: { last: option } } %> +
    • + <% end %> +
    + +<% unless (disable ||= false) %> +
    +
    Add option
    +
  • + <%= text_field_tag 'question[input_options][]', nil, { disabled: (disable ||= false), data: { last: nil } } %> +
  • +
    +<% end %> diff --git a/app/views/admin/question/_table_list.html.erb b/app/views/admin/question/_table_list.html.erb new file mode 100644 index 0000000..1ce0359 --- /dev/null +++ b/app/views/admin/question/_table_list.html.erb @@ -0,0 +1,21 @@ + + + + + + + + + + + <% questions.each do |question| %> + + + + + + + + + <% end %> +
    SortQuestionTypeCategoryActive
    <%= question.sort %><%= question.question %><%= question.input_type %><%= question.category %><%= question.active unless question.active? %><%= link_to 'Edit', admin_edit_question_path(question.to_i), { class: 'btn tertiary-btn' } %>
    diff --git a/app/views/admin/question/_text.html.erb b/app/views/admin/question/_text.html.erb new file mode 100644 index 0000000..e69de29 diff --git a/app/views/admin/question/edit.html.erb b/app/views/admin/question/edit.html.erb index 34ed55e..baea6e8 100644 --- a/app/views/admin/question/edit.html.erb +++ b/app/views/admin/question/edit.html.erb @@ -1,2 +1,9 @@ -

    Admin::Questions#edit

    -

    Find me in app/views/admin/questions/edit.html.erb

    +<% + content_for :section_title, "Questions" +%> + +
    +

    <%= @question.quiz.name %>

    + + <%= render partial: 'form', locals: {question: @question, action: admin_update_question_path } %> +
    diff --git a/app/views/admin/question/index.html.erb b/app/views/admin/question/index.html.erb index e69de29..8ec115b 100644 --- a/app/views/admin/question/index.html.erb +++ b/app/views/admin/question/index.html.erb @@ -0,0 +1,12 @@ +<% + content_for :section_title, "Questions" +%> + +
    + <% quizzes = @questions.group_by{ |q| q.quiz.name } %> + <% quizzes.each do |quiz, questions| %> +

    <%= quiz %>

    + <%= render partial: 'admin/question/table_list', locals: { questions: questions } %> + <%= link_to('Edit Quiz', admin_quiz_path(questions.first.quiz.to_i), { class: 'btn' }) %> + <% end %> +
    diff --git a/app/views/admin/question/new.html.erb b/app/views/admin/question/new.html.erb index f4507f7..8bc7a68 100644 --- a/app/views/admin/question/new.html.erb +++ b/app/views/admin/question/new.html.erb @@ -1,2 +1,7 @@ -

    Admin::Questions#new

    -

    Find me in app/views/admin/questions/new.html.erb

    +<% + content_for :section_title, "New Question" +%> + +
    + <%= render partial: 'form', locals: {question: @question, action: admin_create_question_path } %> +
    diff --git a/app/views/admin/question/view.html.erb b/app/views/admin/question/view.html.erb index fea4846..8cfce12 100644 --- a/app/views/admin/question/view.html.erb +++ b/app/views/admin/question/view.html.erb @@ -1,2 +1,36 @@ -

    Admin::Questions#view

    -

    Find me in app/views/admin/questions/view.html.erb

    +<% + content_for :section_title, "Question for #{@question.quiz.name}" +%> + +
    + + + + + + + + + + + + + + + + + +
    Category<%= @question.category %>
    Type<%= @question.input_type %>
    Sort<%= @question.sort %>
    + <%= check_box_tag 'question_active', nil, @question.active?, {disabled: true} %> + <%= label_tag 'question_active', 'Active' %> +
    + + Question +

    <%= @question.question %>

    + + <%= 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' }) %> +
    diff --git a/app/views/admin/quiz/edit.html.erb b/app/views/admin/quiz/edit.html.erb index f0435b9..a67e234 100644 --- a/app/views/admin/quiz/edit.html.erb +++ b/app/views/admin/quiz/edit.html.erb @@ -2,6 +2,6 @@ content_for :section_title, "Edit: #{@quiz.name}" %> -
    +
    <%= render partial: 'form', locals: { quiz: @quiz, action: admin_update_quiz_path } %>
    diff --git a/app/views/admin/quiz/index.html.erb b/app/views/admin/quiz/index.html.erb index 4b1af23..70b8cf2 100644 --- a/app/views/admin/quiz/index.html.erb +++ b/app/views/admin/quiz/index.html.erb @@ -2,7 +2,7 @@ content_for :section_title, "Quizzes" %> -
    +
    <%= render partial: 'admin/quiz/table_list', locals: { quizzes: @quizzes } %> <%= link_to('New Quiz', admin_new_quiz_path, { class: 'btn' }) %>
    diff --git a/app/views/admin/quiz/new.html.erb b/app/views/admin/quiz/new.html.erb index 639e477..2d0dd11 100644 --- a/app/views/admin/quiz/new.html.erb +++ b/app/views/admin/quiz/new.html.erb @@ -2,6 +2,6 @@ content_for :section_title, "New Quiz" %> -
    +
    <%= render partial: 'form', locals: { quiz: @quiz, action: admin_create_quiz_path } %>
    diff --git a/app/views/admin/quiz/view.html.erb b/app/views/admin/quiz/view.html.erb index 7047ca1..21eb321 100644 --- a/app/views/admin/quiz/view.html.erb +++ b/app/views/admin/quiz/view.html.erb @@ -2,9 +2,14 @@ content_for :section_title, "#{@quiz.name}" %> -
    +

    <%= @quiz.name %>

    <%= @quiz.dept %>

    <%= @quiz.unit %>

    <%= link_to('Edit', admin_edit_quiz_path(@quiz.to_i), { class: 'btn' }) %>
    + +
    + <%= render partial: 'admin/question/table_list', locals: { questions: @quiz.questions, disable: true } %> + <%= link_to('New Question', admin_new_question_path, { class: 'btn' }) %> +
    diff --git a/app/views/admin/user/edit.html.erb b/app/views/admin/user/edit.html.erb index 7d30c1a..0ccc724 100644 --- a/app/views/admin/user/edit.html.erb +++ b/app/views/admin/user/edit.html.erb @@ -2,6 +2,6 @@ content_for :section_title, "Edit: #{@user.name}" %> -
    +
    <%= render partial: 'form', locals: {user: @user, action: admin_update_user_path } %>
    diff --git a/app/views/admin/user/index.html.erb b/app/views/admin/user/index.html.erb index 0d25eb9..3092e28 100644 --- a/app/views/admin/user/index.html.erb +++ b/app/views/admin/user/index.html.erb @@ -2,7 +2,7 @@ content_for :section_title, "Users" %> -
    +

    Users

    <%= render partial: 'admin/user/table_list', locals: { users: @users } %> <%= link_to('New User', admin_new_user_path, { class: 'btn' }) %> diff --git a/app/views/admin/user/new.html.erb b/app/views/admin/user/new.html.erb index 16906ea..dc70d47 100644 --- a/app/views/admin/user/new.html.erb +++ b/app/views/admin/user/new.html.erb @@ -2,6 +2,6 @@ content_for :section_title, "New User" %> -
    +
    <%= render partial: 'form', locals: {user: @user, action: admin_create_user_path } %>
    diff --git a/app/views/admin/user/view.html.erb b/app/views/admin/user/view.html.erb index 3890172..caf2c25 100644 --- a/app/views/admin/user/view.html.erb +++ b/app/views/admin/user/view.html.erb @@ -2,7 +2,7 @@ content_for :section_title, "#{@user.name}" %> -
    +

    <%= @user.name %>

    <%= mail_to(@user.email) %>

    <%= @user.role %>

    diff --git a/app/views/layouts/admin.html.erb b/app/views/layouts/admin.html.erb index 05fa14f..0beec5b 100644 --- a/app/views/layouts/admin.html.erb +++ b/app/views/layouts/admin.html.erb @@ -35,6 +35,7 @@
    +
    diff --git a/app/views/quiz/_live_code.html.erb b/app/views/quiz/_live_code.html.erb index 5e76e3a..9a3a344 100644 --- a/app/views/quiz/_live_code.html.erb +++ b/app/views/quiz/_live_code.html.erb @@ -37,7 +37,7 @@