diff --git a/app/controllers/post_events_controller.rb b/app/controllers/post_events_controller.rb new file mode 100644 index 000000000..5c5cfb196 --- /dev/null +++ b/app/controllers/post_events_controller.rb @@ -0,0 +1,7 @@ +class PostEventsController < ApplicationController + before_filter :member_only + + def index + @events = PostEvent.find_for_post(params[:post_id]) + end +end diff --git a/app/models/post_event.rb b/app/models/post_event.rb new file mode 100644 index 000000000..468e65743 --- /dev/null +++ b/app/models/post_event.rb @@ -0,0 +1,44 @@ +class PostEvent + class Instance + attr_reader :creator_id, :reason, :is_resolved, :created_at, :type + + 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 + + def appeal? + type == "a" + end + + def flag? + type == "f" + 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 self.find_for_post(post_id) + ActiveRecord::Base.select_all_sql(QUERY, post_id, post_id).map {|x| Instance.new(x)} + end +end diff --git a/app/views/post_events/index.html.erb b/app/views/post_events/index.html.erb new file mode 100644 index 000000000..d2e66dce5 --- /dev/null +++ b/app/views/post_events/index.html.erb @@ -0,0 +1,38 @@ +
+
+

Flags & Appeals

+ + + + + + + + + + + + + <% @events.each do |event| %> + + + + + + + + <% end %> + +
TypeCreatorReasonResolved?Date
<%= event.type_name %><%= link_to_user event.creator %><%= format_text event.reason %> + <% if event.is_resolved %> + yes + <% else %> + no + <% end %> + <%= compact_time event.created_at %>
+
+
+ +<% content_for(:page_title) do %> + Events - <%= Danbooru.config.app_name %> +<% end %> diff --git a/app/views/posts/show.html.erb b/app/views/posts/show.html.erb index 223d317a9..dfb486e18 100644 --- a/app/views/posts/show.html.erb +++ b/app/views/posts/show.html.erb @@ -26,8 +26,7 @@ diff --git a/config/routes.rb b/config/routes.rb index 32347eba8..dd8a696ba 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -183,6 +183,7 @@ Rails.application.routes.draw do end resources :pool_versions, :only => [:index] resources :posts do + resources :events, :only => [:index], :controller => "post_events" resources :votes, :controller => "post_votes", :only => [:create, :destroy] collection do get :home diff --git a/test/functional/post_events_controller_test.rb b/test/functional/post_events_controller_test.rb new file mode 100644 index 000000000..09130ab3c --- /dev/null +++ b/test/functional/post_events_controller_test.rb @@ -0,0 +1,29 @@ +require 'test_helper' + +class PostEventsControllerTest < ActionController::TestCase + def setup + super + + Timecop.travel(2.weeks.ago) do + CurrentUser.user = FactoryGirl.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + end + + @post = FactoryGirl.create(:post) + @post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false) + @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") + end + + def teardown + super + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + context "GET /posts/:post_id/events" do + should "render" do + get :index, {:post_id => @post.id}, {:user_id => CurrentUser.user.id} + assert_response :ok + end + end +end diff --git a/test/unit/post_event_test.rb b/test/unit/post_event_test.rb new file mode 100644 index 000000000..609d87ba8 --- /dev/null +++ b/test/unit/post_event_test.rb @@ -0,0 +1,31 @@ +require 'test_helper' + +class PostEventTest < ActiveSupport::TestCase + def setup + super + + Timecop.travel(2.weeks.ago) do + CurrentUser.user = FactoryGirl.create(:user) + CurrentUser.ip_addr = "127.0.0.1" + end + + @post = FactoryGirl.create(:post) + @post_flag = PostFlag.create(:post => @post, :reason => "aaa", :is_resolved => false) + @post_appeal = PostAppeal.create(:post => @post, :reason => "aaa") + end + + def teardown + super + CurrentUser.user = nil + CurrentUser.ip_addr = nil + end + + context "PostEvent.find_for_post" do + should "work" do + results = PostEvent.find_for_post(@post.id) + assert_equal(2, results.size) + assert(results[0].flag?) + assert(results[1].appeal?) + end + end +end