diff --git a/app/assets/javascripts/dashboard/recent_work.js b/app/assets/javascripts/dashboard/recent_work.js index 0b07620ca..a8403ecea 100644 --- a/app/assets/javascripts/dashboard/recent_work.js +++ b/app/assets/javascripts/dashboard/recent_work.js @@ -4,11 +4,11 @@ var DasboardRecentWorkWidget = (function() { function renderRecentWorkItem(data, container) { $.each(data, (i, item) => { - var recentWorkItem = ` -
${item.name}
-
${I18n.t('dashboard.recent_work.subject_type.' + item.subject_type)}
-
${item.last_change}
-
`; + 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(`
-
${I18n.t('dashboard.recent_work.no_results.title')}
-
${I18n.t('dashboard.recent_work.no_results.description')}
-
- -
-
`); + container.append($('#recent-work-no-results-template').html()); } PerfectSb().update_all(); diff --git a/app/models/activity.rb b/app/models/activity.rb index 84118e832..0601df688 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -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( diff --git a/app/services/dashboard/recent_work_service.rb b/app/services/dashboard/recent_work_service.rb index 33f49758d..d77a0adb5 100644 --- a/app/services/dashboard/recent_work_service.rb +++ b/app/services/dashboard/recent_work_service.rb @@ -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 diff --git a/app/views/dashboards/_recent_work.html.erb b/app/views/dashboards/_recent_work.html.erb index 1c48bc9d6..45da980fc 100644 --- a/app/views/dashboards/_recent_work.html.erb +++ b/app/views/dashboards/_recent_work.html.erb @@ -15,3 +15,21 @@
+ + + + \ No newline at end of file