/posts/:id/events incorrectly lists appeals as always being resolved. This is because events UNION together appeals and flags, which doesn't quite work because for appeals is_resolved is a method, not an attribute. is_resolved was hardcoded to true so it'd work in the UNION. This changes PostEvent to be a wrapper object around PostFlag / PostAppeal, instead of a UNION. PostEvent delegates everything to the inner flag/appeals object, so that is_resolved works correctly. Also, this incidentally fixes a problem with /posts/:id/event.xml not serializing correctly.
37 lines
686 B
Ruby
37 lines
686 B
Ruby
class PostEvent
|
|
include ActiveModel::Model
|
|
include ActiveModel::Serializers::JSON
|
|
include ActiveModel::Serializers::Xml
|
|
|
|
attr_accessor :event
|
|
delegate :creator_id, :reason, :is_resolved, :created_at, to: :event
|
|
|
|
def self.find_for_post(post_id)
|
|
post = Post.find(post_id)
|
|
(post.appeals + post.flags).sort_by(&:created_at).reverse.map { |e| new(event: e) }
|
|
end
|
|
|
|
def type_name
|
|
case event
|
|
when PostFlag
|
|
"flag"
|
|
when PostAppeal
|
|
"appeal"
|
|
end
|
|
end
|
|
|
|
def type
|
|
type_name.first
|
|
end
|
|
|
|
def attributes
|
|
{
|
|
"creator_id": nil,
|
|
"created_at": nil,
|
|
"reason": nil,
|
|
"is_resolved": nil,
|
|
"type": nil,
|
|
}
|
|
end
|
|
end
|