Skip to content

Commit

Permalink
Refactoring the questions stats
Browse files Browse the repository at this point in the history
  • Loading branch information
cesswairimu committed Dec 12, 2018
1 parent 1bda7e8 commit 946d3f0
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 23 deletions.
12 changes: 1 addition & 11 deletions app/controllers/questions_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
21 changes: 21 additions & 0 deletions app/helpers/questions_helper.rb
Original file line number Diff line number Diff line change
@@ -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

15 changes: 3 additions & 12 deletions app/views/questions/index.html.erb
Original file line number Diff line number Diff line change
@@ -1,20 +1,11 @@
<div class="col-md-12">

<div class ="row">
<div class="col-md-6 offset-md-2" >
<% if params[:period] == "Week" %>
<h4><b> <%= @week_asked %> Questions asked and <%= @week_answered %> questions answered in the past <%= params[:period] %></b></h4>
<% elsif params[:period] == "Month"%>
<h4><b> <%= @month_asked %> Questions asked and <%= @month_answered %> questions answered in the past <%= params[:period] %></b></h4>
<% elsif params[:period]== "Year" %>
<h4><b> <%= @year_asked %> Questions asked and <%= @year_answered %> questions answered in the past <%= params[:period] %></b></h4>
<% else %>
<h4><b> <%= @asked %> Questions asked and <%= @answered %> questions answered </b></h4>
<% end %>
<div class="col-md-6" >
<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(["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 %>
</div>
</div>
Expand Down

0 comments on commit 946d3f0

Please sign in to comment.