2019-05-02 18:17:27 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
class ReviewableSerializer < ApplicationSerializer
|
|
|
|
class_attribute :_payload_for_serialization
|
|
|
|
|
|
|
|
attributes(
|
|
|
|
:id,
|
|
|
|
:type,
|
|
|
|
:topic_id,
|
2019-04-01 14:28:57 -04:00
|
|
|
:topic_url,
|
2019-06-10 11:33:10 -04:00
|
|
|
:target_url,
|
2019-04-05 14:22:27 -04:00
|
|
|
:topic_tags,
|
2019-01-03 12:03:01 -05:00
|
|
|
:category_id,
|
|
|
|
:created_at,
|
|
|
|
:can_edit,
|
|
|
|
:score,
|
2019-04-05 14:22:27 -04:00
|
|
|
:version,
|
2023-01-09 07:20:10 -05:00
|
|
|
:target_created_by_trust_level,
|
2019-01-03 12:03:01 -05:00
|
|
|
)
|
|
|
|
|
2021-12-08 12:12:24 -05:00
|
|
|
attribute :status_for_database, key: :status
|
|
|
|
|
2023-01-09 07:20:10 -05:00
|
|
|
has_one :created_by, serializer: UserWithCustomFieldsSerializer, root: "users"
|
|
|
|
has_one :target_created_by, serializer: UserWithCustomFieldsSerializer, root: "users"
|
2019-01-03 12:03:01 -05:00
|
|
|
has_one :topic, serializer: ListableTopicSerializer
|
|
|
|
has_many :editable_fields, serializer: ReviewableEditableFieldSerializer, embed: :objects
|
|
|
|
has_many :reviewable_scores, serializer: ReviewableScoreSerializer
|
|
|
|
has_many :bundled_actions, serializer: ReviewableBundledActionSerializer
|
2023-01-09 07:20:10 -05:00
|
|
|
has_one :claimed_by, serializer: UserWithCustomFieldsSerializer, root: "users"
|
2019-01-03 12:03:01 -05:00
|
|
|
|
|
|
|
# Used to keep track of our payload attributes
|
|
|
|
class_attribute :_payload_for_serialization
|
|
|
|
|
|
|
|
def bundled_actions
|
2019-05-08 10:20:51 -04:00
|
|
|
args = {}
|
|
|
|
args[:claimed_by] = claimed_by if @options[:claimed_topics]
|
|
|
|
object.actions_for(scope, args).bundles
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def editable_fields
|
2019-05-08 10:20:51 -04:00
|
|
|
args = {}
|
|
|
|
args[:claimed_by] = claimed_by if @options[:claimed_topics]
|
|
|
|
object.editable_for(scope, args).to_a
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def can_edit
|
|
|
|
editable_fields.present?
|
|
|
|
end
|
|
|
|
|
2019-05-08 10:20:51 -04:00
|
|
|
def claimed_by
|
|
|
|
return nil unless @options[:claimed_topics].present?
|
|
|
|
@options[:claimed_topics][object.topic_id]
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_claimed_by?
|
|
|
|
@options[:claimed_topics]
|
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def self.create_attribute(name, field)
|
|
|
|
attribute(name)
|
|
|
|
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
class_eval <<~RUBY
|
2019-01-03 12:03:01 -05:00
|
|
|
def #{name}
|
|
|
|
#{field}
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_#{name}?
|
|
|
|
#{name}.present?
|
|
|
|
end
|
DEV: Correctly tag heredocs (#16061)
This allows text editors to use correct syntax coloring for the heredoc sections.
Heredoc tag names we use:
languages: SQL, JS, RUBY, LUA, HTML, CSS, SCSS, SH, HBS, XML, YAML/YML, MF, ICS
other: MD, TEXT/TXT, RAW, EMAIL
2022-02-28 14:50:55 -05:00
|
|
|
RUBY
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
# This is easier than creating an AMS method for each attribute
|
|
|
|
def self.target_attributes(*attributes)
|
2023-01-09 07:20:10 -05:00
|
|
|
attributes.each { |a| create_attribute(a, "object.target&.#{a}") }
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
def self.payload_attributes(*attributes)
|
|
|
|
self._payload_for_serialization ||= []
|
|
|
|
self._payload_for_serialization += attributes.map(&:to_s)
|
|
|
|
end
|
|
|
|
|
|
|
|
def attributes
|
2019-04-10 05:25:45 -04:00
|
|
|
super.tap do |data|
|
|
|
|
data[:removed_topic_id] = object.topic_id unless object.topic
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-04-10 05:25:45 -04:00
|
|
|
if object.target.present?
|
|
|
|
# Automatically add the target id as a "good name" for example a target_type of `User`
|
|
|
|
# becomes `user_id`
|
|
|
|
data[:"#{object.target_type.downcase}_id"] = object.target_id
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
|
2019-04-10 05:25:45 -04:00
|
|
|
if self.class._payload_for_serialization.present?
|
|
|
|
data[:payload] = (object.payload || {}).slice(*self.class._payload_for_serialization)
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2019-04-05 14:22:27 -04:00
|
|
|
def topic_tags
|
|
|
|
object.topic.tags.map(&:name)
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_topic_tags?
|
|
|
|
object.topic.present? && SiteSetting.tagging_enabled?
|
|
|
|
end
|
|
|
|
|
2019-06-10 11:33:10 -04:00
|
|
|
def target_url
|
2023-01-09 07:20:10 -05:00
|
|
|
if object.target.is_a?(Post) && object.target.present?
|
|
|
|
return Discourse.base_url + object.target.url
|
|
|
|
end
|
2019-06-10 11:33:10 -04:00
|
|
|
topic_url
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_target_url?
|
|
|
|
target_url.present?
|
|
|
|
end
|
|
|
|
|
2019-04-01 14:28:57 -04:00
|
|
|
def topic_url
|
2019-11-14 15:10:51 -05:00
|
|
|
object.topic&.url
|
2019-04-01 14:28:57 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
def include_topic_url?
|
2019-06-10 11:33:10 -04:00
|
|
|
topic_url.present?
|
2019-04-01 14:28:57 -04:00
|
|
|
end
|
|
|
|
|
2019-01-03 12:03:01 -05:00
|
|
|
def include_topic_id?
|
|
|
|
object.topic_id.present?
|
|
|
|
end
|
|
|
|
|
|
|
|
def include_category_id?
|
|
|
|
object.category_id.present?
|
|
|
|
end
|
|
|
|
|
2020-11-11 12:09:42 -05:00
|
|
|
def target_created_by_trust_level
|
|
|
|
object&.target_created_by&.trust_level
|
|
|
|
end
|
2019-01-03 12:03:01 -05:00
|
|
|
end
|