Allow sorting/grouping by jobs

This takes affect in the time entry view.
This commit is contained in:
Trevor Vallender 2023-12-07 13:44:10 +00:00
parent bcb669745e
commit a74d1ad2ea
2 changed files with 19 additions and 1 deletions

View File

@ -51,6 +51,10 @@ class Job < ActiveRecord::Base
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)
end end
def self.fields_for_order_statement
"jobs.name"
end
def self.default_for(time_entry) def self.default_for(time_entry)
projects = [time_entry.project, time_entry.project.parent] projects = [time_entry.project, time_entry.project.parent]
jobs = Job.where(project: projects).active jobs = Job.where(project: projects).active

View File

@ -14,6 +14,9 @@ module TimeEntryQueryPatch
alias_method :available_columns_without_jobs, :available_columns alias_method :available_columns_without_jobs, :available_columns
alias_method :available_columns, :available_columns_with_jobs alias_method :available_columns, :available_columns_with_jobs
alias_method :joins_for_order_statement_without_jobs, :joins_for_order_statement
alias_method :joins_for_order_statement, :joins_for_order_statement_with_jobs
end end
module InstanceMethods module InstanceMethods
@ -25,7 +28,7 @@ module TimeEntryQueryPatch
def available_columns_with_jobs def available_columns_with_jobs
if @available_columns.nil? if @available_columns.nil?
@available_columns = available_columns_without_jobs @available_columns = available_columns_without_jobs
@available_columns << QueryColumn.new(:job) @available_columns << QueryColumn.new(:job, groupable: true, sortable: -> { Job.fields_for_order_statement })
else else
available_columns_without_jobs available_columns_without_jobs
end end
@ -48,5 +51,16 @@ module TimeEntryQueryPatch
[job.name, job.id.to_s] [job.name, job.id.to_s]
end end
end end
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
end end
end end