Skip to content

Commit

Permalink
Implement questions answered vs questions asked (#4256)
Browse files Browse the repository at this point in the history
* implement questions answered vs questions asked

* Refactoring the questions stats

* caching questions stats

* test question helper
  • Loading branch information
cesswairimu authored and jywarren committed Dec 22, 2018
1 parent fed59e2 commit 1802e62
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 1 deletion.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ group :test, :development do
gem 'test-unit'
gem 'teaspoon-mocha'
gem 'timecop'
gem 'pry-rails'
end

group :production do
Expand Down
7 changes: 7 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ GEM
ci_reporter (~> 2.0)
test-unit (>= 2.5.5, < 4.0)
climate_control (0.2.0)
coderay (1.1.2)
coercible (1.0.0)
descendants_tracker (~> 0.0.1)
coffee-rails (4.2.2)
Expand Down Expand Up @@ -284,6 +285,11 @@ GEM
progress_bar (1.3.0)
highline (>= 1.6, < 3)
options (~> 2.3.0)
pry (0.12.2)
coderay (~> 1.1.0)
method_source (~> 0.9.0)
pry-rails (0.3.8)
pry (>= 0.10.4)
public_suffix (3.0.3)
rack (2.0.6)
rack-accept (0.4.5)
Expand Down Expand Up @@ -517,6 +523,7 @@ DEPENDENCIES
phantomjs
php-serialize
progress_bar
pry-rails
rack-cors
rack-openid
rack-test (= 1.1.0)
Expand Down
6 changes: 6 additions & 0 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class QuestionsController < ApplicationController
before_action :quiz_stats, only: %i(index answered popular liked unanswered)

private

def filter_questions_by_tag(questions, tagnames)
Expand All @@ -16,6 +18,10 @@ def filter_questions_by_tag(questions, tagnames)
end
end

def quiz_stats
@stats = helpers.questions_stats(params[:period])
end

public

def index
Expand Down
25 changes: 25 additions & 0 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
module QuestionsHelper
SORTING_OPTIONS = %w(All Week Month Year).freeze

def questions_stats(period)
return if period.nil?

if period == 'All'
Rails.cache.fetch("all_stats", expires_in: 1.days) do
@asked = Node.questions.length
@answered = Answer.all.map(&:node).uniq.count
"#{@asked} questions asked and #{@answered} questions answered"
end
else
Rails.cache.fetch("#{period}_stats", expires_in: 1.days) do
@asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).length
@answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.count
"#{@asked} questions asked and #{@answered} questions answered in the past #{period.downcase}"
end
end
end

def options
SORTING_OPTIONS
end
end
15 changes: 14 additions & 1 deletion app/views/questions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,4 +1,17 @@
<div class="col-md-12">
<div class ="row">
<div class="col-md-6" >
<% if @stats.blank? %>
<h4><i> Select a range to view questions statistics within that period</i></h4>
<% end %>
<h4><b> <%= @stats if params[:period].present? %></b></h4>
</div>
<div class="col-md-4">
<%= form_tag request.url, :method => 'get' do %>
<%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats by week, month and year", class: "form-control"%>
<% end %>
</div>
</div>

<% if params[:action] == 'answered' %>
<h2>Recently Answered <small class="hidden-sm">Recently answered questions</small></h2>
Expand All @@ -14,7 +27,7 @@
<%= feature('questions-header') %>
<% end %>
</div>
<% end %>
<% end %>

<hr />

Expand Down
29 changes: 29 additions & 0 deletions test/unit/helpers/questions_helper_test.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
require 'test_helper'

class QuestionsHelperTest < ActionView::TestCase
test 'should return null if period is nil' do
assert_nil questions_stats(nil)
end

test 'should return all questions if all' do
request = questions_stats("All")
asked = Node.questions.length
answered = Answer.all.map(&:node).uniq.count
assert_not_nil request
assert_includes request, asked.to_s
assert_includes request, answered.to_s
end

test "should return exact count according to period given" do
options.reject { |option| option == "All" }.each do |period|
request = questions_stats(period)
asked = Node.questions.where('created >= ?', 1.send(period.downcase).ago.to_i).length
answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.count
assert_includes request, asked.to_s
assert_includes request, answered.to_s
end
end

test 'it caches results' do
end
end

0 comments on commit 1802e62

Please sign in to comment.