From 22e45b8f2f57eba2b54250dcfd3e6756e9e6acb8 Mon Sep 17 00:00:00 2001 From: Cess Date: Sat, 8 Dec 2018 13:38:29 +0300 Subject: [PATCH 1/4] implement questions answered vs questions asked --- app/controllers/questions_controller.rb | 11 +++++++++++ app/views/questions/index.html.erb | 21 ++++++++++++++++++++- 2 files changed, 31 insertions(+), 1 deletion(-) diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index ca88516ea2..73c41fc559 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -19,12 +19,23 @@ def filter_questions_by_tag(questions, tagnames) public def index + params[:period] + @asked = Node.questions.to_a.size + @answered = Answer.all.map(&:node).uniq.size @title = 'Questions and Answers' + set_sidebar @questions = Node.questions .where(status: 1) .order('node.nid DESC') .paginate(page: params[:page], per_page: 24) + @week_asked = Node.questions.where('created >= ?', 1.week.ago.to_i).to_a.size + @month_asked = Node.questions.where('created >= ?', 1.month.ago.to_i).to_a.size + @year_asked = Node.questions.where('created >= ?', 1.year.ago.to_i).to_a.size + @week_answered = Answer.where("created_at >= ?", 1.week.ago).map(&:node).uniq.size + @month_answered = Answer.where("created_at >= ?", 1.month.ago).map(&:node).uniq.size + @year_answered = Answer.where("created_at >= ?", 1.year.ago).map(&:node).uniq.size + @period = [@week_asked, @month_asked, @year_asked] end # a form for new questions, at /questions/new diff --git a/app/views/questions/index.html.erb b/app/views/questions/index.html.erb index 299cac9592..ba2b80c2c8 100644 --- a/app/views/questions/index.html.erb +++ b/app/views/questions/index.html.erb @@ -1,5 +1,24 @@
+
+
+ <% if params[:period] == "Week" %> +

<%= @week_asked %> Questions asked and <%= @week_answered %> questions answered in the past <%= params[:period] %>

+ <% elsif params[:period] == "Month"%> +

<%= @month_asked %> Questions asked and <%= @month_answered %> questions answered in the past <%= params[:period] %>

+ <% elsif params[:period]== "Year" %> +

<%= @year_asked %> Questions asked and <%= @year_answered %> questions answered in the past <%= params[:period] %>

+ <% else %> +

<%= @asked %> Questions asked and <%= @answered %> questions answered

+ <% end %> +
+
+ <%= form_tag request.url, :method => 'get' do %> + <%= select_tag :period, options_for_select(["Week", "Month", "Year"]), value: params[:period], selected: params[:period], onchange: "this.form.submit();", include_blank: "All"%> + <% end %> +
+
+ <% if params[:action] == 'answered' %>

Recently Answered

<% elsif params[:action] == 'popular' %> @@ -14,7 +33,7 @@ <%= feature('questions-header') %> <% end %>
- <% end %> + <% end %>
From c0db8a958990f7d5e4e4454ba0967b28011beae6 Mon Sep 17 00:00:00 2001 From: Cess Date: Wed, 12 Dec 2018 20:22:16 +0300 Subject: [PATCH 2/4] Refactoring the questions stats --- app/controllers/questions_controller.rb | 12 +----------- app/helpers/questions_helper.rb | 21 +++++++++++++++++++++ app/views/questions/index.html.erb | 15 +++------------ 3 files changed, 25 insertions(+), 23 deletions(-) create mode 100644 app/helpers/questions_helper.rb diff --git a/app/controllers/questions_controller.rb b/app/controllers/questions_controller.rb index 73c41fc559..a589225629 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -19,23 +19,13 @@ def filter_questions_by_tag(questions, tagnames) public def index - params[:period] - @asked = Node.questions.to_a.size - @answered = Answer.all.map(&:node).uniq.size + @stats = helpers.filtering(params[:period]) @title = 'Questions and Answers' - set_sidebar @questions = Node.questions .where(status: 1) .order('node.nid DESC') .paginate(page: params[:page], per_page: 24) - @week_asked = Node.questions.where('created >= ?', 1.week.ago.to_i).to_a.size - @month_asked = Node.questions.where('created >= ?', 1.month.ago.to_i).to_a.size - @year_asked = Node.questions.where('created >= ?', 1.year.ago.to_i).to_a.size - @week_answered = Answer.where("created_at >= ?", 1.week.ago).map(&:node).uniq.size - @month_answered = Answer.where("created_at >= ?", 1.month.ago).map(&:node).uniq.size - @year_answered = Answer.where("created_at >= ?", 1.year.ago).map(&:node).uniq.size - @period = [@week_asked, @month_asked, @year_asked] end # a form for new questions, at /questions/new diff --git a/app/helpers/questions_helper.rb b/app/helpers/questions_helper.rb new file mode 100644 index 0000000000..7b025894c8 --- /dev/null +++ b/app/helpers/questions_helper.rb @@ -0,0 +1,21 @@ +module QuestionsHelper + SORTING_OPTIONS = %w(All Week Month Year).freeze + + def filtering(period) + return if period.nil? + + if period == 'All' + @asked = Node.questions.to_a.size + @answered = Answer.all.map(&:node).uniq.size + "#{@asked} questions asked and #{@answered} questions answered" + else + @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}" + end + end + + def options + SORTING_OPTIONS + end +end diff --git a/app/views/questions/index.html.erb b/app/views/questions/index.html.erb index ba2b80c2c8..565def31cd 100644 --- a/app/views/questions/index.html.erb +++ b/app/views/questions/index.html.erb @@ -1,20 +1,11 @@
-
-
- <% if params[:period] == "Week" %> -

<%= @week_asked %> Questions asked and <%= @week_answered %> questions answered in the past <%= params[:period] %>

- <% elsif params[:period] == "Month"%> -

<%= @month_asked %> Questions asked and <%= @month_answered %> questions answered in the past <%= params[:period] %>

- <% elsif params[:period]== "Year" %> -

<%= @year_asked %> Questions asked and <%= @year_answered %> questions answered in the past <%= params[:period] %>

- <% else %> -

<%= @asked %> Questions asked and <%= @answered %> questions answered

- <% end %> +
+

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

<%= form_tag request.url, :method => 'get' do %> - <%= select_tag :period, options_for_select(["Week", "Month", "Year"]), value: params[:period], selected: params[:period], onchange: "this.form.submit();", include_blank: "All"%> + <%= select_tag :period, options_for_select(options),onchange: "this.form.submit();", include_blank: "Filter stats", class: "form-control"%> <% end %>
From 715ccd4769dd4404570099c0e337d590ab7cd972 Mon Sep 17 00:00:00 2001 From: Cess Date: Thu, 13 Dec 2018 10:29:27 +0300 Subject: [PATCH 3/4] caching questions stats --- app/helpers/questions_helper.rb | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/app/helpers/questions_helper.rb b/app/helpers/questions_helper.rb index 7b025894c8..708efbacb2 100644 --- a/app/helpers/questions_helper.rb +++ b/app/helpers/questions_helper.rb @@ -5,13 +5,17 @@ def filtering(period) return if period.nil? if period == 'All' - @asked = Node.questions.to_a.size - @answered = Answer.all.map(&:node).uniq.size - "#{@asked} questions asked and #{@answered} questions answered" + Rails.cache.fetch("all_stats", expires_in: 1.days) do + @asked = Node.questions.to_a.size + @answered = Answer.all.map(&:node).uniq.size + "#{@asked} questions asked and #{@answered} questions answered" + end else - @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}" + 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}" + end end end From e9fd26755261628c12e7c9d2bca196def7b208a6 Mon Sep 17 00:00:00 2001 From: Cess Date: Wed, 19 Dec 2018 18:01:24 +0300 Subject: [PATCH 4/4] test question helper --- Gemfile | 1 + Gemfile.lock | 7 ++++++ app/controllers/questions_controller.rb | 7 +++++- app/helpers/questions_helper.rb | 12 ++++----- app/views/questions/index.html.erb | 5 +++- test/unit/helpers/questions_helper_test.rb | 29 ++++++++++++++++++++++ 6 files changed, 53 insertions(+), 8 deletions(-) create mode 100644 test/unit/helpers/questions_helper_test.rb diff --git a/Gemfile b/Gemfile index e6b3677477..05c0fef0c1 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 c485947eb9..dca8b5cd36 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 a589225629..9725578b89 100644 --- a/app/controllers/questions_controller.rb +++ b/app/controllers/questions_controller.rb @@ -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) @@ -16,10 +18,13 @@ 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 708efbacb2..69f9294d93 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 565def31cd..9eea52c7d2 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 0000000000..8e2eb5b3d3 --- /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