A platform for community discussion. Free, open, simple.
DEV: Touch-ups and test coverage for Reviewable#perform (#34950)
Ted Johanssonde93ccbade58
diff --git a/app/models/reviewable.rb b/app/models/reviewable.rbindex 450723dc051..d9241cfeada 100644--- a/app/models/reviewable.rb+++ b/app/models/reviewable.rb@@ -350,17 +350,10 @@ class Reviewable < ActiveRecord::Base# the result of the operation and whether the status of the reviewable changed.def perform(performed_by, action_id, args = nil)args ||= {}- # Support this action or any aliases- aliases = self.class.action_aliases- valid = [action_id, aliases.to_a.select { |k, v| v == action_id }.map(&:first)].flatten-- # Ensure the user has access to the action+ perform_method = "perform_#{aliases[action_id] || action_id}".to_symguardian = args[:guardian] || Guardian.new(performed_by)- actions = actions_for(guardian, args)- raise InvalidAction.new(action_id, self.class) unless valid.any? { |a| actions.has?(a) }- perform_method = "perform_#{aliases[action_id] || action_id}".to_sym- raise InvalidAction.new(action_id, self.class) unless respond_to?(perform_method)+ validate_action!(guardian, action_id, perform_method, args)result = nilupdate_count = false@@ -827,6 +820,22 @@ class Reviewable < ActiveRecord::Baseprivate+ def aliases+ self.class.action_aliases+ end++ def validate_action!(guardian, action_id, perform_method, args)+ # Support this action or any aliases+ action_aliases = [action_id, aliases.to_a.select { |k, v| v == action_id }.map(&:first)].flatten++ # Ensure the user has access to the action+ actions = actions_for(guardian, args)++ if action_aliases.none? { |a| actions.has?(a) } || !respond_to?(perform_method)+ raise InvalidAction.new(action_id, self.class)+ end+ end+def update_flag_stats(status:, user_ids:)return if %i[agreed disagreed ignored].exclude?(status)diff --git a/spec/models/reviewable_spec.rb b/spec/models/reviewable_spec.rbindex 16261b6247e..28cd1dc3925 100644--- a/spec/models/reviewable_spec.rb+++ b/spec/models/reviewable_spec.rb@@ -444,10 +444,22 @@ RSpec.describe Reviewable, type: :model doendend- describe "message bus notifications" do+ describe "#perform" dofab!(:moderator) { Fabricate(:moderator, refresh_auto_groups: true) }let(:post) { Fabricate(:post) }+ it "rolls back the transaction when the action fails" do+ reviewable = Fabricate(:reviewable_queued_post)++ reviewable.stubs(:perform_approve_post).returns(+ Reviewable::PerformResult.new(reviewable, :failure),+ )++ expect { reviewable.perform(moderator, :approve_post) }.not_to change {+ reviewable.reload.version+ }+ end+it "triggers a notification on create" doreviewable = Fabricate(:reviewable_queued_post)job = Jobs::NotifyReviewable.jobs.last