Allow time budgets without an activity

This commit is contained in:
Trevor Vallender 2023-11-24 16:51:15 +00:00
parent 4b83753e71
commit c8c7c195e8
4 changed files with 12 additions and 8 deletions

View File

@ -15,8 +15,9 @@ class Job < ActiveRecord::Base
scope :project_or_parent, ->(project) { where(project_id: [project.id, project.parent&.id]) }
def with_all_time_budgets
time_budgets.build(job_id: id, activity_id: nil) unless time_budgets.where(activity_id: nil).exists?
TimeEntryActivity.where.not(id: time_budgets.pluck(:activity_id)).each do |activity|
time_budgets << TimeBudget.new(job_id: id, activity_id: activity.id)
time_budgets.build(job_id: id, activity_id: activity.id)
end
self
end
@ -42,7 +43,7 @@ class Job < ActiveRecord::Base
end
def total_time_logged_for(activity)
TimeEntry.where(job_id: id, activity_id: activity.id)
TimeEntry.where(job_id: id, activity_id: activity&.id)
.sum(:hours)
end

View File

@ -3,9 +3,11 @@ class TimeBudget < ActiveRecord::Base
presence: true
validates :activity_id,
inclusion: { in: TimeEntryActivity.pluck(:id) }
inclusion: { in: TimeEntryActivity.pluck(:id) },
allow_nil: true
validates_uniqueness_of :job_id, scope: :activity_id, message: "Only one time budget can exist for each activity type"
belongs_to :job
belongs_to :activity, class_name: "TimeEntryActivity"
end

View File

@ -1,4 +1,4 @@
<div class="label"><%= budget.activity.name %></div>
<div class="label"><%= budget.activity&.name || "Unassigned" %></div>
<div class="value">
<%= l_hours_short(
budget.job.total_time_logged_for(budget.activity)

View File

@ -1,4 +1,5 @@
<%= form_with model: @job, id: "job_form" do |f| %>
<%= form_with model: @job, url: job_path(@job, project_id: @job.project.id), id: "job_form" do |f| %>
<%= error_messages_for @job %>
<div class="box tabular">
<p>
<%= f.label :name %>
@ -16,7 +17,7 @@
<%= f.label :ends_on %>
<%= f.date_field :ends_on %>
<%= f.hidden_field :project_id, value: @project.id %>
<%= f.hidden_field :project_id, value: @job.project.id %>
<p>
<%= f.label :external_project_id %>
@ -26,7 +27,7 @@
<legend>Budget</legend>
<%= f.fields_for :time_budgets do |ff| %>
<p>
<%= ff.label :hours, ff.object.activity.name %>
<%= ff.label :hours, ff.object.activity&.name || "Unassigned" %>
<%= ff.number_field :hours %>
<%= ff.hidden_field :activity_id %>
<%= ff.hidden_field :job_id %>