diff --git a/Gemfile b/Gemfile index e6b36774776..05c0fef0c1d 100644 --- a/Gemfile +++ b/Gemfile @@ -109,6 +109,7 @@ group :test, :development do gem 'test-unit' gem 'teaspoon-mocha' gem 'timecop' + gem 'pry-rails' end group :production do diff --git a/Gemfile.lock b/Gemfile.lock index c485947eb9d..dca8b5cd36c 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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) @@ -517,6 +523,7 @@ DEPENDENCIES phantomjs php-serialize progress_bar + pry-rails rack-cors rack-openid rack-test (= 1.1.0) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index a5892256293..935079ba54d 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -1,4 +1,5 @@ class QuestionsController < ApplicationController + before_action :quiz_stats, only: [:index, :answered, :popular, :liked, :unanswered] private def filter_questions_by_tag(questions, tagnames) @@ -16,10 +17,12 @@ def filter_questions_by_tag(questions, tagnames) end end + def quiz_stats + @stats = helpers.questions_stats(params[:period]) + end public def index - @stats = helpers.filtering(params[:period]) @title = 'Questions and Answers' set_sidebar @questions = Node.questions diff --git a/app/helpers/questions_helper.rb b/app/helpers/questions_helper.rb index 708efbacb24..69f9294d93e 100644 --- a/app/helpers/questions_helper.rb +++ b/app/helpers/questions_helper.rb @@ -1,20 +1,20 @@ module QuestionsHelper SORTING_OPTIONS = %w(All Week Month Year).freeze - def filtering(period) + def questions_stats(period) return if period.nil? if period == 'All' Rails.cache.fetch("all_stats", expires_in: 1.days) do - @asked = Node.questions.to_a.size - @answered = Answer.all.map(&:node).uniq.size + @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).to_a.size - @answered = Answer.where("created_at >= ?", 1.send(period.downcase).ago).map(&:node).uniq.size - "#{@asked} questions asked and #{@answered} questions answered in the past #{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 + "#{@asked} questions asked and #{@answered} questions answered in the past #{period.downcase}" end end end diff --git a/app/views/questions/index.html.erb b/app/views/questions/index.html.erb index 565def31cdf..9eea52c7d28 100644 --- a/app/views/questions/index.html.erb +++ b/app/views/questions/index.html.erb @@ -1,11 +1,14 @@
+ <% if @stats.blank? %> +

Select a range to view questions statistics within that period

+ <% end %>

<%= @stats if params[:period].present? %>

<%= form_tag request.url, :method => 'get' do %> - <%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats", class: "form-control"%> + <%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats by week, month and year", class: "form-control"%> <% end %>
diff --git a/test/unit/helpers/questions_helper_test.rb b/test/unit/helpers/questions_helper_test.rb new file mode 100644 index 00000000000..8e2eb5b3d3b --- /dev/null +++ b/test/unit/helpers/questions_helper_test.rb @@ -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