Skip to content

Commit

Permalink
Merge pull request #2927 from evazion/fix-post-events-resolved
Browse files Browse the repository at this point in the history
/posts/:id/events: list is_resolved correctly for appeals.
  • Loading branch information
r888888888 committed Mar 20, 2017
2 parents 9389d1a + 5ba1df5 commit fece0e3
Show file tree
Hide file tree
Showing 2 changed files with 43 additions and 34 deletions.
60 changes: 26 additions & 34 deletions app/models/post_event.rb
@@ -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
17 changes: 17 additions & 0 deletions test/functional/post_events_controller_test.rb
Expand Up @@ -26,4 +26,21 @@ def teardown
assert_response :ok
end
end

context "GET /posts/:post_id/events.xml" do
setup do
get :index, {:post_id => @post.id, :format => :xml}, {:user_id => CurrentUser.user.id}

@xml = Hash.from_xml(response.body)
@appeal = @xml["post_events"].find { |e| e["type"] == "a" }
end

should "render" do
assert_not_nil(@appeal)
end

should "return is_resolved correctly" do
assert_equal(false, @appeal["is_resolved"])
end
end
end

0 comments on commit fece0e3

Please sign in to comment.