FIX: When approving/rejecting a post it should delete the user action

This commit is contained in:
Robin Ward 2015-04-24 15:25:47 -04:00
parent 3a6efa25f0
commit 3660fe4f60
4 changed files with 22 additions and 10 deletions

View File

@ -7,6 +7,14 @@ class QueuedPost < ActiveRecord::Base
belongs_to :approved_by, class_name: "User" belongs_to :approved_by, class_name: "User"
belongs_to :rejected_by, class_name: "User" belongs_to :rejected_by, class_name: "User"
def create_pending_action
UserAction.log_action!(action_type: UserAction::PENDING,
user_id: user_id,
acting_user_id: user_id,
target_topic_id: topic_id,
queued_post_id: id)
end
def self.states def self.states
@states ||= Enum.new(:new, :approved, :rejected) @states ||= Enum.new(:new, :approved, :rejected)
end end
@ -72,6 +80,10 @@ class QueuedPost < ActiveRecord::Base
row_count = QueuedPost.where('id = ? AND state <> ?', id, state_val).update_all(updates) row_count = QueuedPost.where('id = ? AND state <> ?', id, state_val).update_all(updates)
raise InvalidStateTransition.new if row_count == 0 raise InvalidStateTransition.new if row_count == 0
if [:rejected, :approved].include?(state)
UserAction.where(queued_post_id: id).destroy_all
end
# Update the record in memory too, and clear the dirty flag # Update the record in memory too, and clear the dirty flag
updates.each {|k, v| send("#{k}=", v) } updates.each {|k, v| send("#{k}=", v) }
changes_applied changes_applied

View File

@ -24,11 +24,7 @@ class PostEnqueuer
end end
if queued_post.save if queued_post.save
UserAction.log_action!(action_type: UserAction::PENDING, queued_post.create_pending_action
user_id: @user.id,
acting_user_id: @user.id,
target_topic_id: args[:topic_id],
queued_post_id: queued_post.id)
else else
add_errors_from(queued_post) add_errors_from(queued_post)
end end

View File

@ -10,9 +10,6 @@ describe PostEnqueuer do
let(:enqueuer) { PostEnqueuer.new(user, 'new_post') } let(:enqueuer) { PostEnqueuer.new(user, 'new_post') }
it 'enqueues the post' do it 'enqueues the post' do
old_count = user.user_actions.count
qp = enqueuer.enqueue(raw: 'This should be enqueued', qp = enqueuer.enqueue(raw: 'This should be enqueued',
topic_id: topic.id, topic_id: topic.id,
post_options: { reply_to_post_number: 1 }) post_options: { reply_to_post_number: 1 })
@ -21,7 +18,7 @@ describe PostEnqueuer do
expect(qp).to be_present expect(qp).to be_present
expect(qp.topic).to eq(topic) expect(qp.topic).to eq(topic)
expect(qp.user).to eq(user) expect(qp.user).to eq(user)
expect(UserAction.where(user_id: user.id).count).to eq(old_count + 1) expect(UserAction.where(user_id: user.id, action_type: UserAction::PENDING).count).to eq(1)
end end
end end

View File

@ -26,6 +26,7 @@ describe QueuedPost do
end end
it "follows the correct workflow for approval" do it "follows the correct workflow for approval" do
qp.create_pending_action
post = qp.approve!(admin) post = qp.approve!(admin)
# Creates the post with the attributes # Creates the post with the attributes
@ -39,6 +40,9 @@ describe QueuedPost do
expect(qp.state).to eq(QueuedPost.states[:approved]) expect(qp.state).to eq(QueuedPost.states[:approved])
expect(qp.approved_at).to be_present expect(qp.approved_at).to be_present
# It removes the pending action
expect(UserAction.where(queued_post_id: qp.id).count).to eq(0)
# We can't approve twice # We can't approve twice
expect(-> { qp.approve!(admin) }).to raise_error(QueuedPost::InvalidStateTransition) expect(-> { qp.approve!(admin) }).to raise_error(QueuedPost::InvalidStateTransition)
end end
@ -50,6 +54,7 @@ describe QueuedPost do
end end
it "follows the correct workflow for rejection" do it "follows the correct workflow for rejection" do
qp.create_pending_action
qp.reject!(admin) qp.reject!(admin)
# Updates the QP record # Updates the QP record
@ -57,6 +62,9 @@ describe QueuedPost do
expect(qp.state).to eq(QueuedPost.states[:rejected]) expect(qp.state).to eq(QueuedPost.states[:rejected])
expect(qp.rejected_at).to be_present expect(qp.rejected_at).to be_present
# It removes the pending action
expect(UserAction.where(queued_post_id: qp.id).count).to eq(0)
# We can't reject twice # We can't reject twice
expect(-> { qp.reject!(admin) }).to raise_error(QueuedPost::InvalidStateTransition) expect(-> { qp.reject!(admin) }).to raise_error(QueuedPost::InvalidStateTransition)
end end
@ -115,7 +123,6 @@ describe QueuedPost do
end end
context "visibility" do context "visibility" do
it "works as expected in the invisible queue" do it "works as expected in the invisible queue" do
qp = Fabricate(:queued_post, queue: 'invisible') qp = Fabricate(:queued_post, queue: 'invisible')
expect(qp).to_not be_visible expect(qp).to_not be_visible