Skip to content

Commit

Permalink
extract finder stuff into before actions
Browse files Browse the repository at this point in the history
  • Loading branch information
klaustopher committed Dec 17, 2024
1 parent 022e6d8 commit 834bd28
Showing 1 changed file with 35 additions and 21 deletions.
56 changes: 35 additions & 21 deletions modules/costs/app/controllers/time_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,29 +32,15 @@ class TimeEntriesController < ApplicationController

before_action :require_login

with_options only: [:dialog] do
before_action :load_and_authorize_optional_project
before_action :load_and_authorize_optional_work_package
before_action :load_or_build_and_authorize_time_entry
end

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

if params[:work_package_id].present?
@work_package = WorkPackage.visible.find_by(id: params[:work_package_id])
@project = @work_package.project
end

@time_entry = if params[:time_entry_id]
# TODO: Properly handle authorization
TimeEntry.find_by(id: params[:time_entry_id])
else
TimeEntry.new(project: @project, work_package: @work_package, user: User.current)
end

if params[:date].present?
@time_entry.spent_on = params[:date]
end
Expand All @@ -68,7 +54,7 @@ def user_tz_caption
""
end

add_caption_to_input_element_via_turbo_stream("input[name=\"time_entry[user_id]\"]",
add_caption_to_input_element_via_turbo_stream('input[name="time_entry[user_id]"]',
caption:,
clean_other_captions: true)
respond_with_turbo_streams
Expand Down Expand Up @@ -124,4 +110,32 @@ def update
respond_with_turbo_streams
end
end

private

def load_and_authorize_optional_project
if params[:project_id].present?
@project = Project.visible.find(params[:project_id])
# TODO: Authorize
end
rescue ActiveRecord::NotFound
# noop
end

def load_and_authorize_optional_work_package
if params[:work_package_id].present?
@work_package = WorkPackage.visible.find_by(id: params[:work_package_id])
@project = @work_package.project
# TODO: Authorize
end
end

def load_or_build_and_authorize_time_entry
@time_entry = if params[:time_entry_id]
# TODO: Properly handle authorization
TimeEntry.find_by(id: params[:time_entry_id])
else
TimeEntry.new(project: @project, work_package: @work_package, user: User.current)
end
end
end

0 comments on commit 834bd28

Please sign in to comment.