Created
May 12, 2021 14:16
-
-
Save fractaledmind/a2baeb458947992830ccbdc20ab770e6 to your computer and use it in GitHub Desktop.
Example Dashboard with basic time range filtering
This file contains hidden or 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
module CoreExtensions | |
module Date | |
module Ranges | |
def all_yesterday | |
yesterday.all_day | |
end | |
def all_last_week | |
last_week.all_week | |
end | |
def all_last_month | |
last_month.all_month | |
end | |
def all_last_quarter | |
last_quarter.all_quarter | |
end | |
def all_last_year | |
last_year.all_year | |
end | |
end | |
end | |
end |
This file contains hidden or 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
<%= form_with(local: true, url: user_root_path, method: :get, class: "w-2/3 space-x-2 flex items-center") do |form| %> | |
<%= form.collection_select :time_range, | |
@filterable_time_ranges, | |
:first, :last, | |
{ selected: params[:time_range] || :all_month }, | |
{ class: "select form-control control-small" } %> | |
<%= form.button "Filter", type: :submit, name: nil, class: "btn btn-primary" %> | |
<% end %> | |
<% if @activities.any? %> | |
<ul> | |
<% @activities.each do |activity| %> | |
<li> | |
<%= render activity %> | |
</li> | |
<% end %> | |
</ul> | |
<% else %> | |
<% if params[:time_range].present? %> | |
<% time_range_filter = @filterable_time_ranges.find { |k, _| k.to_s == params[:time_range].to_s } %> | |
No activity for | |
<span class="font-medium text-gray-900"><%= time_range_filter.last %></span> | |
<% else %> | |
No activity currently | |
<% end %> | |
<% end %> |
This file contains hidden or 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
class DashboardController < ApplicationController | |
def show | |
Date.include CoreExtensions::Date::Ranges | |
@filterable_time_ranges = [ | |
[:all_day, "Today"], | |
[:all_yesterday, "Yesterday"], | |
[:all_week, "This week"], | |
[:all_last_week, "Last week"], | |
[:all_month, "This month"], | |
[:all_last_month, "Last month"], | |
[:all_quarter, "This quarter"], | |
[:all_last_quarter, "Last quarter"], | |
[:all_year, "This year"], | |
[:all_last_year, "Last year"] | |
] | |
reviews = current_account.reviews.where.not(submitted_at: nil) | |
reviews = if params[:time_range].present? | |
reviews.where(submitted_at: Date.current.public_send(params[:time_range])) | |
else | |
reviews.where(submitted_at: Date.current.all_month) | |
end | |
@review_activities = reviews.includes(:repository, :requester) | |
feedbacks = current_account.feedbacks.where.not(submitted_at: nil) | |
feedbacks = if params[:time_range].present? | |
feedbacks.where(submitted_at: Date.current.public_send(params[:time_range])) | |
else | |
feedbacks.where(submitted_at: Date.current.all_month) | |
end | |
@feedback_activities = feedbacks.includes(:review, :repository) | |
@activities = [@review_activities, @feedback_activities].flatten.compact.sort_by(&:submitted_at).reverse! | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment