FIX: error when re-bookmarking a topic/post

This commit is contained in:
Régis Hanol 2015-02-19 16:43:12 +01:00
parent 873f6b7487
commit e09e8e0b76
1 changed files with 10 additions and 12 deletions

View File

@ -242,28 +242,26 @@ class PostAction < ActiveRecord::Base
post_action_type_id: post_action_type_id
}
action_attributes = {
action_attrs = {
staff_took_action: staff_took_action,
related_post_id: related_post_id,
targets_topic: !!targets_topic
}
# First try to revive a trashed record
row_count = PostAction.where(where_attrs)
.with_deleted
.where("deleted_at IS NOT NULL")
.update_all(action_attributes.merge(deleted_at: nil))
post_action = PostAction.where(where_attrs)
.with_deleted
.where("deleted_at IS NOT NULL")
.first
if row_count == 0
post_action = create(where_attrs.merge(action_attributes))
if post_action
post_action.recover!
post_action.save
else
post_action = create(where_attrs.merge(action_attrs))
if post_action && post_action.errors.count == 0
BadgeGranter.queue_badge_grant(Badge::Trigger::PostAction, post_action: post_action)
end
else
post_action = PostAction.where(where_attrs).first
# after_commit is not called on an 'update_all' so do the notify ourselves
post_action.notify_subscribers
end
# agree with other flags