Skip to content

Commit

Permalink
fix form handling
Browse files Browse the repository at this point in the history
  • Loading branch information
klaustopher committed Dec 12, 2024
1 parent 82be505 commit 6080475
Show file tree
Hide file tree
Showing 7 changed files with 65 additions and 24 deletions.
4 changes: 3 additions & 1 deletion modules/costs/app/components/time_entries/time_entry_form.rb
Original file line number Diff line number Diff line change
Expand Up @@ -117,10 +117,12 @@ def show_start_and_end_time_fields?
end

def activities
return [] if project.blank?

TimeEntryActivity.active_in_project(project)
end

def user_autocompleter_filter_options
def user_completer_filter_options
filters = [
{ name: "type", operator: "=", values: %w[User Group] },
{ name: "status", operator: "=", values: [Principal.statuses[:active], Principal.statuses[:invited]] }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,13 +53,13 @@ def form_options

if time_entry.persisted?
base.merge({
url: project_time_entry_path(project, time_entry),
url: time_entry_path(time_entry),
method: :patch
})
else

base.merge({
url: project_time_entries_path(project),
url: time_entries_path,
method: :post
})
end
Expand Down
33 changes: 18 additions & 15 deletions modules/costs/app/controllers/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -31,23 +31,30 @@ class TimeEntriesController < ApplicationController
include OpTurbo::DialogStreamHelper

before_action :require_login
before_action :find_project_by_project_id

authorization_checked! :dialog, :create, :update, :user_caption
authorization_checked! :dialog, :create, :update, :user_tz_caption, :time_entry_activities

def dialog
if params[:project_id].present?
@project = begin
Project.visible.find(params[:project_id])
rescue ActiveRecord::NotFound
nil
end
end
@work_package = WorkPackage.visible.find_by(id: params[:work_package_id]) if params[:work_package_id].present?
@time_entry = if params[:time_entry_id]
# TODO: Properly handle authorization
TimeEntry.find_by(id: params[:time_entry_id])
else
TimeEntry.new(project: @project, user: User.current)
TimeEntry.new(project: @project, work_package: @work_package, user: User.current)
end
end

Check notice on line 52 in modules/costs/app/controllers/time_entries_controller.rb

View workflow job for this annotation

GitHub Actions / rubocop

[rubocop] modules/costs/app/controllers/time_entries_controller.rb#L37-L52 <Metrics/AbcSize>

Assignment Branch Condition size for dialog is too high. [<3, 21, 5> 21.79/17]
Raw output
modules/costs/app/controllers/time_entries_controller.rb:37:3: C: Metrics/AbcSize: Assignment Branch Condition size for dialog is too high. [<3, 21, 5> 21.79/17]

def user_caption
def user_tz_caption
user = User.visible.find_by(id: params[:user_id])
caption = if user && user.time_zone != User.current.time_zone
I18n.t("notice_different_time_zones", tz: user.time_zone.name)
I18n.t("notice_different_time_zones", tz: helpers.friendly_timezone_name(user.time_zone))
else
""
end
Expand All @@ -58,10 +65,14 @@ def user_caption
respond_with_turbo_streams
end

def time_entry_activities
respond_with_turbo_streams
end

def create
call = TimeEntries::CreateService
.new(user: current_user)
.call(time_entry_params)
.call(permitted_params.time_entries)

@time_entry = call.result

Expand All @@ -80,7 +91,7 @@ def update

call = TimeEntries::UpdateService
.new(user: current_user, model: time_entry)
.call(time_entry_params)
.call(permitted_params.time_entries)

@time_entry = call.result

Expand All @@ -93,12 +104,4 @@ def update
respond_with_turbo_streams
end
end

private

def time_entry_params
permitted_params.time_entries.merge(
project_id: @project.id
)
end
end
36 changes: 36 additions & 0 deletions modules/costs/app/helpers/time_entries_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#-- copyright
# OpenProject is an open source project management software.
# Copyright (C) the OpenProject GmbH
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License version 3.
#
# OpenProject is a fork of ChiliProject, which is a fork of Redmine. The copyright follows:
# Copyright (C) 2006-2013 Jean-Philippe Lang
# Copyright (C) 2010-2013 the ChiliProject Team
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
#
# See COPYRIGHT and LICENSE files for more details.
#++

module TimeEntriesHelper
def friendly_timezone_name(time_zone)
tz_info = time_zone.tzinfo
friendly_name = ActiveSupport::TimeZone::MAPPING.key(tz_info.canonical_zone.name)

"(UTC#{ActiveSupport::TimeZone.seconds_to_utc_offset(tz_info.base_utc_offset)}) #{friendly_name}"
end
end
8 changes: 4 additions & 4 deletions modules/costs/config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@
Rails.application.routes.draw do
resources :time_entries, only: %i[create update] do
get :dialog, on: :collection
get "users/:user_id/tz_caption", action: :user_caption, on: :collection
get "work_packages/:work_package_id/time_entry_activities", action: :change_activities_input, on: :collection
get "/users/:user_id/tz_caption", action: :user_tz_caption, on: :collection
get "/work_packages/:work_package_id/time_entry_activities", action: :change_activities_input, on: :collection
end

scope "projects/:project_id", as: "projects" do
Expand All @@ -39,6 +39,8 @@
resources :hourly_rates, only: %i[show edit update] do
post :set_rate, on: :member
end

get "/time_entries/dialog" => "time_entries#dialog"
end

scope "my" do
Expand All @@ -49,8 +51,6 @@
namespace "settings" do
resource :time_entry_activities, only: %i[show update]
end

get "/time_entries/dialog" => "time_entries#dialog"
end

scope "work_packages/:work_package_id", as: "work_packages" do
Expand Down
4 changes: 2 additions & 2 deletions modules/overviews/app/views/overviews/overviews/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -74,13 +74,13 @@ end %>
<%# TEMPORARY # %>
<%- if Rails.env.development? %>
<%= link_to "log time",
dialog_project_time_entries_path(@project),
projects_time_entries_dialog_path(@project),
data: {
controller: "async-dialog",
test_selector: "toggle-log-time-dialog-button",
} %>
<%= link_to "edit log time",
dialog_project_time_entries_path(@project, time_entry_id: TimeEntry.first.id),
projects_time_entries_dialog_path(@project, time_entry_id: TimeEntry.first.id),
data: {
controller: "async-dialog",
test_selector: "toggle-log-time-dialog-button",
Expand Down

0 comments on commit 6080475

Please sign in to comment.