OpenProject is the leading open source project management software for product, project and portfolio management. A powerful Jira alternative with agile planning, issue tracking, roadmaps, Gantt charts, time tracking, collaboration features, and more. Available on premises or in the cloud. ⭐ Star us on GitHub
Ruby agile-developmentangularbcfboardsbug-trackerganttgantt-chartifcissue-trackerjira-alternativekanbanopenprojectproduct-managementproject-managementproject-planningroadmaprubyscrumtimelineworkflows
simplify rendering logic
Klaus Zandersc733466243de
diff --git a/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb b/modules/costs/app/components/my/time_tracking/mode_switcher_component.rbindex 429a6b85ac6..7a9460c0a9c 100644--- a/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb+++ b/modules/costs/app/components/my/time_tracking/mode_switcher_component.rb@@ -32,18 +32,19 @@ module Mymodule TimeTrackingclass ModeSwitcherComponent < ApplicationComponentoptions :current_mode,- :view_mode+ :view_mode,+ :datedef callrender(Primer::Alpha::SegmentedControl.new("aria-label": I18n.t(:label_meeting_date_time))) do |control|- %i[day week month].each do |action|+ %i[day week month].each do |mode|control.with_item(tag: :a,- href: url_for(params.permit(:controller, :date).merge(action:, view_mode: view_mode)),- icon: icon_for_mode(action),- label: t("label_#{action}"),- title: t("label_#{action}"),- selected: (current_mode == action)+ href: my_time_tracking_path(date:, view_mode:, mode:),+ icon: icon_for_mode(mode),+ label: t("label_#{mode}"),+ title: t("label_#{mode}"),+ selected: (current_mode == mode))endenddiff --git a/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb b/modules/costs/app/components/my/time_tracking/sub_header_component.html.erbindex f3cbceefe02..a823a822bb0 100644--- a/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb+++ b/modules/costs/app/components/my/time_tracking/sub_header_component.html.erb@@ -1,6 +1,6 @@<%= render(Primer::OpenProject::SubHeader.new) do |component|component.with_filter_component do- render(My::TimeTracking::ModeSwitcherComponent.new(current_mode: mode, view_mode: view_mode))+ render(My::TimeTracking::ModeSwitcherComponent.new(current_mode: mode, view_mode: view_mode, date: date))endcomponent.with_text { title }diff --git a/modules/costs/app/components/my/time_tracking/sub_header_component.rb b/modules/costs/app/components/my/time_tracking/sub_header_component.rbindex 0428a2516a2..daa5dc72d69 100644--- a/modules/costs/app/components/my/time_tracking/sub_header_component.rb+++ b/modules/costs/app/components/my/time_tracking/sub_header_component.rb@@ -54,26 +54,19 @@ module Myenddef today_href- case mode- when :day- my_time_tracking_day_path(date: Date.current, view_mode: params[:view_mode])- when :week- my_time_tracking_week_path(date: Date.current, view_mode: params[:view_mode])- when :month- my_time_tracking_month_path(date: Date.current, view_mode: params[:view_mode])- end+ my_time_tracking_path(date: Date.current, view_mode:, mode:)enddef previous_attrs # rubocop:disable Metrics/AbcSizecase modewhen :day- { href: my_time_tracking_day_path(date: date - 1.day, view_mode: params[:view_mode]),+ { href: my_time_tracking_path(date: date - 1.day, view_mode:, mode:),aria: { label: I18n.t(:label_previous_day) } }when :week- { href: my_time_tracking_week_path(date: date - 1.week, view_mode: params[:view_mode]),+ { href: my_time_tracking_path(date: date - 1.week, view_mode:, mode:),aria: { label: I18n.t(:label_previous_week) } }when :month- { href: my_time_tracking_month_path(date: date - 1.month, view_mode: params[:view_mode]),+ { href: my_time_tracking_path(date: date - 1.month, view_mode:, mode:),aria: { label: I18n.t(:label_previous_month) } }endend@@ -81,13 +74,13 @@ module Mydef next_attrs # rubocop:disable Metrics/AbcSizecase modewhen :day- { href: my_time_tracking_day_path(date: date + 1.day, view_mode: params[:view_mode]),+ { href: my_time_tracking_path(date: date + 1.day, view_mode:, mode:),aria: { label: I18n.t(:label_next_day) } }when :week- { href: my_time_tracking_week_path(date: date + 1.week, view_mode: params[:view_mode]),+ { href: my_time_tracking_path(date: date + 1.week, view_mode:, mode:),aria: { label: I18n.t(:label_next_week) } }when :month- { href: my_time_tracking_month_path(date: date + 1.month, view_mode: params[:view_mode]),+ { href: my_time_tracking_path(date: date + 1.month, view_mode:, mode:),aria: { label: I18n.t(:label_next_month) } }endenddiff --git a/modules/costs/app/controllers/my/time_tracking_controller.rb b/modules/costs/app/controllers/my/time_tracking_controller.rbindex 6111bd14d60..8b559a5390c 100644--- a/modules/costs/app/controllers/my/time_tracking_controller.rb+++ b/modules/costs/app/controllers/my/time_tracking_controller.rb@@ -30,62 +30,60 @@module Myclass TimeTrackingController < ApplicationController- before_action :require_login+ include OpTurbo::ComponentStream- no_authorization_required!(:index, :day, :week, :month)+ before_action :require_login, :view_mode, :mode, :date++ no_authorization_required!(:index, :refresh)menu_item :my_time_trackinglayout "global"- helper_method :current_day, :today?, :this_week?, :this_month?, :list_view_component, :view_mode+ helper_method :list_view_componentdef index- if mobile?- redirect_to action: :day, view_mode: default_view_mode- else- redirect_to action: :week, view_mode: default_view_mode+ case mode+ when :day then load_time_entries(date)+ when :week then load_time_entries(date.all_week)+ when :month then load_time_entries(date.all_month)endend- def day- load_time_entries(current_day)- end-- def week- load_time_entries(current_day.all_week)+ def refresh+ if mode == :month+ load_time_entries(date.all_week)+ else+ load_time_entries(date)+ endend- def month- load_time_entries(current_day.all_month)- end+ private- def today?- current_day == Time.zone.today+ def date+ @date ||= parsed_date || current_dateend- def this_week?- current_day == Time.zone.today.beginning_of_week+ def parsed_date+ if params[:date].present?+ begin+ Date.iso8601(params[:date])+ rescue StandardError+ nil+ end+ endend- def this_month?- current_day == Time.zone.today.beginning_of_month+ def default_mode+ if mobile?+ "day"+ else+ "week"+ endend- private-- def current_day- return @current_day if defined?(@current_day)-- parsed_date = if params[:date].present?- begin- Date.iso8601(params[:date])- rescue StandardError- nil- end- end-- @current_day = parsed_date || current_date+ def mode+ @mode ||= (params[:mode].presence || default_mode).to_symenddef default_view_mode@@ -97,15 +95,11 @@ module Myenddef view_mode- if ["calendar", "list"].include?(params[:view_mode])- params[:view_mode]- else- default_view_mode- end.to_sym+ @view_mode ||= (params[:view_mode].presence || default_view_mode).to_symenddef current_date- case params[:action].to_sym+ case modewhen :day then Time.zone.todaywhen :week then Time.zone.today.beginning_of_weekwhen :month then Time.zone.today.beginning_of_month@@ -124,14 +118,14 @@ module Myif view_mode == :listMy::TimeTracking::ListComponent.new(time_entries: @time_entries,- mode: params[:action].to_sym,- date: current_day+ mode: mode,+ date: date)elseMy::TimeTracking::CalendarComponent.new(time_entries: @time_entries,- mode: params[:action].to_sym,- date: current_day+ mode: mode,+ date: date)endenddiff --git a/modules/costs/app/views/my/time_tracking/day.html.erb b/modules/costs/app/views/my/time_tracking/index.html.erbsimilarity index 100%rename from modules/costs/app/views/my/time_tracking/day.html.erbrename to modules/costs/app/views/my/time_tracking/index.html.erbdiff --git a/modules/costs/app/views/my/time_tracking/month.html.erb b/modules/costs/app/views/my/time_tracking/month.html.erbdeleted file mode 100644index b3f3712cf7d..00000000000--- a/modules/costs/app/views/my/time_tracking/month.html.erb+++ /dev/null@@ -1 +0,0 @@-<%= render(list_view_component) %>diff --git a/modules/costs/app/views/my/time_tracking/week.html.erb b/modules/costs/app/views/my/time_tracking/week.html.erbdeleted file mode 100644index b3f3712cf7d..00000000000--- a/modules/costs/app/views/my/time_tracking/week.html.erb+++ /dev/null@@ -1 +0,0 @@-<%= render(list_view_component) %>diff --git a/modules/costs/config/routes.rb b/modules/costs/config/routes.rbindex 642a837e29f..856b91dc7e4 100644--- a/modules/costs/config/routes.rb+++ b/modules/costs/config/routes.rb@@ -50,13 +50,15 @@ Rails.application.routes.draw donamespace "my" doget "/timer" => "timer#show", as: "timers"- get "/time-tracking" => "time_tracking#index"- get "/time-tracking/day(-:view_mode)(/:date)" => "time_tracking#day",- as: :time_tracking_day- get "/time-tracking/week(-:view_mode)(/:date)" => "time_tracking#week",- as: :time_tracking_week- get "/time-tracking/month(-:view_mode)(/:date)" => "time_tracking#month",- as: :time_tracking_month+ get "/time-tracking/(:mode-:view_mode)(/:date)" => "time_tracking#index",+ as: :time_tracking,+ constraints: {+ mode: /day|week|month/,+ view_mode: /list|calendar/,+ date: /\d{4}-\d{2}-\d{2}/+ }+ get "/time-tracking/refresh" => "time_tracking#refresh",+ as: :time_tracking_refreshendscope "projects/:project_id", as: "project", module: "projects" dodiff --git a/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb b/modules/costs/spec/controllers/my/time_tracking_controller_spec.rbindex 1ec0f15ef15..eefcebe57c0 100644--- a/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb+++ b/modules/costs/spec/controllers/my/time_tracking_controller_spec.rb@@ -48,9 +48,10 @@ RSpec.describe My::TimeTrackingController doallow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(true)end- it "redirects to the week calendar view" do+ it "renders the calendar week view" doget :index- expect(response).to redirect_to(action: :week, view_mode: "calendar")+ expect(assigns(:mode)).to eq(:week)+ expect(assigns(:view_mode)).to eq(:calendar)endend@@ -59,9 +60,10 @@ RSpec.describe My::TimeTrackingController doallow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(false)end- it "redirects to the week list view" do+ it "renders the week list view" doget :index- expect(response).to redirect_to(action: :week, view_mode: "list")+ expect(assigns(:mode)).to eq(:week)+ expect(assigns(:view_mode)).to eq(:list)endendend@@ -76,9 +78,10 @@ RSpec.describe My::TimeTrackingController doallow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(true)end- it "redirects to the day calendar view" do+ it "renders the day calendar view" doget :index- expect(response).to redirect_to(action: :day, view_mode: "calendar")+ expect(assigns(:mode)).to eq(:day)+ expect(assigns(:view_mode)).to eq(:calendar)endend@@ -87,9 +90,10 @@ RSpec.describe My::TimeTrackingController doallow(TimeEntry).to receive(:can_track_start_and_end_time?).and_return(false)end- it "redirects to the day list view" do+ it "renders the day list view" doget :index- expect(response).to redirect_to(action: :day, view_mode: "list")+ expect(assigns(:mode)).to eq(:day)+ expect(assigns(:view_mode)).to eq(:list)endendend@@ -97,52 +101,52 @@ RSpec.describe My::TimeTrackingController dodescribe "GET /my/time-tracking/day" doit "without a date param it uses the current date" do- get :day- expect(assigns(:current_day)).to eq(Date.current)+ get :index, params: { mode: :day }+ expect(assigns(:date)).to eq(Date.current)endit "with a date param it uses the given date" do- get :day, params: { date: "2023-12-31" }- expect(assigns(:current_day)).to eq(Date.parse("2023-12-31"))+ get :index, params: { mode: :day, date: "2023-12-31" }+ expect(assigns(:date)).to eq(Date.parse("2023-12-31"))endit "with an invalid date param it uses the current date" do- get :day, params: { date: "invalid-date" }- expect(assigns(:current_day)).to eq(Date.current)+ get :index, params: { mode: :day, date: "invalid-date" }+ expect(assigns(:date)).to eq(Date.current)endenddescribe "GET /my/time-tracking/week" doit "without a date param it uses the beginning of current week" do- get :week- expect(assigns(:current_day)).to eq(Date.current.beginning_of_week)+ get :index, params: { mode: :week }+ expect(assigns(:date)).to eq(Date.current.beginning_of_week)endit "with a date param it uses the given date" do- get :week, params: { date: "2023-12-31" }- expect(assigns(:current_day)).to eq(Date.parse("2023-12-31"))+ get :index, params: { mode: :week, date: "2023-12-31" }+ expect(assigns(:date)).to eq(Date.parse("2023-12-31"))endit "with an invalid date param it uses the beginning of current week" do- get :week, params: { date: "invalid-date" }- expect(assigns(:current_day)).to eq(Date.current.beginning_of_week)+ get :index, params: { mode: :week, date: "invalid-date" }+ expect(assigns(:date)).to eq(Date.current.beginning_of_week)endenddescribe "GET /my/time-tracking/month" doit "without a date param it uses the beginning of current month" do- get :month- expect(assigns(:current_day)).to eq(Date.current.beginning_of_month)+ get :index, params: { mode: :month }+ expect(assigns(:date)).to eq(Date.current.beginning_of_month)endit "with a date param it uses the given date" do- get :month, params: { date: "2023-12-31" }- expect(assigns(:current_day)).to eq(Date.parse("2023-12-31"))+ get :index, params: { mode: :month, date: "2023-12-31" }+ expect(assigns(:date)).to eq(Date.parse("2023-12-31"))endit "with an invalid date param it uses the beginning of current month" do- get :month, params: { date: "invalid-date" }- expect(assigns(:current_day)).to eq(Date.current.beginning_of_month)+ get :index, params: { mode: :month, date: "invalid-date" }+ expect(assigns(:date)).to eq(Date.current.beginning_of_month)endendend