Skip to content

Commit

Permalink
Cache autoflag status on Post
Browse files Browse the repository at this point in the history
  • Loading branch information
ArtOfCode- committed Jul 19, 2017
1 parent 591defc commit 6a44fb4
Show file tree
Hide file tree
Showing 4 changed files with 27 additions and 11 deletions.
17 changes: 7 additions & 10 deletions app/models/post.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,9 @@ class Post < ApplicationRecord
scope(:includes_for_post_row, -> { includes(:stack_exchange_user).includes(:reasons).includes(feedbacks: [:user, :api_key]) })
scope(:without_feedback, -> { left_joins(:feedbacks).where(feedbacks: { post_id: nil }) })

scope(:autoflagged, -> { includes(:flag_logs).where(flag_logs: { is_auto: true }) })
scope(:autoflagged, -> { where(autoflagged: true) })

scope(:not_autoflagged, lambda {
# I'm sorry.
left_joins(:flag_logs)
.joins("LEFT JOIN (SELECT posts.id AS 'post_id', COUNT(DISTINCT flag_logs.id) AS 'autoflag_count' FROM posts INNER JOIN flag_logs " \
'ON flag_logs.post_id = posts.id WHERE flag_logs.is_auto = 1 GROUP BY posts.id) AS flag_counts ON flag_counts.post_id = posts.id')
.where(flag_counts: { autoflag_count: 0 }) +
left_joins(:flag_logs).where(flag_logs: { post_id: nil })
})
scope(:not_autoflagged, -> { where(autoflagged: false) })

after_create do
ActionCable.server.broadcast 'posts_realtime', row: PostsController.render(locals: { post: Post.last }, partial: 'post').html_safe
Expand Down Expand Up @@ -96,7 +89,11 @@ def autoflag
raise if Rails.env.test?
end

post.send_not_autoflagged if post.flag_logs.where(success: true).empty?
if post.flag_logs.where(success: true).empty?
post.send_not_autoflagged
else
post.update(autoflagged: true)
end
end

def send_autoflag(user, dry_run, condition)
Expand Down
12 changes: 12 additions & 0 deletions db/migrate/20170719220932_add_autoflagged_to_posts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
class AddAutoflaggedToPosts < ActiveRecord::Migration[5.1]
def change
add_column :posts, :autoflagged, :boolean, default: false

Post.where(id: (Post.left_joins(:flag_logs)
.joins("LEFT JOIN (SELECT posts.id AS 'post_id', COUNT(DISTINCT flag_logs.id) AS 'autoflag_count' FROM posts INNER JOIN flag_logs " \
'ON flag_logs.post_id = posts.id WHERE flag_logs.is_auto = 1 GROUP BY posts.id) AS flag_counts ON flag_counts.post_id = posts.id')
.where(flag_counts: { autoflag_count: 0 }) +
Post.left_joins(:flag_logs).where(flag_logs: { post_id: nil })).map(&:id)).update_all(autoflagged: false)
Post.joins(:flag_logs).where(flag_logs: { is_auto: true }).update_all(autoflagged: true)
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema.define(version: 20170707225913) do
ActiveRecord::Schema.define(version: 20170719220932) do

create_table "announcements", force: :cascade, options: "ENGINE=InnoDB DEFAULT CHARSET=utf8" do |t|
t.string "text"
Expand Down Expand Up @@ -202,6 +202,7 @@
t.integer "revision_count"
t.datetime "deleted_at"
t.integer "smoke_detector_id"
t.boolean "autoflagged"
t.index ["created_at"], name: "index_posts_on_created_at"
t.index ["link"], name: "index_posts_on_link", length: { link: 191 }
end
Expand Down
6 changes: 6 additions & 0 deletions test/controllers/search_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,10 @@ class SearchControllerTest < ActionController::TestCase
get :index, params: { title: 'foo', title_is_regex: true, title_is_inverse_regex: true }
assert_response :success
end

test 'should search by not autoflagged' do
get :index, params: { autoflagged: 'No' }
assert_response :success
assert_not_nil assigns(:results)
end
end

0 comments on commit 6a44fb4

Please sign in to comment.