From 9f23fe557a9e61fe1ce167b51a2c6c270cbc1f3d Mon Sep 17 00:00:00 2001 From: artoscinote <85488244+artoscinote@users.noreply.github.com> Date: Wed, 23 Aug 2023 16:49:17 +0200 Subject: [PATCH] Add entity breadcrumbs to activity webhooks [SCI-9056] (#6056) --- app/models/activity.rb | 17 +++++++++++++++++ .../activity_filter_matching_service.rb | 19 +------------------ .../activities/activity_webhook_service.rb | 5 ++++- config/initializers/extends.rb | 3 ++- 4 files changed, 24 insertions(+), 20 deletions(-) diff --git a/app/models/activity.rb b/app/models/activity.rb index 21e418745..35aaabd23 100644 --- a/app/models/activity.rb +++ b/app/models/activity.rb @@ -14,6 +14,11 @@ class Activity < ApplicationRecord change_user_role_on_my_module ).freeze + # invert the children hash to get a hash defining parents + ACTIVITY_SUBJECT_PARENTS = Extends::ACTIVITY_SUBJECT_CHILDREN.invert.map do |k, v| + k&.map { |s| [s.to_s.classify, v.to_s.classify.constantize.reflect_on_association(s)&.inverse_of&.name || v] } + end.compact.sum.to_h.freeze + include ActivityValuesModel include GenerateNotificationModel @@ -127,6 +132,18 @@ class Activity < ApplicationRecord result.join('&') end + def subject_parents + recursive_subject_parents(subject, []).compact + end + + def recursive_subject_parents(activity_subject, parents) + parent_model_name = ACTIVITY_SUBJECT_PARENTS[activity_subject.class.name] + return parents if parent_model_name.nil? + + parent = activity_subject.public_send(parent_model_name) + recursive_subject_parents(parent, parents << parent) + end + private def generate_breadcrumb(subject) diff --git a/app/services/activities/activity_filter_matching_service.rb b/app/services/activities/activity_filter_matching_service.rb index 93eba70e8..01d48324a 100644 --- a/app/services/activities/activity_filter_matching_service.rb +++ b/app/services/activities/activity_filter_matching_service.rb @@ -2,11 +2,6 @@ module Activities class ActivityFilterMatchingService - # invert the children hash to get a hash defining parents - ACTIVITY_SUBJECT_PARENTS = Extends::ACTIVITY_SUBJECT_CHILDREN.invert.map do |k, v| - k&.map { |s| [s.to_s.classify, v.to_s.classify.constantize.reflect_on_association(s)&.inverse_of&.name || v] } - end.compact.sum.to_h.freeze - def initialize(activity) @activity = activity @activity_filters = ActivityFilter.all @@ -60,7 +55,7 @@ module Activities end def filter_subjects! - parents = activity_subject_parents + parents = @activity.subject_parents @activity_filters = @activity_filters.where( "NOT(filter ? 'subjects') OR "\ @@ -72,17 +67,5 @@ module Activities end.join(' OR ') ) end - - def activity_subject_parents - subject_parents(@activity.subject, []).compact - end - - def subject_parents(subject, parents) - parent_model_name = ACTIVITY_SUBJECT_PARENTS[subject.class.name] - return parents if parent_model_name.nil? - - parent = subject.public_send(parent_model_name) - subject_parents(parent, parents << parent) - end end end diff --git a/app/services/activities/activity_webhook_service.rb b/app/services/activities/activity_webhook_service.rb index e2500539c..a71006db2 100644 --- a/app/services/activities/activity_webhook_service.rb +++ b/app/services/activities/activity_webhook_service.rb @@ -18,7 +18,10 @@ module Activities subject: { type: @activity.subject_type, id: @activity.subject_id - } + }, + subject_breadcrumbs: @activity.subject_parents.map do |subject| + { type: subject.model_name.human.downcase, id: subject.id } + end ) end end diff --git a/config/initializers/extends.rb b/config/initializers/extends.rb index 9362e225f..0bb45f5c2 100644 --- a/config/initializers/extends.rb +++ b/config/initializers/extends.rb @@ -204,7 +204,8 @@ class Extends repository_base: [:repository_rows], repository_row: nil, report: nil, - project: nil, + team: [:projects], + project: [:experiments], experiment: [:my_modules], my_module: %i(results protocols), result: [:asset],