Add entity breadcrumbs to activity webhooks [SCI-9056] (#6056)

This commit is contained in:
artoscinote 2023-08-23 16:49:17 +02:00 committed by GitHub
parent 3c0389adc8
commit 9f23fe557a
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
4 changed files with 24 additions and 20 deletions

View file

@ -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)

View file

@ -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

View file

@ -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

View file

@ -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],