mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-03-02 02:35:39 +08:00
Split joins scope
This commit is contained in:
parent
bfaf5a1786
commit
a32a3db8e5
2 changed files with 41 additions and 27 deletions
|
@ -21,30 +21,38 @@ class Activity < ApplicationRecord
|
|||
validates :subject_type, inclusion: { in: Extends::ACTIVITY_SUBJECT_TYPES,
|
||||
allow_blank: true }
|
||||
|
||||
scope :subjects_joins, lambda {
|
||||
joins("
|
||||
LEFT JOIN results ON
|
||||
subject_type = 'Result'
|
||||
AND subject_id = results.id
|
||||
LEFT JOIN protocols ON
|
||||
subject_type = 'Protocol'
|
||||
AND subject_id = protocols.id
|
||||
LEFT JOIN my_modules ON
|
||||
(subject_type = 'MyModule' AND subject_id = my_modules.id)
|
||||
OR protocols.my_module_id = my_modules.id
|
||||
OR results.my_module_id = my_modules.id
|
||||
LEFT JOIN experiments ON
|
||||
(subject_type = 'Experiment' AND subject_id = experiments.id)
|
||||
OR experiments.id = my_modules.experiment_id
|
||||
LEFT JOIN projects ON
|
||||
(subject_type = 'Project' AND subject_id = projects.id)
|
||||
OR projects.id = experiments.project_id
|
||||
LEFT JOIN repositories ON
|
||||
subject_type = 'Repository'
|
||||
AND subject_id = repositories.id
|
||||
LEFT JOIN reports ON subject_type = 'Report'
|
||||
AND subject_id = reports.id
|
||||
")
|
||||
scope :results_joins, lambda {
|
||||
joins("LEFT JOIN results ON subject_type = 'Result' AND subject_id = results.id")
|
||||
}
|
||||
|
||||
scope :protocols_joins, lambda {
|
||||
joins("LEFT JOIN protocols ON subject_type = 'Protocol' AND subject_id = protocols.id")
|
||||
}
|
||||
|
||||
scope :my_modules_joins, lambda { |*params|
|
||||
joins_query = "LEFT JOIN my_modules ON (subject_type = 'MyModule' AND subject_id = my_modules.id)"
|
||||
joins_query += ' OR protocols.my_module_id = my_modules.id' if params.include? :from_protocols
|
||||
joins_query += ' OR results.my_module_id = my_modules.id' if params.include? :from_results
|
||||
joins(joins_query)
|
||||
}
|
||||
scope :experiments_joins, lambda { |*params|
|
||||
joins_query = "LEFT JOIN experiments ON (subject_type = 'Experiment' AND subject_id = experiments.id)"
|
||||
joins_query += ' OR experiments.id = my_modules.experiment_id' if params.include? :from_my_modules
|
||||
joins(joins_query)
|
||||
}
|
||||
|
||||
scope :projects_joins, lambda { |*params|
|
||||
joins_query = "LEFT JOIN projects ON (subject_type = 'Project' AND subject_id = projects.id)"
|
||||
joins_query += ' OR projects.id = experiments.project_id' if params.include? :from_experiments
|
||||
joins(joins_query)
|
||||
}
|
||||
|
||||
scope :repositories_joins, lambda {
|
||||
joins("LEFT JOIN repositories ON subject_type = 'Repository' AND subject_id = repositories.id")
|
||||
}
|
||||
|
||||
scope :reports_joins, lambda {
|
||||
joins("LEFT JOIN reports ON subject_type = 'Report' AND subject_id = reports.id")
|
||||
}
|
||||
|
||||
store_accessor :values, :message_items, :breadcrumbs
|
||||
|
|
|
@ -14,7 +14,7 @@ module Dashboard
|
|||
def call
|
||||
visible_projects = Project.viewable_by_user(@user, @team)
|
||||
|
||||
activities = Activity.where("(values #>> '{message_items, user, id}') :: BIGINT = ?", @user.id)
|
||||
activities = Activity.where(owner_id: @user.id)
|
||||
.where('((project_id IS NULL AND team_id = ?) OR project_id IN (?))',
|
||||
@team.id,
|
||||
visible_projects.pluck(:id))
|
||||
|
@ -25,7 +25,13 @@ module Dashboard
|
|||
.order(last_change: :desc)
|
||||
|
||||
query = Activity.from("(#{activities.to_sql}) AS activities")
|
||||
.subjects_joins
|
||||
.results_joins
|
||||
.protocols_joins
|
||||
.my_modules_joins(:from_results, :from_protocols)
|
||||
.experiments_joins(:from_my_modules)
|
||||
.projects_joins(:from_experiments)
|
||||
.repositories_joins
|
||||
.reports_joins
|
||||
.where('projects.archived IS NOT TRUE')
|
||||
.where('experiments.archived IS NOT TRUE')
|
||||
.where('my_modules.archived IS NOT TRUE')
|
||||
|
|
Loading…
Reference in a new issue