Auto-assign time to jobs

If a job is not specified, a reasonable choice will be made.

Refs #2111
This commit is contained in:
Trevor Vallender 2023-12-05 14:25:24 +00:00
parent 40eb43e0c5
commit a71ffbcacc
4 changed files with 32 additions and 1 deletions

View File

@ -26,7 +26,7 @@ class JobsController < ApplicationController
end
def create
@job = Job.new(job_params)
@job = Job.new(remove_empty_time_budgets(job_params))
if @job.save
redirect_to project_job_path(@job.project, @job)
else

View File

@ -50,4 +50,23 @@ class Job < ActiveRecord::Base
ActionView::Base.send(:include, Rails.application.routes.url_helpers)
ActionController::Base.helpers.link_to name, ActionController::Base.helpers.project_job_path(project, self)
end
def self.default_for(time_entry)
projects = [time_entry.project, time_entry.project.parent]
jobs = Job.where(project: projects).active
support = jobs.where(category: JobCategory.support).first
retainer = jobs.where(category: JobCategory.retainer).first
sprints = jobs.where(category: JobCategory.sprints).first
priority_list = [sprints, retainer, support].compact
return jobs.first if priority_list.empty?
return support if time_entry.activity.name == "Support"
return priority_list.first if time_entry.issue.blank?
return support if time_entry.issue.tracker.name == "Support"
priority_list.first
end
end

View File

@ -5,6 +5,10 @@ class JobCategory < Enumeration
OptionName = :enumeration_job_category
scope :support, -> { where(name: 'Support').first }
scope :retainer, -> { where(name: 'Retainer').first }
scope :sprints, -> { where(name: 'Sprints').first }
def option_name
OptionName
end

View File

@ -7,5 +7,13 @@ module TimeEntryPatch
included do
belongs_to :job
before_save :set_job, unless: :job
def set_job
return if job.present?
self.job = Job.default_for(self)
end
end
end