scinote-web/app/services/lists/experiments_service.rb

76 lines
3.1 KiB
Ruby
Raw Normal View History

2023-12-07 03:53:11 +08:00
# frozen_string_literal: true
module Lists
class ExperimentsService < BaseService
private
def fetch_records
2023-12-12 19:17:38 +08:00
@records = @raw_data.joins(:project)
2023-12-07 03:53:11 +08:00
.includes(my_modules: { my_module_status: :my_module_status_implications })
.includes(workflowimg_attachment: :blob, user_assignments: %i(user_role user))
.joins('LEFT OUTER JOIN my_modules AS active_tasks ON
active_tasks.experiment_id = experiments.id
AND active_tasks.archived = FALSE')
.joins('LEFT OUTER JOIN my_modules AS active_completed_tasks ON
active_completed_tasks.experiment_id = experiments.id
AND active_completed_tasks.archived = FALSE AND active_completed_tasks.state = 1')
.readable_by_user(@user)
.select('experiments.*')
.select('COUNT(DISTINCT active_tasks.id) AS task_count')
.select('COUNT(DISTINCT active_completed_tasks.id) AS completed_task_count')
.group('experiments.id')
view_mode = if @params[:project].archived?
'archived'
else
@params[:view_mode] || 'active'
end
2023-12-12 19:17:38 +08:00
@records = @records.archived if view_mode == 'archived' && @params[:project].active?
@records = @records.active if view_mode == 'active'
2023-12-07 03:53:11 +08:00
end
2023-12-12 19:17:38 +08:00
def filter_records
2023-12-11 22:41:03 +08:00
if @filters[:query].present?
2023-12-12 19:17:38 +08:00
@records = @records.where_attributes_like(
2023-12-11 22:41:03 +08:00
['experiments.name', 'experiments.description', "('EX' || experiments.id)"],
@filters[:query]
)
end
2023-12-07 03:53:11 +08:00
2023-12-11 22:41:03 +08:00
if @filters[:created_at_from].present?
2023-12-12 19:17:38 +08:00
@records = @records.where('experiments.created_at > ?', @filters[:created_at_from])
2023-12-11 22:41:03 +08:00
end
if @filters[:created_at_to].present?
2023-12-12 19:17:38 +08:00
@records = @records.where('experiments.created_at < ?',
2023-12-11 22:41:03 +08:00
@filters[:created_at_to])
end
if @filters[:updated_on_from].present?
2023-12-12 19:17:38 +08:00
@records = @records.where('experiments.updated_at > ?', @filters[:updated_on_from])
2023-12-11 22:41:03 +08:00
end
if @filters[:updated_on_to].present?
2023-12-12 19:17:38 +08:00
@records = @records.where('experiments.updated_at < ?',
2023-12-11 22:41:03 +08:00
@filters[:updated_on_to])
end
if @filters[:archived_on_from].present?
2023-12-12 19:17:38 +08:00
@records = @records.where('COALESCE(experiments.archived_on, projects.archived_on) > ?',
2023-12-11 22:41:03 +08:00
@filters[:archived_on_from])
end
if @filters[:archived_on_to].present?
2023-12-12 19:17:38 +08:00
@records = @records.where('COALESCE(experiments.archived_on, projects.archived_on) < ?',
2023-12-11 22:41:03 +08:00
@filters[:archived_on_to])
end
2023-12-07 03:53:11 +08:00
end
def sortable_columns
@sortable_columns ||= {
created_at: 'experiments.created_at',
name: 'experiments.name',
code: 'experiments.id',
archived_on: 'COALESCE(experiments.archived_on, projects.archived_on)'
}
end
end
end