Time budgets use categories, not activities

Refs #2142
This commit is contained in:
Trevor Vallender 2023-11-30 17:37:54 +00:00
parent 6bc0ef80fb
commit 983e6a3b6a
7 changed files with 23 additions and 31 deletions

View File

@ -52,7 +52,7 @@ class JobsController < ApplicationController
:budget,
:name,
:description,
time_budgets_attributes: [:id, :activity_id, :hours, :job_id, :_destroy]
time_budgets_attributes: [:id, :category_id, :hours, :job_id, :_destroy]
)
end

View File

@ -7,9 +7,6 @@ module JobsHelper
end
def progress_bar_for(budget)
progress_bar(budget.job.done_ratio_for(budget.activity),
legend: "#{budget.job.done_ratio_for(budget.activity)}%
(#{l_hours_short(budget.job.total_time_logged_for(budget.activity))}/#{l_hours_short(budget.hours)})",
class: "progress")
l_hours_short(budget.hours)
end
end

View File

@ -15,16 +15,16 @@ class Job < ActiveRecord::Base
scope :active, -> { where(starts_on: ..Date.today, ends_on: Date.today..) }
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.build(job_id: id, activity_id: activity.id)
time_budgets.build(job_id: id, category_id: nil) unless time_budgets.where(category_id: nil).exists?
TimeBudgetCategory.where.not(id: time_budgets.pluck(:category_id)).each do |category|
time_budgets.build(job_id: id, category_id: category.id)
end
self
end
def missing_time_budgets
budgets = []
new_activities.collect { |activity| budgets << TimeBudget.new(job_id: id, activity_id: activity.id) }
new_activities.collect { |category| budgets << TimeBudget.new(job_id: id, category_id: category.id) }
end
def total_time_budget
@ -33,10 +33,10 @@ class Job < ActiveRecord::Base
time_budgets.sum(&:hours)
end
def time_budget_for(activity)
return 0 if activity.nil? || time_budgets.find_by(activity_id: activity.id).nil?
def time_budget_for(category)
return 0 if category.nil? || time_budgets.find_by(category_id: category.id).nil?
time_budgets.find_by(activity_id: activity.id).hours
time_budgets.find_by(category_id: category.id).hours
end
def total_time_logged
@ -44,22 +44,12 @@ class Job < ActiveRecord::Base
.sum(:hours)
end
def total_time_logged_for(activity)
TimeEntry.where(job_id: id, activity_id: activity&.id)
.sum(:hours)
end
def done_ratio
return 0 if total_time_budget.zero?
(total_time_logged / total_time_budget * 100).to_i
end
def done_ratio_for(activity)
return 0 if total_time_logged_for(activity).zero?
(total_time_logged_for(activity) / time_budget_for(activity) * 100).to_i
end
def to_s
ActionView::Base.send(:include, Rails.application.routes.url_helpers)
ActionController::Base.helpers.link_to name, ActionController::Base.helpers.project_job_path(project, self)

View File

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

View File

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

View File

@ -23,9 +23,9 @@
<legend>Budget</legend>
<%= f.fields_for :time_budgets do |ff| %>
<p>
<%= ff.label :hours, ff.object.activity&.name || "Unassigned" %>
<%= ff.label :hours, ff.object.category&.name || "Unassigned" %>
<%= ff.number_field :hours %>
<%= ff.hidden_field :activity_id %>
<%= ff.hidden_field :category_id %>
<%= ff.hidden_field :job_id %>
<%= ff.hidden_field :_destroy, value: false %>
<% end %>

View File

@ -0,0 +1,5 @@
class RenameActivityIdOnTimeBudgets < ActiveRecord::Migration[6.1]
def change
rename_column :time_budgets, :activity_id, :category_id
end
end