From e73351d0f548ec80982f42ab9db7f35c298406a7 Mon Sep 17 00:00:00 2001 From: Jared Cosulich Date: Sat, 28 Oct 2017 15:32:57 -0400 Subject: [PATCH] reversed questions --- app/models/attempt.rb | 7 ++++++- app/models/question.rb | 4 ++-- app/models/school_category.rb | 2 +- app/views/questions/_question.html.haml | 2 +- app/views/recipients/show.html.haml | 2 +- app/views/shared/_histogram.html.haml | 2 +- db/migrate/20171028181758_add_reverse_to_questions.rb | 5 +++++ db/schema.rb | 3 ++- 8 files changed, 19 insertions(+), 8 deletions(-) create mode 100644 db/migrate/20171028181758_add_reverse_to_questions.rb diff --git a/app/models/attempt.rb b/app/models/attempt.rb index b5019afe..aceb58b2 100644 --- a/app/models/attempt.rb +++ b/app/models/attempt.rb @@ -51,7 +51,7 @@ class Attempt < ApplicationRecord def response return 'No Answer Yet' if answer_index.blank? - question.options[answer_index - 1] + question.options[answer_index_with_reverse - 1] end def save_response(answer_index: nil, twilio_details: nil, responded_at: Time.new) @@ -66,6 +66,11 @@ class Attempt < ApplicationRecord end end + def answer_index_with_reverse + return 5 - answer_index if question.reverse? + return answer_index + end + private def update_school_categories diff --git a/app/models/question.rb b/app/models/question.rb index 0fc232cd..13d83108 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -40,9 +40,9 @@ class Question < ApplicationRecord school_responses = attempts.for_school(school).with_answer.order(id: :asc) return unless school_responses.present? - response_answer_total = school_responses.inject(0) { |total, response| total + response.answer_index } + response_answer_total = school_responses.inject(0) { |total, response| total + response.answer_index_with_reverse } + histogram = school_responses.group_by(&:answer_index_with_reverse) - histogram = school_responses.group_by(&:answer_index) most_popular_answer_index = histogram.to_a.sort_by { |info| info[1].length }.last[0] most_popular_answer = send("option#{most_popular_answer_index}") diff --git a/app/models/school_category.rb b/app/models/school_category.rb index af65cb20..3d4bb68a 100644 --- a/app/models/school_category.rb +++ b/app/models/school_category.rb @@ -19,7 +19,7 @@ class SchoolCategory < ApplicationRecord for_school(school). select('count(attempts.id) as attempt_count'). select('count(attempts.answer_index) as response_count'). - select('sum(attempts.answer_index) as answer_index_total')[0] + select('sum(case when questions.reverse then 5 - attempts.answer_index else attempts.answer_index end) as answer_index_total')[0] return { attempt_count: attempt_data.attempt_count || 0, diff --git a/app/views/questions/_question.html.haml b/app/views/questions/_question.html.haml index 323cb56d..44269942 100644 --- a/app/views/questions/_question.html.haml +++ b/app/views/questions/_question.html.haml @@ -35,4 +35,4 @@ = render 'shared/histogram', data: aggregated_responses %p.collapse{id: "raw-data#{question.id}"} - = aggregated_responses.responses.map(&:answer_index).join(', ') + = aggregated_responses.responses.map(&:answer_index_with_reverse).join(', ') diff --git a/app/views/recipients/show.html.haml b/app/views/recipients/show.html.haml index 1fd970f0..0791746a 100644 --- a/app/views/recipients/show.html.haml +++ b/app/views/recipients/show.html.haml @@ -76,4 +76,4 @@ %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}" + %td= "(#{attempt.answer_index_with_reverse}) #{attempt.response}" diff --git a/app/views/shared/_histogram.html.haml b/app/views/shared/_histogram.html.haml index 16a94f57..2fe088ab 100644 --- a/app/views/shared/_histogram.html.haml +++ b/app/views/shared/_histogram.html.haml @@ -1,4 +1,4 @@ -- histogram = data.responses.map(&:answer_index).group_by { |a| a.to_s } +- histogram = data.responses.map(&:answer_index_with_reverse).group_by { |a| a.to_s } %table.answers %tbody - data.question.options.each_with_index do |option, index| diff --git a/db/migrate/20171028181758_add_reverse_to_questions.rb b/db/migrate/20171028181758_add_reverse_to_questions.rb new file mode 100644 index 00000000..275d414a --- /dev/null +++ b/db/migrate/20171028181758_add_reverse_to_questions.rb @@ -0,0 +1,5 @@ +class AddReverseToQuestions < ActiveRecord::Migration[5.0] + def change + add_column :questions, :reverse, :boolean, default: false + end +end diff --git a/db/schema.rb b/db/schema.rb index aaa5d361..15f828a0 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170418173141) do +ActiveRecord::Schema.define(version: 20171028181758) do # These are extensions that must be enabled in order to support this database enable_extension "plpgsql" @@ -72,6 +72,7 @@ ActiveRecord::Schema.define(version: 20170418173141) do t.datetime "updated_at", null: false t.integer "target_group", default: 0 t.boolean "for_recipient_students", default: false + t.boolean "reverse", default: false end create_table "recipient_lists", force: :cascade do |t|