Skip to content

Commit ea87db6

Browse files
committed
Make start of the week customizable for the profile page
This is a hidden parameter for now, but if somebody wants a different view that doesn't start with Monday, it is possible.
1 parent 943dd82 commit ea87db6

11 files changed

Lines changed: 100 additions & 26 deletions

app/controllers/concerns/profile_activity.rb

Lines changed: 12 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ def person_ids_for_query
2121
end
2222

2323
def load_activity_data(scope: nil, year: nil)
24+
@week_start_day = parse_week_start_day
2425
@activity_filters = parse_activity_filters
2526
effective_scope = scope || default_recent_scope
2627
@activity_entries = build_activity_entries(scope: effective_scope, filters: @activity_filters)
@@ -29,6 +30,7 @@ def load_activity_data(scope: nil, year: nil)
2930
@contribution_years = contribution_years
3031
@contribution_year = year || select_contribution_year(@contribution_years)
3132
@contribution_weeks, @contribution_month_spans = build_contribution_weeks(@contribution_year, filters: @activity_filters)
33+
@weekday_labels = WeekCalculation.weekday_labels(@week_start_day)
3234
end
3335

3436
def default_recent_scope
@@ -179,28 +181,29 @@ def build_contribution_weeks(year, filters: nil)
179181
return [[], []] if ids.blank?
180182

181183
year = year.to_i
182-
start_date = Date.commercial(year, 1, 1)
183-
end_date = Date.commercial(year, Date.new(year, 12, 28).cweek, 7)
184+
wday_start = @week_start_day || WeekCalculation::DEFAULT_WEEK_START
185+
start_date, end_date = WeekCalculation.year_weeks_range(year, wday_start)
184186

185187
counts = build_filtered_contribution_counts(start_date, end_date, filters)
186188

187189
total_days = (end_date - start_date).to_i + 1
188190
days = (0...total_days).map do |idx|
189-
date = start_date + idx.days
191+
date = start_date + idx
190192
count = counts[date] || 0
191193
{ date: date, count: count, level: contribution_level(count) }
192194
end
193195

194196
weeks_data = days.each_slice(7).map do |week_days|
195197
first_day = week_days.first[:date]
196-
{ days: week_days, year: first_day.cwyear, week: first_day.cweek, count: week_days.sum { |d| d[:count] } }
198+
week_num = WeekCalculation.week_number(first_day, year, wday_start)
199+
{ days: week_days, year: year, week: week_num, count: week_days.sum { |d| d[:count] } }
197200
end
198201

199202
weeks_data.each do |week|
200203
next if week[:days].length == 7
201204
missing = 7 - week[:days].length
202205
missing.times do |idx|
203-
date = week[:days].last[:date] + (idx + 1).days
206+
date = week[:days].last[:date] + (idx + 1)
204207
week[:days] << { date: date, count: 0, level: 0 }
205208
end
206209
end
@@ -305,6 +308,10 @@ def parse_activity_filters
305308
params[:filters].select { |f| ALL_ACTIVITY_FILTERS.include?(f) }
306309
end
307310

311+
def parse_week_start_day
312+
WeekCalculation.parse_week_start(params[:week_start])
313+
end
314+
308315
def parse_activity_date
309316
Date.iso8601(params[:date])
310317
rescue ArgumentError

app/controllers/people_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ def monthly_activity
3838
def weekly_activity
3939
year = params[:year].to_i
4040
week = params[:week].to_i
41-
start_date = Date.commercial(year, week, 1)
42-
end_date = start_date + 6.days
41+
wday_start = WeekCalculation.parse_week_start(params[:week_start])
42+
start_date = WeekCalculation.week_start_date(year, week, wday_start)
43+
end_date = start_date + 6
4344
@profile_email = profile_email
4445
@activity_period = { type: :week, year: year, week: week, start_date: start_date, end_date: end_date }
4546
load_activity_data(scope: messages_scope_for_range(start_date, end_date), year: year)

app/controllers/teams_profile_controller.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,8 +34,9 @@ def monthly_activity
3434
def weekly_activity
3535
year = params[:year].to_i
3636
week = params[:week].to_i
37-
start_date = Date.commercial(year, week, 1)
38-
end_date = start_date + 6.days
37+
wday_start = WeekCalculation.parse_week_start(params[:week_start])
38+
start_date = WeekCalculation.week_start_date(year, week, wday_start)
39+
end_date = start_date + 6
3940
@activity_period = { type: :week, year: year, week: week, start_date: start_date, end_date: end_date }
4041
load_activity_data(scope: messages_scope_for_range(start_date, end_date), year: year)
4142
render :activity

app/helpers/profile_helper.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -14,23 +14,25 @@ def profile_filter_url(profile_routes, activity_period)
1414
end
1515
end
1616

17-
def person_profile_routes(email)
17+
def person_profile_routes(email, week_start: nil)
18+
ws = week_start ? { week_start: week_start } : {}
1819
{
19-
default: person_path(email),
20-
daily: ->(date) { person_activity_path(email, date) },
21-
weekly: ->(year, week) { person_weekly_activity_path(email, year, week) },
22-
monthly: ->(year, month) { person_monthly_activity_path(email, year, month) },
23-
contributions: ->(year) { person_contributions_path(email, year: year) }
20+
default: person_path(email, **ws),
21+
daily: ->(date) { person_activity_path(email, date, **ws) },
22+
weekly: ->(year, week) { person_weekly_activity_path(email, year, week, **ws) },
23+
monthly: ->(year, month) { person_monthly_activity_path(email, year, month, **ws) },
24+
contributions: ->(year) { person_contributions_path(email, year: year, **ws) }
2425
}
2526
end
2627

27-
def team_profile_routes(name)
28+
def team_profile_routes(name, week_start: nil)
29+
ws = week_start ? { week_start: week_start } : {}
2830
{
29-
default: team_profile_path(name),
30-
daily: ->(date) { team_activity_path(name, date) },
31-
weekly: ->(year, week) { team_weekly_activity_path(name, year, week) },
32-
monthly: ->(year, month) { team_monthly_activity_path(name, year, month) },
33-
contributions: ->(year) { team_contributions_path(name, year: year) }
31+
default: team_profile_path(name, **ws),
32+
daily: ->(date) { team_activity_path(name, date, **ws) },
33+
weekly: ->(year, week) { team_weekly_activity_path(name, year, week, **ws) },
34+
monthly: ->(year, month) { team_monthly_activity_path(name, year, month, **ws) },
35+
contributions: ->(year) { team_contributions_path(name, year: year, **ws) }
3436
}
3537
end
3638
end
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render "shared/profile/activity_filters", activity_filters: @activity_filters, activity_period: @activity_period, profile_routes: person_profile_routes(@profile_email), turbo_frame: "person-activity"
1+
= render "shared/profile/activity_filters", activity_filters: @activity_filters, activity_period: @activity_period, profile_routes: person_profile_routes(@profile_email, week_start: WeekCalculation.week_start_param(@week_start_day)), week_start_param: WeekCalculation.week_start_param(@week_start_day), turbo_frame: "person-activity"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render "shared/profile/contributions", title: "Activity", contribution_years: @contribution_years, contribution_year: @contribution_year, contribution_weeks: @contribution_weeks, contribution_month_spans: @contribution_month_spans, profile_routes: person_profile_routes(@profile_email), turbo_frame: "person-activity"
1+
= render "shared/profile/contributions", title: "Activity", contribution_years: @contribution_years, contribution_year: @contribution_year, contribution_weeks: @contribution_weeks, contribution_month_spans: @contribution_month_spans, weekday_labels: @weekday_labels, profile_routes: person_profile_routes(@profile_email, week_start: WeekCalculation.week_start_param(@week_start_day)), turbo_frame: "person-activity"

app/views/shared/profile/_activity_filters.html.slim

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,8 @@
22

33
.activity-filters data-controller="activity-filter"
44
= form_with url: profile_filter_url(profile_routes, activity_period), method: :get, data: { turbo_frame: turbo_frame, activity_filter_target: "form" } do |f|
5+
- if local_assigns[:week_start_param].present?
6+
= hidden_field_tag :week_start, week_start_param
57
span.filter-label Filter:
68
label.filter-checkbox
79
= check_box_tag "filters[]", "started_thread", filter_types.include?("started_thread"), data: { action: "change->activity-filter#submit" }

app/views/shared/profile/_contributions.html.slim

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ div data-controller="contrib-calendar" data-contrib-calendar-filter-selector-val
2222
- else
2323
span.contrib-week-empty = week[:week]
2424
tbody
25-
- weekday_labels = ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
25+
- weekday_labels ||= ["Mon", "Tue", "Wed", "Thu", "Fri", "Sat", "Sun"]
2626
- 7.times do |row|
2727
tr
2828
th.contrib-label = weekday_labels[row]
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render "shared/profile/activity_filters", activity_filters: @activity_filters, activity_period: @activity_period, profile_routes: team_profile_routes(@team.name), turbo_frame: "team-activity"
1+
= render "shared/profile/activity_filters", activity_filters: @activity_filters, activity_period: @activity_period, profile_routes: team_profile_routes(@team.name, week_start: WeekCalculation.week_start_param(@week_start_day)), week_start_param: WeekCalculation.week_start_param(@week_start_day), turbo_frame: "team-activity"
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
= render "shared/profile/contributions", title: "Team Activity", contribution_years: @contribution_years, contribution_year: @contribution_year, contribution_weeks: @contribution_weeks, contribution_month_spans: @contribution_month_spans, profile_routes: team_profile_routes(@team.name), turbo_frame: "team-activity"
1+
= render "shared/profile/contributions", title: "Team Activity", contribution_years: @contribution_years, contribution_year: @contribution_year, contribution_weeks: @contribution_weeks, contribution_month_spans: @contribution_month_spans, weekday_labels: @weekday_labels, profile_routes: team_profile_routes(@team.name, week_start: WeekCalculation.week_start_param(@week_start_day)), turbo_frame: "team-activity"

0 commit comments

Comments
 (0)