-
Notifications
You must be signed in to change notification settings - Fork 407
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
/posts/:id/events: list is_resolved correctly for appeals.
/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.
- Loading branch information
Showing
2 changed files
with
43 additions
and
34 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,44 +1,36 @@ | ||
class PostEvent | ||
class Instance | ||
attr_reader :creator_id, :reason, :is_resolved, :created_at, :type | ||
include ActiveModel::Model | ||
include ActiveModel::Serializers::JSON | ||
include ActiveModel::Serializers::Xml | ||
|
||
def initialize(row) | ||
@creator_id = row["creator_id"].to_i | ||
@reason = row["reason"] | ||
@is_resolved = (row["is_resolved"] == "t") | ||
@created_at = row["created_at"].to_time | ||
@type = row["type"] | ||
end | ||
|
||
def creator | ||
User.find(creator_id) | ||
end | ||
|
||
def type_name | ||
if appeal? | ||
"appeal" | ||
else | ||
"flag" | ||
end | ||
end | ||
attr_accessor :event | ||
delegate :creator_id, :reason, :is_resolved, :created_at, to: :event | ||
|
||
def appeal? | ||
type == "a" | ||
end | ||
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 flag? | ||
type == "f" | ||
def type_name | ||
case event | ||
when PostFlag | ||
"flag" | ||
when PostAppeal | ||
"appeal" | ||
end | ||
end | ||
|
||
QUERY = <<-EOS | ||
(SELECT post_flags.creator_id, post_flags.reason, post_flags.is_resolved, post_flags.created_at, 'f' as type FROM post_flags WHERE post_flags.post_id = ?) | ||
UNION | ||
(SELECT post_appeals.creator_id, post_appeals.reason, 't' AS is_resolved, post_appeals.created_at, 'a' as type FROM post_appeals WHERE post_appeals.post_id = ?) | ||
ORDER BY created_at | ||
EOS | ||
def type | ||
type_name.first | ||
end | ||
|
||
def self.find_for_post(post_id) | ||
ActiveRecord::Base.select_all_sql(QUERY, post_id, post_id).map {|x| Instance.new(x)} | ||
def attributes | ||
{ | ||
"creator_id": nil, | ||
"created_at": nil, | ||
"reason": nil, | ||
"is_resolved": nil, | ||
"type": nil, | ||
} | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters