Adding children selector to global activities subjects [SCI-3247] ()

* Adding children selector to global activities subjects

* Fix naming, add seperate check for project id
This commit is contained in:
aignatov-bio 2019-03-29 09:26:27 +01:00 committed by GitHub
parent ba4b35d004
commit b51dcc9d75
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
5 changed files with 45 additions and 11 deletions
app
config/initializers

View file

@ -1,4 +1,4 @@
/* global animateSpinner */ /* global animateSpinner I18n */
// Common code // Common code
@ -79,7 +79,10 @@ $(function() {
label: key label: key
}; };
}); });
result.push({ text: key, children: updateItems }); result.push({
text: I18n.t('global_activities.subject_name.' + key.toLowerCase()),
children: updateItems
});
}); });
return { return {
results: result results: result
@ -142,7 +145,7 @@ $(function() {
} else { } else {
moreButton.addClass('hidden'); moreButton.addClass('hidden');
} }
if (json.activities_html === "") { if (json.activities_html === '') {
noActivitiesMessage.removeClass('hidden'); noActivitiesMessage.removeClass('hidden');
} else { } else {
noActivitiesMessage.addClass('hidden'); noActivitiesMessage.addClass('hidden');

View file

@ -57,8 +57,7 @@ class GlobalActivitiesController < ApplicationController
.pluck(:id, :name) .pluck(:id, :name)
next if matched.length.zero? next if matched.length.zero?
subject_name = t("global_activities.subject_name.#{subject.to_s.downcase}") results[subject] = matched.map { |pr| { id: pr[0], name: pr[1] } }
results[subject_name] = matched.map { |pr| { id: pr[0], name: pr[1] } }
end end
respond_to do |format| respond_to do |format|
format.json do format.json do

View file

@ -77,9 +77,7 @@ class MyModulesController < ApplicationController
def activities def activities
params[:subjects] = { params[:subjects] = {
MyModule: [@my_module.id], MyModule: [@my_module.id]
Result: @my_module.results.pluck(:id),
Protocol: @my_module.protocols.pluck(:id)
} }
@activity_types = Activity.activity_types_list @activity_types = Activity.activity_types_list
@user_list = User.where(id: UserTeam.where(team: current_user.teams).select(:user_id)) @user_list = User.where(id: UserTeam.where(team: current_user.teams).select(:user_id))

View file

@ -8,10 +8,15 @@ class ActivitiesService
query = Activity.where(project: visible_projects) query = Activity.where(project: visible_projects)
.or(Activity.where(project: nil, team: visible_teams)) .or(Activity.where(project: nil, team: visible_teams))
if filters[:subjects].present? if filters[:subjects].present?
subjects_with_children = load_subjects_children(filters[:subjects])
if subjects_with_children[:Project]
add_or = subjects_with_children.length > 1
query = query.where("project_id IN (?) #{add_or ? 'OR' : ''}", subjects_with_children[:Project])
subjects_with_children.except!(:Project)
end
query = query.where( query = query.where(
filters[:subjects] subjects_with_children.map { '(subject_type = ? AND subject_id IN(?))' }.join(' OR '),
.to_h.map { '(subject_type = ? AND subject_id IN(?))' }.join(' OR '), *subjects_with_children.flatten
*filters[:subjects].to_h.flatten
) )
end end
@ -53,4 +58,22 @@ class ActivitiesService
results[last_date] = activities.to_a results[last_date] = activities.to_a
[results, more_left] [results, more_left]
end end
def self.load_subjects_children(subjects = {})
subject_types = Extends::ACTIVITY_SUBJECT_CHILDREN
subject_types.each do |subject_name, children|
next unless children && subjects[subject_name]
children.each do |child|
parent_model = subject_name.to_s.constantize
child_model = parent_model.reflect_on_association(child).class_name.to_sym
child_id = parent_model.where(id: subjects[subject_name])
.joins(child)
.pluck("#{child}.id")
subjects[child_model] = (subjects[child_model] ||= []) + child_id
end
end
subjects.each { |_sub, children| children.uniq! }
end
end end

View file

@ -92,6 +92,17 @@ class Extends
Repository Project Experiment MyModule Result Protocol Step Report Repository Project Experiment MyModule Result Protocol Step Report
).freeze ).freeze
ACTIVITY_SUBJECT_CHILDREN = {
Repository: nil,
Report: nil,
Project: nil,
Experiment: [:my_modules],
MyModule: [:results,:protocols],
Result: nil,
Protocol: [:steps],
Step: nil
}.freeze
ACTIVITY_MESSAGE_ITEMS_TYPES = ACTIVITY_MESSAGE_ITEMS_TYPES =
ACTIVITY_SUBJECT_TYPES + %w(User Tag RepositoryColumn RepositoryRow Step) ACTIVITY_SUBJECT_TYPES + %w(User Tag RepositoryColumn RepositoryRow Step)
.freeze .freeze