2023-11-22 19:44:13 +00:00
|
|
|
# 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
|
|
|
|
|
2023-11-27 14:20:28 +00:00
|
|
|
alias_method :initialize_available_filters_without_jobs, :initialize_available_filters
|
|
|
|
alias_method :initialize_available_filters, :initialize_available_filters_with_jobs
|
2023-11-22 19:44:13 +00:00
|
|
|
|
|
|
|
alias_method :available_columns_without_jobs, :available_columns
|
|
|
|
alias_method :available_columns, :available_columns_with_jobs
|
2023-12-07 13:44:10 +00:00
|
|
|
|
|
|
|
alias_method :joins_for_order_statement_without_jobs, :joins_for_order_statement
|
|
|
|
alias_method :joins_for_order_statement, :joins_for_order_statement_with_jobs
|
2023-11-22 19:44:13 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
module InstanceMethods
|
2023-11-27 14:20:28 +00:00
|
|
|
def initialize_available_filters_with_jobs
|
|
|
|
initialize_available_filters_without_jobs
|
|
|
|
initialize_issue_jobs_filter
|
|
|
|
end
|
2023-11-22 19:44:13 +00:00
|
|
|
|
|
|
|
def available_columns_with_jobs
|
|
|
|
if @available_columns.nil?
|
|
|
|
@available_columns = available_columns_without_jobs
|
2023-12-07 13:44:10 +00:00
|
|
|
@available_columns << QueryColumn.new(:job, groupable: true, sortable: -> { Job.fields_for_order_statement })
|
2023-11-22 19:44:13 +00:00
|
|
|
else
|
|
|
|
available_columns_without_jobs
|
|
|
|
end
|
|
|
|
@available_columns
|
|
|
|
end
|
|
|
|
|
2023-11-27 14:20:28 +00:00
|
|
|
def initialize_issue_jobs_filter(position: nil)
|
|
|
|
add_available_filter("job_id", order: position,
|
|
|
|
type: :list_optional,
|
|
|
|
values: jobs_list
|
|
|
|
)
|
|
|
|
end
|
|
|
|
|
2023-11-22 19:44:13 +00:00
|
|
|
def sql_for_issue_jobs_field(_field, operator, values)
|
|
|
|
build_sql_for_jobs_field klass: Issue, operator: operator, values: values
|
|
|
|
end
|
2023-11-27 14:20:28 +00:00
|
|
|
|
|
|
|
def jobs_list
|
|
|
|
Job.where(project: [project, project&.parent]).map do |job|
|
|
|
|
[job.name, job.id.to_s]
|
|
|
|
end
|
|
|
|
end
|
2023-12-07 13:44:10 +00:00
|
|
|
|
|
|
|
def joins_for_order_statement_with_jobs(order_options)
|
|
|
|
joins = joins_for_order_statement_without_jobs(order_options) || ""
|
|
|
|
|
|
|
|
if order_options
|
|
|
|
if order_options.include?('jobs')
|
|
|
|
joins += " LEFT OUTER JOIN #{Job.table_name} ON #{Job.table_name}.id = #{TimeEntry.table_name}.job_id"
|
|
|
|
end
|
|
|
|
end
|
|
|
|
joins
|
|
|
|
end
|
2023-11-22 19:44:13 +00:00
|
|
|
end
|
|
|
|
end
|