working on question_lists

This commit is contained in:
Jared Cosulich 2017-03-05 12:23:11 -05:00
parent d731049fc6
commit cc84c3bbda
26 changed files with 498 additions and 10 deletions

View file

@ -0,0 +1,3 @@
# Place all the behaviors and hooks related to the matching controller here.
# All this logic will automatically be available in application.js.
# You can use CoffeeScript in this file: http://coffeescript.org/

View file

@ -0,0 +1,3 @@
// Place all the styles related to the QuestionLists controller here.
// They will automatically be included in application.css.
// You can use Sass (SCSS) here: http://sass-lang.com/

View file

@ -0,0 +1,74 @@
class QuestionListsController < ApplicationController
before_action :set_question_list, only: [:show, :edit, :update, :destroy]
# GET /question_lists
# GET /question_lists.json
def index
@question_lists = QuestionList.all
end
# GET /question_lists/1
# GET /question_lists/1.json
def show
end
# GET /question_lists/new
def new
@question_list = QuestionList.new
end
# GET /question_lists/1/edit
def edit
end
# POST /question_lists
# POST /question_lists.json
def create
@question_list = QuestionList.new(question_list_params)
respond_to do |format|
if @question_list.save
format.html { redirect_to @question_list, notice: 'Question list was successfully created.' }
format.json { render :show, status: :created, location: @question_list }
else
format.html { render :new }
format.json { render json: @question_list.errors, status: :unprocessable_entity }
end
end
end
# PATCH/PUT /question_lists/1
# PATCH/PUT /question_lists/1.json
def update
respond_to do |format|
if @question_list.update(question_list_params)
format.html { redirect_to @question_list, notice: 'Question list was successfully updated.' }
format.json { render :show, status: :ok, location: @question_list }
else
format.html { render :edit }
format.json { render json: @question_list.errors, status: :unprocessable_entity }
end
end
end
# DELETE /question_lists/1
# DELETE /question_lists/1.json
def destroy
@question_list.destroy
respond_to do |format|
format.html { redirect_to question_lists_url, notice: 'Question list was successfully destroyed.' }
format.json { head :no_content }
end
end
private
# Use callbacks to share common setup or constraints between actions.
def set_question_list
@question_list = QuestionList.find(params[:id])
end
# Never trust parameters from the scary internet, only allow the white list through.
def question_list_params
params.require(:question_list).permit(:name, :description, question_id_array: [])
end
end

View file

@ -0,0 +1,2 @@
module QuestionListsHelper
end

View file

@ -0,0 +1,26 @@
class QuestionList < ApplicationRecord
validates :name, presence: true
validates :question_ids, presence: true
attr_accessor :question_id_array
before_validation :convert_question_id_array
after_initialize :set_question_id_array
def questions
question_id_array.collect { |id| Question.where(id: id).first }
end
private
def convert_question_id_array
return if question_id_array.blank?
self.question_ids = question_id_array.reject { |id| id.to_s.empty? }.join(',')
end
def set_question_id_array
return if question_ids.blank?
self.question_id_array = question_ids.split(',').map(&:to_i)
end
end

View file

@ -1,3 +1,3 @@
- categories = [category]
- categories << category while (category = category.parent_category)
- categories << category while (category = category.parent_category).present?
= categories.reverse.map { |category| link_to(category.name, category) }.join('&nbsp; | &nbsp;').html_safe

View file

@ -0,0 +1,20 @@
= form_for(question_list) do |f|
- if question_list.errors.any?
#error_explanation
%h2
= pluralize(question_list.errors.count, "error")
prohibited this question_list from being saved:
%ul
- question_list.errors.full_messages.each do |message|
%li= message
.field
= f.label :name
= f.text_field :name
.field
= f.label :description
= f.text_area :description
.field
= f.label :question_ids
= f.text_area :question_ids
.actions
= f.submit

View file

@ -0,0 +1,5 @@
%h1 Editing Question List
= render 'form', question_list: @question_list
= link_to 'Show', @question_list
|
= link_to 'Back', question_lists_path

View file

@ -0,0 +1,20 @@
%p#notice= notice
%h1 Question Lists
%table
%thead
%tr
%th Name
%th Description
%th Question ids
%th{:colspan => "3"}
%tbody
- @question_lists.each do |question_list|
%tr
%td= question_list.name
%td= question_list.description
%td= question_list.question_ids
%td= link_to 'Show', question_list
%td= link_to 'Edit', edit_question_list_path(question_list)
%td= link_to 'Destroy', question_list, method: :delete, data: { confirm: 'Are you sure?' }
%br/
= link_to 'New Question List', new_question_list_path

View file

@ -0,0 +1,4 @@
json.array!(@question_lists) do |question_list|
json.extract! question_list, :id, :name, :description, :question_ids
json.url question_list_url(question_list, format: :json)
end

View file

@ -0,0 +1,3 @@
%h1 New Question List
= render 'form', question_list: @question_list
= link_to 'Back', question_lists_path

View file

@ -0,0 +1,13 @@
%p#notice= notice
%p
%strong Name:
= @question_list.name
%p
%strong Description:
= @question_list.description
%p
%strong Question ids:
= @question_list.question_ids
= link_to 'Edit', edit_question_list_path(@question_list)
|
= link_to 'Back', question_lists_path

View file

@ -0,0 +1 @@
json.extract! @question_list, :id, :name, :description, :question_ids, :created_at, :updated_at

View file

@ -20,7 +20,10 @@
%p
%strong Category:
&nbsp;
= render partial: 'categories/full_path', locals: {category: @question.category}
- if @question.category.present?
= render partial: 'categories/full_path', locals: {category: @question.category}
- else
No Category
= link_to 'Edit', edit_question_path(@question)
|