From 683fc31c5dfb6f91584a5fb90510e4cd5c240396 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Thu, 16 Mar 2017 15:40:43 -0400 Subject: [PATCH] admin recipients --- app/controllers/questions_controller.rb | 7 ++ app/controllers/schools_controller.rb | 4 +- app/models/attempt.rb | 5 + app/views/questions/show.html.haml | 9 ++ app/views/recipients/show.html.haml | 97 +++++++++++++------ app/views/schools/admin.html.haml | 2 +- config/routes.rb | 1 + spec/controllers/questions_controller_spec.rb | 4 +- spec/views/questions/show.html.erb_spec.rb | 6 +- spec/views/recipients/show.html.erb_spec.rb | 1 - 10 files changed, 99 insertions(+), 37 deletions(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 8abd88f2..eff3fdbc 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -1,4 +1,5 @@ class QuestionsController < ApplicationController + before_action :set_school, only: [:show] before_action :set_question, only: [:show, :edit, :update, :destroy] # GET /questions @@ -62,6 +63,12 @@ class QuestionsController < ApplicationController end private + def set_school + redirect_to root_path and return false unless params.include?(:school_id) + @school = School.find(params[:school_id]) + redirect_to root_path and return false if @school.nil? + end + # Use callbacks to share common setup or constraints between actions. def set_question @question = Question.find(params[:id]) diff --git a/app/controllers/schools_controller.rb b/app/controllers/schools_controller.rb index f9df5c68..41593344 100644 --- a/app/controllers/schools_controller.rb +++ b/app/controllers/schools_controller.rb @@ -1,5 +1,5 @@ class SchoolsController < ApplicationController - before_action :set_school, only: [:show, :edit, :update, :destroy] + before_action :set_school, only: [:admin, :show, :edit, :update, :destroy] # GET /schools # GET /schools.json @@ -65,7 +65,7 @@ class SchoolsController < ApplicationController private # Use callbacks to share common setup or constraints between actions. def set_school - @school = School.find(params[:id]) + @school = School.find(params[:id] || params[:school_id]) end # Never trust parameters from the scary internet, only allow the white list through. diff --git a/app/models/attempt.rb b/app/models/attempt.rb index 618c60c0..37c78ed7 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -27,6 +27,11 @@ class Attempt < ApplicationRecord recipient.update_attributes(phone: client.messages.get(message.sid).to) end + def response + return 'No Answer Yet' if answer_index.blank? + question.options[answer_index - 1] + end + private def update_school_categories diff --git a/app/views/questions/show.html.haml b/app/views/questions/show.html.haml index 08a1e351..a808a8b5 100644 --- a/app/views/questions/show.html.haml +++ b/app/views/questions/show.html.haml @@ -28,3 +28,12 @@ = link_to 'Edit', edit_question_path(@question) | = link_to 'Back', questions_path + + +%br +%br +%br +%h4 + Results For + = @school.name += render @question diff --git a/app/views/recipients/show.html.haml b/app/views/recipients/show.html.haml index d89fe625..2a07346e 100644 --- a/app/views/recipients/show.html.haml +++ b/app/views/recipients/show.html.haml @@ -1,33 +1,70 @@ -%p - %strong Name: - = @recipient.name -%p - %strong Phone: - = @recipient.phone -%p - %strong Birth date: - = @recipient.birth_date -%p - %strong Gender: - = @recipient.gender -%p - %strong Race: - = @recipient.race -%p - %strong Ethnicity: - = @recipient.ethnicity -%p - %strong Home language: - = @recipient.home_language_id -%p - %strong Income: - = @recipient.income -%p - %strong Opted out: - = @recipient.opted_out -%p - %strong School: - = @recipient.school_id +.row + .col + %p + %strong School: + = @school.name + %p + %strong Recipient: + = @recipient.name + + - if @recipient.phone.present? + %p + %strong Phone: + = @recipient.phone + + - if @recipient.birth_date.present? + %p + %strong Birth date: + = @recipient.birth_date + + - if @recipient.gender.present? + %p + %strong Gender: + = @recipient.gender + + - if @recipient.race.present? + %p + %strong Race: + = @recipient.race + + - if @recipient.ethnicity.present? + %p + %strong Ethnicity: + = @recipient.ethnicity + + - if @recipient.home_language_id.present? + %p + %strong Home language: + = @recipient.home_language_id + + - if @recipient.income.present? + %p + %strong Income: + = @recipient.income + + %p + %strong Opted out: + = @recipient.opted_out + = link_to 'Edit', edit_school_recipient_path(@school, @recipient) | = link_to 'Back', school_path(@school) + + + +%br +%br +%br +%h4 Attempts +%table{style: 'width: 100%;'} + %tbody + %thead{style: 'font-weight: bold;'} + %th Date + %th Question + %th Response + - @recipient.attempts.each do |attempt| + %tr.attempt + %td + = attempt.sent_at.strftime('%x') if attempt.sent_at.present? + %td= link_to truncate(attempt.question.text, length: 60), [@school, attempt.question], title: attempt.question.text + %td= "(#{attempt.answer_index}) #{attempt.response}" diff --git a/app/views/schools/admin.html.haml b/app/views/schools/admin.html.haml index 31de8e5c..4f7313ad 100644 --- a/app/views/schools/admin.html.haml +++ b/app/views/schools/admin.html.haml @@ -71,7 +71,7 @@ %th{colspan: 2} Actions - @school.recipients.each do |recipient| %tr.recipient - %td= recipient.name + %td= link_to recipient.name, [@school, recipient] %td= recipient.phone %td= link_to('Edit', edit_school_recipient_path(@school, recipient)) %td= link_to('Delete', school_recipient_path(@school, recipient), method: :delete, data: {confirm: 'Are you sure you want to delete this recipient?'}) diff --git a/config/routes.rb b/config/routes.rb index 86e3dd10..14c2eb6e 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -14,6 +14,7 @@ Rails.application.routes.draw do end resources :schedules resources :categories, only: [:show] + resources :questions, only: [:show] get :admin end diff --git a/spec/controllers/questions_controller_spec.rb b/spec/controllers/questions_controller_spec.rb index 09a87481..63182540 100644 --- a/spec/controllers/questions_controller_spec.rb +++ b/spec/controllers/questions_controller_spec.rb @@ -55,9 +55,11 @@ RSpec.describe QuestionsController, type: :controller do describe "GET #show" do it "assigns the requested question as @question" do + school = School.create!(name: 'School') question = Question.create! valid_attributes - get :show, params: {id: question.to_param}, session: valid_session + get :show, params: {school_id: school.id, id: question.to_param}, session: valid_session expect(assigns(:question)).to eq(question) + expect(assigns(:school)).to eq(school) end end diff --git a/spec/views/questions/show.html.erb_spec.rb b/spec/views/questions/show.html.erb_spec.rb index 4e5f4da0..42f41ec6 100644 --- a/spec/views/questions/show.html.erb_spec.rb +++ b/spec/views/questions/show.html.erb_spec.rb @@ -2,8 +2,9 @@ require 'rails_helper' RSpec.describe "questions/show", type: :view do before(:each) do + @school = assign(:school, School.create!(name: 'School')) @question = assign(:question, Question.create!( - :text => "Text", + :text => "Question Text", :option1 => "Option1", :option2 => "Option2", :option3 => "Option3", @@ -15,7 +16,8 @@ RSpec.describe "questions/show", type: :view do it "renders attributes in

" do render - expect(rendered).to match(/Text/) + expect(rendered).to match(/School/) + expect(rendered).to match(/Question Text/) expect(rendered).to match(/Option1/) expect(rendered).to match(/Option2/) expect(rendered).to match(/Option3/) diff --git a/spec/views/recipients/show.html.erb_spec.rb b/spec/views/recipients/show.html.erb_spec.rb index 10bd4e46..df4f34cf 100644 --- a/spec/views/recipients/show.html.erb_spec.rb +++ b/spec/views/recipients/show.html.erb_spec.rb @@ -29,6 +29,5 @@ RSpec.describe "recipients/show", type: :view do expect(rendered).to match(/2/) expect(rendered).to match(/Income/) expect(rendered).to match(/false/) - expect(rendered).to match(/#{@school.to_param}/) end end