working on aggregated results

This commit is contained in:
Jared Cosulich 2017-03-12 21:04:40 -04:00
parent f5b473ed28
commit 48eb55ad94
11 changed files with 120 additions and 8 deletions

View file

@ -7,6 +7,11 @@ class Attempt < ApplicationRecord
belongs_to :recipient_schedule
belongs_to :question
after_save :update_school_categories
scope :for_category, -> (category) { joins(:question).merge(Question.for_category(category)) }
scope :for_school, -> (school) { joins(:recipient).merge(Recipient.for_school(school)) }
def send_message
twilio_number = ENV['TWILIO_NUMBER']
client = Twilio::REST::Client.new ENV['TWILIO_ACCOUNT_SID'], ENV['TWILIO_AUTH_TOKEN']
@ -21,4 +26,14 @@ class Attempt < ApplicationRecord
recipient.update_attributes(phone: client.messages.get(message.sid).to)
end
private
def update_school_categories
school_category = SchoolCategory.for(recipient.school, question.category).first
if school_category.nil?
school_category = SchoolCategory.create(school: recipient.school, category: question.category)
end
# school_category.aggregate_responses
end
end

View file

@ -8,6 +8,9 @@ class Question < ApplicationRecord
validates :option4, presence: true
validates :option5, presence: true
scope :for_category, -> (category) { where(category: category) }
def options
[option1, option2, option3, option4, option5].map(&:downcase).map(&:strip)
end

View file

@ -8,6 +8,8 @@ class Recipient < ApplicationRecord
validates :name, presence: true
scope :for_school, -> (school) { where(school: school) }
before_destroy :sync_lists
def self.import(school, file)

View file

@ -0,0 +1,27 @@
class SchoolCategory < ApplicationRecord
belongs_to :school
belongs_to :category
scope :for, -> (school, category) { where(school: school).where(category: category) }
def aggregated_responses
attempt_data = Attempt.
for_category(category).
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]
return {
attempt_count: attempt_data.attempt_count,
response_count: attempt_data.response_count,
answer_index_total: attempt_data.answer_index_total
}
end
def aggregate_responses
return if ENV['BULK_PROCESS']
end
end