diff --git a/app/models/job.rb b/app/models/job.rb index 55d3a29..8157c1d 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -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 diff --git a/app/models/time_budget.rb b/app/models/time_budget.rb index 83198b4..ef1af59 100644 --- a/app/models/time_budget.rb +++ b/app/models/time_budget.rb @@ -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 diff --git a/app/views/jobs/_budget.html.erb b/app/views/jobs/_budget.html.erb index 1f54c16..284e127 100644 --- a/app/views/jobs/_budget.html.erb +++ b/app/views/jobs/_budget.html.erb @@ -1,4 +1,4 @@ -
<%= 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 %>
<%= f.label :external_project_id %> @@ -26,7 +27,7 @@ <%= f.fields_for :time_budgets do |ff| %>
- <%= 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 %>