mirror of
https://github.com/scinote-eln/scinote-web.git
synced 2025-02-19 13:25:58 +08:00
50 lines
1.3 KiB
Ruby
50 lines
1.3 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
class Activity < ApplicationRecord
|
|
enum type_of: Extends::ACTIVITY_TYPES
|
|
|
|
belongs_to :owner, inverse_of: :activities, class_name: 'User'
|
|
belongs_to :subject, polymorphic: true, optional: true
|
|
|
|
# For permissions check
|
|
belongs_to :project, inverse_of: :activities, optional: true
|
|
belongs_to :team, inverse_of: :activities
|
|
|
|
# Associations for old activity type
|
|
belongs_to :experiment, inverse_of: :activities, optional: true
|
|
belongs_to :my_module, inverse_of: :activities, optional: true
|
|
|
|
validate :activity_version
|
|
validates :type_of, :owner, presence: true
|
|
validates :subject_type, inclusion: { in: Extends::ACTIVITY_SUBJECT_TYPES,
|
|
allow_blank: true }
|
|
|
|
def self.activity_types_list
|
|
type_ofs.map do |key, value|
|
|
{
|
|
id: value,
|
|
name: key.tr('_', ' ').capitalize
|
|
}
|
|
end.sort_by { |a| a[:name] }
|
|
end
|
|
|
|
def old_activity?
|
|
subject.nil?
|
|
end
|
|
|
|
def message_items
|
|
values['message_items'].with_indifferent_access.merge(user: owner.id)
|
|
end
|
|
|
|
def html_message
|
|
I18n.t "activities.#{type_of}", message_items
|
|
end
|
|
|
|
private
|
|
|
|
def activity_version
|
|
if (experiment || my_module) && subject
|
|
errors.add(:activity, 'wrong combination of associations')
|
|
end
|
|
end
|
|
end
|