Skip to content

A platform for community discussion. Free, open, simple.

FIX: Discard empty bundles for reviewables (#30121)

Martin Brennan8a89a7724876
diff --git a/app/models/reviewable.rb b/app/models/reviewable.rb
index 5535ca375f3..a9e957b7f34 100644
--- a/app/models/reviewable.rb
+++ b/app/models/reviewable.rb
@@ -270,8 +270,15 @@ class Reviewable < ActiveRecord::Base
def actions_for(guardian, args = nil)
args ||= {}
+ built_actions =
+ Actions.new(self, guardian).tap { |actions| build_actions(actions, guardian, args) }
- Actions.new(self, guardian).tap { |actions| build_actions(actions, guardian, args) }
+ # Empty bundles can cause big issues on the client side, so we remove them
+ # here. It's not valid anyway to have a bundle with no actions, but you can
+ # add a bundle via actions.add_bundle and then not add any actions to it.
+ built_actions.bundles.reject!(&:empty?)
+
+ built_actions
end
def editable_for(guardian, args = nil)
diff --git a/lib/reviewable/actions.rb b/lib/reviewable/actions.rb
index 4df0eaee70f..5714035eeea 100644
--- a/lib/reviewable/actions.rb
+++ b/lib/reviewable/actions.rb
@@ -30,6 +30,10 @@ class Reviewable < ActiveRecord::Base
@label = label
@actions = []
end
+
+ def empty?
+ @actions.empty?
+ end
end
class Action < Item
diff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rb
index de197304323..387e4f65115 100644
--- a/spec/models/reviewable_spec.rb
+++ b/spec/models/reviewable_spec.rb
@@ -621,6 +621,45 @@ RSpec.describe Reviewable, type: :model do
end
end
+ describe "#actions_for" do
+ fab!(:reviewable) { Fabricate(:reviewable_queued_post) }
+ fab!(:user)
+
+ it "gets the bundles and actions for a reviewable" do
+ actions = reviewable.actions_for(user.guardian)
+ expect(actions.bundles.map(&:id)).to eq(%w[approve_post reject_post revise_and_reject_post])
+ expect(actions.bundles.find { |b| b.id == "approve_post" }.actions.map(&:id)).to eq(
+ ["approve_post"],
+ )
+ expect(actions.bundles.find { |b| b.id == "reject_post" }.actions.map(&:id)).to eq(
+ ["reject_post"],
+ )
+ expect(actions.bundles.find { |b| b.id == "revise_and_reject_post" }.actions.map(&:id)).to eq(
+ ["revise_and_reject_post"],
+ )
+ end
+
+ describe "handling empty bundles" do
+ class ReviewableTestRecord < Reviewable
+ def build_actions(actions, guardian, args)
+ actions.add(:approve_post) do |action|
+ action.icon = "check"
+ action.label = "reviewables.actions.approve_post.title"
+ end
+ actions.add_bundle("empty_bundle", icon: "xmark", label: "Empty Bundle")
+ end
+ end
+
+ it "does not return empty bundles with no actions" do
+ actions = ReviewableTestRecord.new.actions_for(user.guardian)
+ expect(actions.bundles.map(&:id)).to eq(%w[approve_post])
+ expect(actions.bundles.find { |b| b.id == "approve_post" }.actions.map(&:id)).to eq(
+ ["approve_post"],
+ )
+ end
+ end
+ end
+
describe "default actions" do
let(:reviewable) { Reviewable.new }
let(:actions) { Reviewable::Actions.new(reviewable, Guardian.new) }