minor refactor

This commit is contained in:
Sam 2013-08-15 13:44:30 +10:00
parent fcbb9919b6
commit fda6cce0de
1 changed files with 18 additions and 9 deletions

View File

@ -17,6 +17,9 @@ class PostAction < ActiveRecord::Base
scope :spam_flags, -> { where(post_action_type_id: PostActionType.types[:spam]) } scope :spam_flags, -> { where(post_action_type_id: PostActionType.types[:spam]) }
after_save :update_counters
after_save :enforce_rules
def self.update_flagged_posts_count def self.update_flagged_posts_count
posts_flagged_count = PostAction.joins(post: :topic) posts_flagged_count = PostAction.joins(post: :topic)
.where('defer = false or defer IS NULL') .where('defer = false or defer IS NULL')
@ -140,14 +143,14 @@ class PostAction < ActiveRecord::Base
user_id: user.id, user_id: user.id,
post_action_type_id: post_action_type_id:
post_action_type_id).first post_action_type_id).first
action.trash!(user) action.remove_act!(user)
action.run_callbacks(:save)
end end
end end
def remove_act!(user) def remove_act!(user)
trash!(user) trash!(user)
run_callbacks(:save) update_counters
enforce_rules
end end
def is_bookmark? def is_bookmark?
@ -214,14 +217,18 @@ class PostAction < ActiveRecord::Base
[flag_counts['old_flags'].to_i, flag_counts['new_flags'].to_i] [flag_counts['old_flags'].to_i, flag_counts['new_flags'].to_i]
end end
after_save do def post_action_type_key
PostActionType.types[post_action_type_id]
end
def update_counters
# Update denormalized counts # Update denormalized counts
post_action_type = PostActionType.types[post_action_type_id] column = "#{post_action_type_key.to_s}_count"
column = "#{post_action_type.to_s}_count"
delta = deleted_at.nil? ? 1 : -1 delta = deleted_at.nil? ? 1 : -1
# We probably want to refactor this method to something cleaner. # We probably want to refactor this method to something cleaner.
case post_action_type case post_action_type_key
when :vote when :vote
# Voting also changes the sort_order # Voting also changes the sort_order
Post.where(id: post_id).update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)", Post.where(id: post_id).update_all ["vote_count = vote_count + :delta, sort_order = :max - (vote_count + :delta)",
@ -243,9 +250,11 @@ class PostAction < ActiveRecord::Base
PostAction.update_flagged_posts_count PostAction.update_flagged_posts_count
end end
PostAction.auto_hide_if_needed(post, post_action_type) end
SpamRulesEnforcer.enforce!(post.user) if post_action_type == :spam def enforce_rules
PostAction.auto_hide_if_needed(post, post_action_type_key)
SpamRulesEnforcer.enforce!(post.user) if post_action_type_key == :spam
end end
def self.auto_hide_if_needed(post, post_action_type) def self.auto_hide_if_needed(post, post_action_type)