-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement questions answered vs questions asked (#4256)
* implement questions answered vs questions asked * Refactoring the questions stats * caching questions stats * test question helper
- Loading branch information
1 parent
fed59e2
commit 1802e62
Showing
6 changed files
with
82 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |