Class: ReviewableSerializer

Inherits:
ApplicationSerializer show all
Defined in:
app/serializers/reviewable_serializer.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from ApplicationSerializer

expire_cache_fragment!, fragment_cache

Methods inherited from ActiveModel::Serializer

#include!

Class Method Details

.create_attribute(name, field) ⇒ Object



60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'app/serializers/reviewable_serializer.rb', line 60

def self.create_attribute(name, field)
  attribute(name)

  class_eval <<~RUBY
    def #{name}
      #{field}
    end

    def include_#{name}?
      #{name}.present?
    end
  RUBY
end

.payload_attributes(*attributes) ⇒ Object



79
80
81
82
# File 'app/serializers/reviewable_serializer.rb', line 79

def self.payload_attributes(*attributes)
  self._payload_for_serialization ||= []
  self._payload_for_serialization += attributes.map(&:to_s)
end

.target_attributes(*attributes) ⇒ Object

This is easier than creating an AMS method for each attribute



75
76
77
# File 'app/serializers/reviewable_serializer.rb', line 75

def self.target_attributes(*attributes)
  attributes.each { |a| create_attribute(a, "object.target&.#{a}") }
end

Instance Method Details

#attributesObject



84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
# File 'app/serializers/reviewable_serializer.rb', line 84

def attributes
  super.tap do |data|
    data[:removed_topic_id] = object.topic_id unless object.topic

    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

    if self.class._payload_for_serialization.present?
      data[:payload] = (object.payload || {}).slice(*self.class._payload_for_serialization)
    end
  end
end

#bundled_actionsObject



35
36
37
38
39
# File 'app/serializers/reviewable_serializer.rb', line 35

def bundled_actions
  args = {}
  args[:claimed_by] = claimed_by if @options[:claimed_topics]
  object.actions_for(scope, args).bundles
end

#can_editObject



47
48
49
# File 'app/serializers/reviewable_serializer.rb', line 47

def can_edit
  editable_fields.present?
end

#claimed_byObject



51
52
53
54
# File 'app/serializers/reviewable_serializer.rb', line 51

def claimed_by
  return nil unless @options[:claimed_topics].present?
  @options[:claimed_topics][object.topic_id]
end

#created_from_flag?Boolean

Returns:

  • (Boolean)


100
101
102
# File 'app/serializers/reviewable_serializer.rb', line 100

def created_from_flag?
  false
end

#editable_fieldsObject



41
42
43
44
45
# File 'app/serializers/reviewable_serializer.rb', line 41

def editable_fields
  args = {}
  args[:claimed_by] = claimed_by if @options[:claimed_topics]
  object.editable_for(scope, args).to_a
end

#include_category_id?Boolean

Returns:

  • (Boolean)


135
136
137
# File 'app/serializers/reviewable_serializer.rb', line 135

def include_category_id?
  object.category_id.present?
end

#include_claimed_by?Boolean

Returns:

  • (Boolean)


56
57
58
# File 'app/serializers/reviewable_serializer.rb', line 56

def include_claimed_by?
  @options[:claimed_topics]
end

#include_target_url?Boolean

Returns:

  • (Boolean)


119
120
121
# File 'app/serializers/reviewable_serializer.rb', line 119

def include_target_url?
  target_url.present?
end

#include_topic_id?Boolean

Returns:

  • (Boolean)


131
132
133
# File 'app/serializers/reviewable_serializer.rb', line 131

def include_topic_id?
  object.topic_id.present?
end

#include_topic_tags?Boolean

Returns:

  • (Boolean)


108
109
110
# File 'app/serializers/reviewable_serializer.rb', line 108

def include_topic_tags?
  object.topic.present? && SiteSetting.tagging_enabled?
end

#include_topic_url?Boolean

Returns:

  • (Boolean)


127
128
129
# File 'app/serializers/reviewable_serializer.rb', line 127

def include_topic_url?
  topic_url.present?
end

#target_created_by_trust_levelObject



139
140
141
# File 'app/serializers/reviewable_serializer.rb', line 139

def target_created_by_trust_level
  object&.target_created_by&.trust_level
end

#target_urlObject



112
113
114
115
116
117
# File 'app/serializers/reviewable_serializer.rb', line 112

def target_url
  if object.target.is_a?(Post) && object.target.present?
    return Discourse.base_url + object.target.url
  end
  topic_url
end

#topic_tagsObject



104
105
106
# File 'app/serializers/reviewable_serializer.rb', line 104

def topic_tags
  object.topic.tags.map(&:name)
end

#topic_urlObject



123
124
125
# File 'app/serializers/reviewable_serializer.rb', line 123

def topic_url
  object.topic&.url
end