mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2024-12-27 10:14:17 +08:00
Fix recent work service and move templates to html
This commit is contained in:
parent
180b736769
commit
c13721f842
4 changed files with 113 additions and 108 deletions
|
@ -4,11 +4,11 @@
|
|||
var DasboardRecentWorkWidget = (function() {
|
||||
function renderRecentWorkItem(data, container) {
|
||||
$.each(data, (i, item) => {
|
||||
var recentWorkItem = `<a href="${item.url}" class="recent-work-item">
|
||||
<div class="object-name">${item.name}</div>
|
||||
<div class="object-type">${I18n.t('dashboard.recent_work.subject_type.' + item.subject_type)}</div>
|
||||
<div class="object-changed">${item.last_change}</div>
|
||||
</a>`;
|
||||
var recentWorkItem = $($('#recent-work-item-template').html());
|
||||
recentWorkItem.attr('href', item.url);
|
||||
recentWorkItem.find('.object-name').html(item.name);
|
||||
recentWorkItem.find('.object-type').html(I18n.t('dashboard.recent_work.subject_type.' + item.subject_type));
|
||||
recentWorkItem.find('.object-changed').html(item.last_change);
|
||||
container.append(recentWorkItem);
|
||||
});
|
||||
}
|
||||
|
@ -22,13 +22,7 @@ var DasboardRecentWorkWidget = (function() {
|
|||
if (result.length) {
|
||||
renderRecentWorkItem(result, container);
|
||||
} else {
|
||||
container.append(`<div class="no-results">
|
||||
<div class="no-results-title">${I18n.t('dashboard.recent_work.no_results.title')}</div>
|
||||
<div class="no-results-description">${I18n.t('dashboard.recent_work.no_results.description')}</div>
|
||||
<div class="no-results-arrow">
|
||||
<i class="fas fa-angle-double-left"></i>
|
||||
</div>
|
||||
</div>`);
|
||||
container.append($('#recent-work-no-results-template').html());
|
||||
}
|
||||
|
||||
PerfectSb().update_all();
|
||||
|
|
|
@ -21,6 +21,30 @@ class Activity < ApplicationRecord
|
|||
validates :subject_type, inclusion: { in: Extends::ACTIVITY_SUBJECT_TYPES,
|
||||
allow_blank: true }
|
||||
|
||||
scope :subjects_joins, -> { 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
|
||||
") }
|
||||
|
||||
store_accessor :values, :message_items, :breadcrumbs
|
||||
|
||||
default_values(
|
||||
|
|
|
@ -13,43 +13,25 @@ module Dashboard
|
|||
|
||||
def call
|
||||
visible_projects = Project.viewable_by_user(@user, @team)
|
||||
visible_by_team = activities_with_filter.where(project: nil, team_id: @team.id)
|
||||
visible_by_projects = activities_with_filter.where(project_id: visible_projects.pluck(:id))
|
||||
query = Activity.from("((#{visible_by_team.to_sql}) UNION ALL (#{visible_by_projects.to_sql})) AS activities")
|
||||
|
||||
# Join subjects
|
||||
query = query.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
|
||||
")
|
||||
.where("
|
||||
(projects.archived != 'true' OR projects.archived IS NULL)
|
||||
AND (experiments.archived != 'true' OR experiments.archived IS NULL)
|
||||
AND (my_modules.archived != 'true' OR my_modules.archived IS NULL)
|
||||
AND (protocols.protocol_type != 4 OR protocols.protocol_type IS NULL)
|
||||
")
|
||||
.select('
|
||||
DISTINCT ON (group_id)
|
||||
activities = Activity.where("(values #>> '{message_items, user, id}') :: BIGINT = ?", @user.id)
|
||||
.where('((project_id IS NULL AND team_id = ?) OR project_id IN (?))',
|
||||
@team.id,
|
||||
visible_projects.pluck(:id))
|
||||
.select('MAX(created_at) AS last_change',
|
||||
:subject_id,
|
||||
:subject_type)
|
||||
.group(:subject_id, :subject_type)
|
||||
.order(last_change: :desc)
|
||||
|
||||
query = Activity.from("(#{activities.to_sql}) AS activities")
|
||||
.subjects_joins
|
||||
.where('projects.archived IS NOT TRUE')
|
||||
.where('experiments.archived IS NOT TRUE')
|
||||
.where('my_modules.archived IS NOT TRUE')
|
||||
.where('protocols.protocol_type != ? OR protocols.protocol_type IS NULL',
|
||||
Protocol.protocol_types[:in_repository_archived])
|
||||
.select('
|
||||
CASE
|
||||
WHEN my_modules.id IS NOT NULL THEN
|
||||
CONCAT(\'tsk\', my_modules.id)
|
||||
|
@ -63,100 +45,87 @@ module Dashboard
|
|||
CONCAT(\'inv\', repositories.id)
|
||||
WHEN reports.id IS NOT NULL THEN
|
||||
CONCAT(\'rpt\', reports.id)
|
||||
END as group_id,
|
||||
END AS group_id,
|
||||
|
||||
CASE
|
||||
WHEN my_modules.name IS NOT NULL THEN
|
||||
my_modules.name
|
||||
WHEN experiments.name IS NOT NULL THEN
|
||||
experiments.name
|
||||
WHEN projects.name IS NOT NULL THEN
|
||||
projects.name
|
||||
WHEN protocols.name IS NOT NULL THEN
|
||||
protocols.name
|
||||
WHEN repositories.name IS NOT NULL THEN
|
||||
repositories.name
|
||||
WHEN reports.name IS NOT NULL THEN
|
||||
COALESCE (
|
||||
my_modules.name,
|
||||
experiments.name,
|
||||
projects.name,
|
||||
protocols.name,
|
||||
repositories.name,
|
||||
reports.name
|
||||
END as name,
|
||||
) AS name,
|
||||
|
||||
reports.project_id as report_project_id,
|
||||
reports.project_id AS report_project_id,
|
||||
subject_id,
|
||||
subject_type,
|
||||
last_change
|
||||
')
|
||||
|
||||
query_filter = []
|
||||
if %w(all projects).include? @mode
|
||||
query_filter.push("group_id LIKE '%tsk%' OR group_id LIKE '%exp%' OR group_id LIKE '%pro%'")
|
||||
end
|
||||
ordered_query = Activity.from("(#{query.to_sql}) AS activities").where.not(group_id: nil)
|
||||
.select(:group_id,
|
||||
:name,
|
||||
'MAX(last_change) AS last_change',
|
||||
'MAX(report_project_id) AS report_project_id')
|
||||
.group(:group_id, :name)
|
||||
.order('MAX(last_change) DESC').limit(Constants::SEARCH_LIMIT)
|
||||
|
||||
query_filter.push("group_id LIKE '%prt%'") if %w(all protocols).include? @mode
|
||||
query_filter = "(group_id LIKE 'tsk%' OR group_id LIKE 'exp%' OR group_id LIKE 'pro%')" if @mode == 'projects'
|
||||
query_filter = "group_id LIKE 'prt%'" if @mode == 'protocols'
|
||||
query_filter = "group_id LIKE 'inv%'" if @mode == 'repositories'
|
||||
query_filter = "group_id LIKE 'rpt%'" if @mode == 'reports'
|
||||
ordered_query = ordered_query.where(query_filter) unless @mode == 'all'
|
||||
|
||||
query_filter.push("group_id LIKE '%inv%'") if %w(all repositories).include? @mode
|
||||
recent_objects = ordered_query.as_json.map do |recent_object|
|
||||
recent_object.deep_symbolize_keys!
|
||||
recent_object.delete_if { |_k, v| v.nil? }
|
||||
|
||||
query_filter.push("group_id LIKE '%rpt%'") if %w(all reports).include? @mode
|
||||
|
||||
ordered_query = Activity.from("(#{query.to_sql}) AS activities").where(query_filter.join(' OR '))
|
||||
.order('last_change DESC').limit(Constants::SEARCH_LIMIT)
|
||||
|
||||
activities = ordered_query.as_json.map do |activity|
|
||||
activity.deep_symbolize_keys!
|
||||
activity.delete_if { |_k, v| v.nil? }
|
||||
|
||||
activity[:last_change] = I18n.l(
|
||||
DateTime.parse(activity[:last_change]).in_time_zone(@user.settings[:time_zone] || 'UTC'),
|
||||
recent_object[:last_change] = I18n.l(
|
||||
DateTime.parse(recent_object[:last_change]).in_time_zone(@user.settings[:time_zone] || 'UTC'),
|
||||
format: :full_with_comma
|
||||
)
|
||||
activity[:subject_type] = override_subject_type(activity)
|
||||
activity[:name] = escape_input(activity[:name])
|
||||
activity[:url] = generate_url(activity)
|
||||
activity
|
||||
recent_object[:subject_type] = override_subject_type(recent_object)
|
||||
recent_object[:name] = escape_input(recent_object[:name])
|
||||
recent_object[:url] = generate_url(recent_object)
|
||||
recent_object
|
||||
end
|
||||
|
||||
activities
|
||||
recent_objects
|
||||
end
|
||||
|
||||
private
|
||||
|
||||
def generate_url(activity)
|
||||
case activity[:subject_type]
|
||||
def generate_url(recent_object)
|
||||
object_id = recent_object[:group_id].gsub(/[^0-9]/, '')
|
||||
|
||||
case recent_object[:subject_type]
|
||||
when 'MyModule'
|
||||
protocols_my_module_path(activity[:subject_id])
|
||||
protocols_my_module_path(object_id)
|
||||
when 'Experiment'
|
||||
canvas_experiment_path(activity[:subject_id])
|
||||
canvas_experiment_path(object_id)
|
||||
when 'Project'
|
||||
project_path(activity[:subject_id])
|
||||
project_path(object_id)
|
||||
when 'Protocol'
|
||||
edit_protocol_path(activity[:subject_id])
|
||||
edit_protocol_path(object_id)
|
||||
when 'Repository'
|
||||
repository_path(activity[:subject_id])
|
||||
repository_path(object_id)
|
||||
when 'Report'
|
||||
edit_project_report_path(activity[:report_project_id], activity[:subject_id]) if activity[:report_project_id]
|
||||
edit_project_report_path(recent_object[:report_project_id], object_id) if recent_object[:report_project_id]
|
||||
end
|
||||
end
|
||||
|
||||
def activities_with_filter
|
||||
Activity.where("(values #>> '{message_items, user, id}') :: BIGINT = ?", @user.id)
|
||||
.select('MAX(created_at) as last_change,
|
||||
subject_id,
|
||||
subject_type')
|
||||
.group(:subject_id, :subject_type)
|
||||
.order(last_change: :desc)
|
||||
end
|
||||
|
||||
def override_subject_type(activity)
|
||||
if activity[:group_id].include?('pro')
|
||||
def override_subject_type(recent_object)
|
||||
if recent_object[:group_id].include?('pro')
|
||||
'Project'
|
||||
elsif activity[:group_id].include?('exp')
|
||||
elsif recent_object[:group_id].include?('exp')
|
||||
'Experiment'
|
||||
elsif activity[:group_id].include?('tsk')
|
||||
elsif recent_object[:group_id].include?('tsk')
|
||||
'MyModule'
|
||||
elsif activity[:group_id].include?('prt')
|
||||
elsif recent_object[:group_id].include?('prt')
|
||||
'Protocol'
|
||||
elsif activity[:group_id].include?('inv')
|
||||
elsif recent_object[:group_id].include?('inv')
|
||||
'Repository'
|
||||
elsif activity[:group_id].include?('rpt')
|
||||
elsif recent_object[:group_id].include?('rpt')
|
||||
'Report'
|
||||
end
|
||||
end
|
||||
|
|
|
@ -15,3 +15,21 @@
|
|||
<div class="recent-work-container perfect-scrollbar" data-url="<%= dashboard_recent_works_path %>"></div>
|
||||
</div>
|
||||
</div>
|
||||
|
||||
<template id="recent-work-item-template">
|
||||
<a href="" class="recent-work-item">
|
||||
<div class="object-name"></div>
|
||||
<div class="object-type"></div>
|
||||
<div class="object-changed"></div>
|
||||
</a>
|
||||
</template>
|
||||
|
||||
<template id="recent-work-no-results-template">
|
||||
<div class="no-results">
|
||||
<div class="no-results-title"><%= t('dashboard.recent_work.no_results.title') %></div>
|
||||
<div class="no-results-description"><%= t('dashboard.recent_work.no_results.description') %></div>
|
||||
<div class="no-results-arrow">
|
||||
<i class="fas fa-angle-double-left"></i>
|
||||
</div>
|
||||
</div>
|
||||
</template>
|
Loading…
Reference in a new issue