Show jobs in time entries view

This commit is contained in:
Trevor Vallender 2023-11-22 19:44:13 +00:00
parent de3762bf1c
commit afcb4c3ccc
6 changed files with 65 additions and 4 deletions

View File

@ -1,3 +1,5 @@
include ActionView::Helpers::UrlHelper
class Job < ActiveRecord::Base class Job < ActiveRecord::Base
validates :starts_on, validates :starts_on,
:ends_on, :ends_on,
@ -5,6 +7,7 @@ class Job < ActiveRecord::Base
presence: true presence: true
belongs_to :project belongs_to :project
has_many :time_entries
scope :project, ->(project) { where(project_id: project.id) } scope :project, ->(project) { where(project_id: project.id) }
@ -17,4 +20,9 @@ class Job < ActiveRecord::Base
TimeEntry.where(job_id: id, activity_id: activity.id) TimeEntry.where(job_id: id, activity_id: activity.id)
.sum(:hours) .sum(:hours)
end end
def to_s
ActionView::Base.send(:include, Rails.application.routes.url_helpers)
ActionController::Base.helpers.link_to name, ActionController::Base.helpers.job_path(self, project_id: project.id)
end
end end

View File

@ -1,3 +1,2 @@
# English strings go here for Rails i18n
en: en:
# my_label: "My label" field_job: Job

View File

@ -10,4 +10,10 @@ Redmine::Plugin.register :jobs do
menu :project_menu, :jobs, { controller: 'jobs', action: 'index' }, caption: 'Jobs', after: :issues, param: :project_id menu :project_menu, :jobs, { controller: 'jobs', action: 'index' }, caption: 'Jobs', after: :issues, param: :project_id
TimeEntry.safe_attributes 'job_id' TimeEntry.safe_attributes 'job_id'
Rails.application.config.before_initialize do
Rails.logger.info "Patch Jobs"
TimeEntryQuery.send(:include, TimeEntryQueryPatch)
TimeEntry.send(:include, TimeEntryPatch)
end
end end

11
lib/time_entry_patch.rb Normal file
View File

@ -0,0 +1,11 @@
# frozen_string_literal: true
require_dependency 'time_entry'
module TimeEntryPatch
extend ActiveSupport::Concern
included do
belongs_to :job
end
end

View File

@ -0,0 +1,39 @@
# frozen_string_literal: true
require_dependency 'time_entry_query'
# Here we add the jobs field so it shows in a time entry query as an available column.
module TimeEntryQueryPatch
extend ActiveSupport::Concern
included do
include InstanceMethods
# alias_method :initialize_available_filters_without_jobs, :initialize_available_filters
# alias_method :initialize_available_filters, :initialize_available_filters_with_jobs
alias_method :available_columns_without_jobs, :available_columns
alias_method :available_columns, :available_columns_with_jobs
end
module InstanceMethods
# def initialize_available_filters_with_jobs
# initialize_available_filters_without_jobs
# initialize_issue_jobs_filter
# end
def available_columns_with_jobs
if @available_columns.nil?
@available_columns = available_columns_without_jobs
@available_columns << QueryColumn.new(:job)
else
available_columns_without_jobs
end
@available_columns
end
def sql_for_issue_jobs_field(_field, operator, values)
build_sql_for_jobs_field klass: Issue, operator: operator, values: values
end
end
end

View File

@ -1,5 +1,3 @@
- Show job in time logs
- Make job association required
- Add to job model - Add to job model
- Job ID? (this is in Amigo) - Job ID? (this is in Amigo)
- Status (pending acceptance etc.) - Status (pending acceptance etc.)