Redmine-Jobs/app/models/job.rb

55 lines
1.6 KiB
Ruby
Raw Normal View History

2023-11-22 19:44:13 +00:00
include ActionView::Helpers::UrlHelper
2023-11-16 20:23:09 +00:00
class Job < ActiveRecord::Base
validates :starts_on,
:ends_on,
:name,
presence: true
2023-11-21 14:09:51 +00:00
belongs_to :project
2023-11-27 07:53:55 +00:00
has_many :time_entries, dependent: :restrict_with_error, message: "You cannot delete a job with time logged against it."
has_many :time_budgets, dependent: :destroy
accepts_nested_attributes_for :time_budgets, allow_destroy: true
2023-11-21 14:09:51 +00:00
2023-11-21 13:59:43 +00:00
scope :project, ->(project) { where(project_id: project.id) }
scope :project_or_parent, ->(project) { where(project_id: [project.id, project.parent&.id]) }
def with_all_time_budgets
2023-11-24 16:51:15 +00:00
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|
2023-11-24 16:51:15 +00:00
time_budgets.build(job_id: id, activity_id: activity.id)
end
self
end
def missing_time_budgets
budgets = []
new_activities.collect { |activity| budgets << TimeBudget.new(job_id: id, activity_id: activity.id) }
end
def total_time_budget
return 0 if time_budgets.empty?
time_budgets.sum(&:hours)
end
def time_budget_for(activity)
time_budgets.where(activity_id: activity.id).hours || 0
end
2023-11-21 13:59:43 +00:00
2023-11-21 14:37:11 +00:00
def total_time_logged
TimeEntry.where(job_id: id)
.sum(:hours)
end
def total_time_logged_for(activity)
2023-11-24 16:51:15 +00:00
TimeEntry.where(job_id: id, activity_id: activity&.id)
2023-11-21 14:37:11 +00:00
.sum(:hours)
2023-11-16 20:23:09 +00:00
end
2023-11-22 19:44:13 +00:00
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)
2023-11-22 19:44:13 +00:00
end
2023-11-16 20:23:09 +00:00
end