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
|
2019-02-28 21:01:58 +08:00
|
|
|
include ActivityValuesModel
|
|
|
|
|
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
|
|
|
|
2019-02-28 21:01:58 +08:00
|
|
|
store_accessor :values, :message_items
|
|
|
|
|
|
|
|
default_values(
|
|
|
|
message_items: {}
|
|
|
|
)
|
|
|
|
|
2019-03-06 17:34:04 +08:00
|
|
|
def self.activity_types_list
|
|
|
|
type_ofs.map do |key, value|
|
|
|
|
{
|
|
|
|
id: value,
|
|
|
|
name: key.tr('_', ' ').capitalize
|
|
|
|
}
|
|
|
|
end.sort_by { |a| a[:name] }
|
|
|
|
end
|
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
def old_activity?
|
|
|
|
subject.nil?
|
|
|
|
end
|
2016-10-03 17:37:29 +08:00
|
|
|
|
|
|
|
private
|
|
|
|
|
2019-02-25 22:21:13 +08:00
|
|
|
def activity_version
|
|
|
|
if (experiment || my_module) && subject
|
|
|
|
errors.add(:activity, 'wrong combination of associations')
|
2016-10-03 17:37:29 +08:00
|
|
|
end
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|