From 3556deb1eacc5ef81633d9068c043fc680ff5fed Mon Sep 17 00:00:00 2001 From: Joachim Filip Ignacy Bartosik Date: Mon, 23 Aug 2010 17:11:49 +0200 Subject: Pie chart on how recruits evaluated documentation on questions how pages Charts are shown only to recruiters on questions show pages, but other users may view charts directly (by entering URL manually). --- app/controllers/questions_controller.rb | 3 +++ app/models/answer.rb | 5 +++++ app/models/question.rb | 20 +++++++++++++++++ app/views/questions/doc_feedback_chart.dryml | 16 ++++++++++++++ app/views/questions/show.dryml | 3 +++ features/documentation_feedback.feature | 25 ++++++++++++++++++++++ .../documentation_feedback_steps.rb | 16 ++++++++++++++ spec/models/answer_spec.rb | 16 ++++++++++++++ spec/models/question_spec.rb | 12 +++++++++++ 9 files changed, 116 insertions(+) create mode 100644 app/views/questions/doc_feedback_chart.dryml create mode 100644 features/documentation_feedback.feature create mode 100644 features/step_definitions/documentation_feedback_steps.rb diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index ae35065..0e1ba3c 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -30,4 +30,7 @@ class QuestionsController < ApplicationController def approve_questions hobo_index Question.questions_to_approve end + + show_action :doc_feedback_chart + end diff --git a/app/models/answer.rb b/app/models/answer.rb index f013547..47544b3 100644 --- a/app/models/answer.rb +++ b/app/models/answer.rb @@ -47,6 +47,11 @@ class Answer < ActiveRecord::Base named_scope :in_category, lambda { |category| { :joins => :question, :conditions => { 'questions.question_category_id', category} } } + named_scope :with_feedback, lambda { |opt| { + :conditions => { :feedback => opt } } } + + named_scope :with_some_feedback, :conditions => "answers.feedback IS NOT NULL AND answers.feedback <> ''" + validates_uniqueness_of :question_id, :scope => :reference, :if => :reference validates_uniqueness_of :question_id, :scope => :owner_id, :unless => :reference diff --git a/app/models/question.rb b/app/models/question.rb index 084f826..294e03e 100644 --- a/app/models/question.rb +++ b/app/models/question.rb @@ -160,6 +160,26 @@ class Question < ActiveRecord::Base after_create :notify_new_question after_update :notify_approved_question + # Returns hash with: + # :values - string with coma-separated values + # :labels - string with coma-separated, quoted labels for values + def feedback_chart_data + classes = Answer.new.feedback.class.values - [''] + delta = 0.00001 + result = {} + counts = classes.collect{ |opt| answers.with_feedback(opt).count } + + result[:values] = counts.inject(nil) do |res, cur| + res.nil? ? (cur + delta).to_s : "#{res}, #{cur + delta}" + end + + result[:labels] = classes.inject(nil) do |res, cur| + res.nil? ? "\"%%.%% - #{cur} (##)\"" : "#{res}, \"%%.%% - #{cur} (##)\"" + end + + result + end + protected # Sends notification about new question (TODO: check for group). def notify_new_question diff --git a/app/views/questions/doc_feedback_chart.dryml b/app/views/questions/doc_feedback_chart.dryml new file mode 100644 index 0000000..1a02c45 --- /dev/null +++ b/app/views/questions/doc_feedback_chart.dryml @@ -0,0 +1,16 @@ + + + + + + + + + + diff --git a/app/views/questions/show.dryml b/app/views/questions/show.dryml index 2e30578..8cb6629 100644 --- a/app/views/questions/show.dryml +++ b/app/views/questions/show.dryml @@ -26,5 +26,8 @@
View reference answer. + +