parent
6bc0ef80fb
commit
983e6a3b6a
|
@ -52,7 +52,7 @@ class JobsController < ApplicationController
|
||||||
:budget,
|
:budget,
|
||||||
:name,
|
:name,
|
||||||
:description,
|
:description,
|
||||||
time_budgets_attributes: [:id, :activity_id, :hours, :job_id, :_destroy]
|
time_budgets_attributes: [:id, :category_id, :hours, :job_id, :_destroy]
|
||||||
)
|
)
|
||||||
end
|
end
|
||||||
|
|
||||||
|
|
|
@ -7,9 +7,6 @@ module JobsHelper
|
||||||
end
|
end
|
||||||
|
|
||||||
def progress_bar_for(budget)
|
def progress_bar_for(budget)
|
||||||
progress_bar(budget.job.done_ratio_for(budget.activity),
|
l_hours_short(budget.hours)
|
||||||
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")
|
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
|
|
|
@ -15,16 +15,16 @@ class Job < ActiveRecord::Base
|
||||||
scope :active, -> { where(starts_on: ..Date.today, ends_on: Date.today..) }
|
scope :active, -> { where(starts_on: ..Date.today, ends_on: Date.today..) }
|
||||||
|
|
||||||
def with_all_time_budgets
|
def with_all_time_budgets
|
||||||
time_budgets.build(job_id: id, activity_id: nil) unless time_budgets.where(activity_id: nil).exists?
|
time_budgets.build(job_id: id, category_id: nil) unless time_budgets.where(category_id: nil).exists?
|
||||||
TimeEntryActivity.where.not(id: time_budgets.pluck(:activity_id)).each do |activity|
|
TimeBudgetCategory.where.not(id: time_budgets.pluck(:category_id)).each do |category|
|
||||||
time_budgets.build(job_id: id, activity_id: activity.id)
|
time_budgets.build(job_id: id, category_id: category.id)
|
||||||
end
|
end
|
||||||
self
|
self
|
||||||
end
|
end
|
||||||
|
|
||||||
def missing_time_budgets
|
def missing_time_budgets
|
||||||
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
|
end
|
||||||
|
|
||||||
def total_time_budget
|
def total_time_budget
|
||||||
|
@ -33,10 +33,10 @@ class Job < ActiveRecord::Base
|
||||||
time_budgets.sum(&:hours)
|
time_budgets.sum(&:hours)
|
||||||
end
|
end
|
||||||
|
|
||||||
def time_budget_for(activity)
|
def time_budget_for(category)
|
||||||
return 0 if activity.nil? || time_budgets.find_by(activity_id: activity.id).nil?
|
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
|
end
|
||||||
|
|
||||||
def total_time_logged
|
def total_time_logged
|
||||||
|
@ -44,22 +44,12 @@ class Job < ActiveRecord::Base
|
||||||
.sum(:hours)
|
.sum(:hours)
|
||||||
end
|
end
|
||||||
|
|
||||||
def total_time_logged_for(activity)
|
|
||||||
TimeEntry.where(job_id: id, activity_id: activity&.id)
|
|
||||||
.sum(:hours)
|
|
||||||
end
|
|
||||||
|
|
||||||
|
|
||||||
def done_ratio
|
def done_ratio
|
||||||
|
return 0 if total_time_budget.zero?
|
||||||
|
|
||||||
(total_time_logged / total_time_budget * 100).to_i
|
(total_time_logged / total_time_budget * 100).to_i
|
||||||
end
|
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
|
def to_s
|
||||||
ActionView::Base.send(:include, Rails.application.routes.url_helpers)
|
ActionView::Base.send(:include, Rails.application.routes.url_helpers)
|
||||||
ActionController::Base.helpers.link_to name, ActionController::Base.helpers.project_job_path(project, self)
|
ActionController::Base.helpers.link_to name, ActionController::Base.helpers.project_job_path(project, self)
|
||||||
|
|
|
@ -2,12 +2,12 @@ class TimeBudget < ActiveRecord::Base
|
||||||
validates :hours,
|
validates :hours,
|
||||||
presence: true
|
presence: true
|
||||||
|
|
||||||
validates :activity_id,
|
validates :category_id,
|
||||||
inclusion: { in: TimeEntryActivity.pluck(:id) },
|
inclusion: { in: TimeBudgetCategory.pluck(:id) },
|
||||||
allow_nil: true
|
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 :job
|
||||||
belongs_to :activity, class_name: "TimeEntryActivity"
|
belongs_to :category, class_name: "TimeBudgetCategory"
|
||||||
end
|
end
|
||||||
|
|
|
@ -1,4 +1,4 @@
|
||||||
<div class="label"><%= budget.activity&.name || "Unassigned" %></div>
|
<div class="label"><%= budget.category&.name || "Unassigned" %></div>
|
||||||
<div class="value">
|
<div class="value">
|
||||||
<%= progress_bar_for(budget) %>
|
<%= progress_bar_for(budget) %>
|
||||||
</div>
|
</div>
|
||||||
|
|
|
@ -23,9 +23,9 @@
|
||||||
<legend>Budget</legend>
|
<legend>Budget</legend>
|
||||||
<%= f.fields_for :time_budgets do |ff| %>
|
<%= f.fields_for :time_budgets do |ff| %>
|
||||||
<p>
|
<p>
|
||||||
<%= ff.label :hours, ff.object.activity&.name || "Unassigned" %>
|
<%= ff.label :hours, ff.object.category&.name || "Unassigned" %>
|
||||||
<%= ff.number_field :hours %>
|
<%= ff.number_field :hours %>
|
||||||
<%= ff.hidden_field :activity_id %>
|
<%= ff.hidden_field :category_id %>
|
||||||
<%= ff.hidden_field :job_id %>
|
<%= ff.hidden_field :job_id %>
|
||||||
<%= ff.hidden_field :_destroy, value: false %>
|
<%= ff.hidden_field :_destroy, value: false %>
|
||||||
<% end %>
|
<% end %>
|
||||||
|
|
|
@ -0,0 +1,5 @@
|
||||||
|
class RenameActivityIdOnTimeBudgets < ActiveRecord::Migration[6.1]
|
||||||
|
def change
|
||||||
|
rename_column :time_budgets, :activity_id, :category_id
|
||||||
|
end
|
||||||
|
end
|
Loading…
Reference in New Issue