diff --git a/app/controllers/jobs_controller.rb b/app/controllers/jobs_controller.rb index b26c01a..dc5a5a9 100644 --- a/app/controllers/jobs_controller.rb +++ b/app/controllers/jobs_controller.rb @@ -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 diff --git a/app/helpers/jobs_helper.rb b/app/helpers/jobs_helper.rb index b56b93d..380a898 100644 --- a/app/helpers/jobs_helper.rb +++ b/app/helpers/jobs_helper.rb @@ -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 diff --git a/app/models/job.rb b/app/models/job.rb index a599540..0cc18b6 100644 --- a/app/models/job.rb +++ b/app/models/job.rb @@ -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) diff --git a/app/models/time_budget.rb b/app/models/time_budget.rb index ef1af59..05ffbf1 100644 --- a/app/models/time_budget.rb +++ b/app/models/time_budget.rb @@ -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 diff --git a/app/views/jobs/_budget.html.erb b/app/views/jobs/_budget.html.erb index ef83d67..08eee42 100644 --- a/app/views/jobs/_budget.html.erb +++ b/app/views/jobs/_budget.html.erb @@ -1,4 +1,4 @@ -
- <%= 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 %> diff --git a/db/migrate/20231130172247_rename_activity_id_on_time_budgets.rb b/db/migrate/20231130172247_rename_activity_id_on_time_budgets.rb new file mode 100644 index 0000000..b4d7b91 --- /dev/null +++ b/db/migrate/20231130172247_rename_activity_id_on_time_budgets.rb @@ -0,0 +1,5 @@ +class RenameActivityIdOnTimeBudgets < ActiveRecord::Migration[6.1] + def change + rename_column :time_budgets, :activity_id, :category_id + end +end