2019-02-25 22:21:13 +08:00
|
|
|
# frozen_string_literal: true
|
2016-10-03 17:37:29 +08:00
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
class Activity < ApplicationRecord
|
2021-07-29 16:13:28 +08:00
|
|
|
ASSIGNMENT_TYPES = %w(
|
|
|
|
assign_user_to_project
|
|
|
|
change_user_role_on_project
|
|
|
|
unassign_user_from_project
|
|
|
|
assign_user_to_module
|
|
|
|
unassign_user_from_module
|
|
|
|
invite_user_to_team
|
|
|
|
remove_user_from_team
|
|
|
|
change_users_role_on_team
|
|
|
|
).freeze
|
|
|
|
|
2019-02-28 21:01:58 +08:00
|
|
|
include ActivityValuesModel
|
2021-02-25 14:43:04 +08:00
|
|
|
include GenerateNotificationModel
|
2019-02-28 21:01:58 +08:00
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
enum type_of: Extends::ACTIVITY_TYPES
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
belongs_to :owner, inverse_of: :activities, class_name: 'User'
|
|
|
|
belongs_to :subject, polymorphic: true, optional: true
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
# For permissions check
|
|
|
|
belongs_to :project, inverse_of: :activities, optional: true
|
|
|
|
belongs_to :team, inverse_of: :activities
|
2019-02-13 16:31:10 +08:00
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
# Associations for old activity type
|
2017-06-28 21:21:32 +08:00
|
|
|
belongs_to :experiment, inverse_of: :activities, optional: true
|
|
|
|
belongs_to :my_module, inverse_of: :activities, optional: true
|
2019-02-13 16:31:10 +08:00
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
validate :activity_version
|
|
|
|
validates :type_of, :owner, presence: true
|
|
|
|
validates :subject_type, inclusion: { in: Extends::ACTIVITY_SUBJECT_TYPES,
|
|
|
|
allow_blank: true }
|
2019-02-13 16:31:10 +08:00
|
|
|
|
2020-03-11 21:30:41 +08:00
|
|
|
scope :results_joins, lambda {
|
|
|
|
joins("LEFT JOIN results ON subject_type = 'Result' AND subject_id = results.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :protocols_joins, lambda {
|
|
|
|
joins("LEFT JOIN protocols ON subject_type = 'Protocol' AND subject_id = protocols.id")
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :my_modules_joins, lambda { |*params|
|
|
|
|
joins_query = "LEFT JOIN my_modules ON (subject_type = 'MyModule' AND subject_id = my_modules.id)"
|
|
|
|
joins_query += ' OR protocols.my_module_id = my_modules.id' if params.include? :from_protocols
|
|
|
|
joins_query += ' OR results.my_module_id = my_modules.id' if params.include? :from_results
|
|
|
|
joins(joins_query)
|
|
|
|
}
|
|
|
|
scope :experiments_joins, lambda { |*params|
|
|
|
|
joins_query = "LEFT JOIN experiments ON (subject_type = 'Experiment' AND subject_id = experiments.id)"
|
|
|
|
joins_query += ' OR experiments.id = my_modules.experiment_id' if params.include? :from_my_modules
|
|
|
|
joins(joins_query)
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :projects_joins, lambda { |*params|
|
|
|
|
joins_query = "LEFT JOIN projects ON (subject_type = 'Project' AND subject_id = projects.id)"
|
|
|
|
joins_query += ' OR projects.id = experiments.project_id' if params.include? :from_experiments
|
|
|
|
joins(joins_query)
|
|
|
|
}
|
|
|
|
|
|
|
|
scope :repositories_joins, lambda {
|
2020-04-09 18:33:04 +08:00
|
|
|
joins("LEFT JOIN repositories ON subject_type = 'RepositoryBase' AND subject_id = repositories.id")
|
2020-03-11 21:30:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
scope :reports_joins, lambda {
|
|
|
|
joins("LEFT JOIN reports ON subject_type = 'Report' AND subject_id = reports.id")
|
|
|
|
}
|
2020-03-11 18:34:59 +08:00
|
|
|
|
2019-03-19 20:33:32 +08:00
|
|
|
store_accessor :values, :message_items, :breadcrumbs
|
2019-02-28 21:01:58 +08:00
|
|
|
|
|
|
|
default_values(
|
2019-03-19 20:33:32 +08:00
|
|
|
message_items: {},
|
|
|
|
breadcrumbs: {}
|
2019-02-28 21:01:58 +08:00
|
|
|
)
|
|
|
|
|
2021-08-26 20:08:15 +08:00
|
|
|
after_create ->(activity) { Activities::DispatchWebhooksJob.perform_later(activity) },
|
|
|
|
if: -> { Rails.application.config.x.webhooks_enabled }
|
2021-06-17 15:19:04 +08:00
|
|
|
|
2019-03-06 17:34:04 +08:00
|
|
|
def self.activity_types_list
|
2019-03-26 22:29:21 +08:00
|
|
|
activity_list = type_ofs.map do |key, value|
|
|
|
|
[
|
2019-03-27 17:33:33 +08:00
|
|
|
I18n.t("global_activities.activity_name.#{key}"),
|
2019-03-26 22:29:21 +08:00
|
|
|
value
|
|
|
|
]
|
|
|
|
end.sort_by { |a| a[0] }
|
|
|
|
activity_groups = Extends::ACTIVITY_GROUPS
|
|
|
|
|
|
|
|
result = {}
|
|
|
|
|
|
|
|
activity_groups.each do |key, activities|
|
2019-03-27 17:33:33 +08:00
|
|
|
group_name = I18n.t("global_activities.activity_group.#{key}")
|
2019-03-26 22:29:21 +08:00
|
|
|
result[group_name] = []
|
|
|
|
activities.each do |activity_id|
|
|
|
|
activity_hash = activity_list.select { |activity| activity[1] == activity_id }[0]
|
|
|
|
result[group_name].push(activity_hash) if activity_hash
|
|
|
|
end
|
|
|
|
end
|
|
|
|
result
|
2019-03-06 17:34:04 +08:00
|
|
|
end
|
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
def old_activity?
|
2019-03-19 20:33:32 +08:00
|
|
|
subject_id.nil?
|
|
|
|
end
|
|
|
|
|
|
|
|
def generate_breadcrumbs
|
|
|
|
generate_breadcrumb subject if subject
|
2019-02-25 22:21:13 +08:00
|
|
|
end
|
2016-10-03 17:37:29 +08:00
|
|
|
|
2019-04-02 17:14:36 +08:00
|
|
|
def self.url_search_query(filters)
|
|
|
|
result = []
|
|
|
|
filters.each do |filter, values|
|
|
|
|
result.push(values.to_query(filter))
|
|
|
|
end
|
|
|
|
if filters[:subjects]
|
|
|
|
subject_labels = []
|
|
|
|
filters[:subjects].each do |object, values|
|
|
|
|
values.each do |value|
|
2019-08-08 21:51:08 +08:00
|
|
|
label = object.to_s.constantize.find_by_id(value).name
|
2019-08-26 21:49:33 +08:00
|
|
|
subject_labels.push({ value: value, label: label, object: object.downcase, group: '' }.as_json)
|
2019-04-02 17:14:36 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
result.push(subject_labels.to_query('subject_labels'))
|
|
|
|
end
|
|
|
|
result.join('&')
|
|
|
|
end
|
|
|
|
|
2016-10-03 17:37:29 +08:00
|
|
|
private
|
|
|
|
|
2019-03-19 20:33:32 +08:00
|
|
|
def generate_breadcrumb(subject)
|
|
|
|
case subject
|
|
|
|
when Protocol
|
|
|
|
breadcrumbs[:protocol] = subject.name
|
|
|
|
if subject.in_repository?
|
|
|
|
generate_breadcrumb(subject.team)
|
|
|
|
else
|
|
|
|
generate_breadcrumb(subject.my_module)
|
|
|
|
end
|
|
|
|
when MyModule
|
|
|
|
breadcrumbs[:my_module] = subject.name
|
|
|
|
generate_breadcrumb(subject.experiment)
|
|
|
|
when Experiment
|
|
|
|
breadcrumbs[:experiment] = subject.name
|
|
|
|
generate_breadcrumb(subject.project)
|
|
|
|
when Project
|
|
|
|
breadcrumbs[:project] = subject.name
|
|
|
|
generate_breadcrumb(subject.team)
|
|
|
|
when Repository
|
|
|
|
breadcrumbs[:repository] = subject.name
|
|
|
|
generate_breadcrumb(subject.team)
|
|
|
|
when Result
|
|
|
|
breadcrumbs[:result] = subject.name
|
|
|
|
generate_breadcrumb(subject.my_module)
|
|
|
|
when Team
|
|
|
|
breadcrumbs[:team] = subject.name
|
2019-04-04 16:30:31 +08:00
|
|
|
when Report
|
|
|
|
breadcrumbs[:report] = subject.name
|
|
|
|
generate_breadcrumb(subject.team) if subject.team
|
2020-12-01 21:51:26 +08:00
|
|
|
when ProjectFolder
|
|
|
|
breadcrumbs[:project_folder] = subject.name
|
|
|
|
generate_breadcrumb(subject.team)
|
2021-07-15 18:37:04 +08:00
|
|
|
when Step
|
|
|
|
breadcrumbs[:step] = subject.name
|
2021-08-11 20:10:05 +08:00
|
|
|
generate_breadcrumb(subject.protocol)
|
2021-07-15 18:37:04 +08:00
|
|
|
when Asset
|
|
|
|
breadcrumbs[:asset] = subject.blob.filename.to_s
|
2021-08-03 15:58:13 +08:00
|
|
|
generate_breadcrumb(subject.result || subject.step || subject.repository_cell.repository_row.repository)
|
2019-03-19 20:33:32 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
def activity_version
|
2019-04-01 17:26:51 +08:00
|
|
|
errors.add(:activity, 'wrong combination of associations') if (experiment_id || my_module_id) && subject
|
2016-10-03 17:37:29 +08:00
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|