BURs: clean up parsing and error handling.

* Don't raise exceptions when a BUR is invalid. Instead, use Rails
  validations to return errors. Fixes invalid BURs potentially raising
  exceptions in views. Also makes it so that each error in a BUR is
  reported, not just the first one.

* Revalidate the BUR whenever the script is edited, not just when the
  BUR is created. Ensures the BUR can't be broken by editing. Fixes a bug
  where forum threads could be broken by someone editing a BUR and
  breaking the syntax, thereby causing the BUR to raise an unparseable
  script error when the forum thread was viewed.

* Validate that removed aliases and implication actually exist.

* Validate that the tag actually exists when changing a tag's category.

* Combine bulk update request processor unit tests with main bulk update
  request unit tests.
This commit is contained in:
evazion
2020-08-24 16:41:52 -05:00
parent c295e233f2
commit 1ddcc661e1
5 changed files with 287 additions and 273 deletions

View File

@@ -9,14 +9,14 @@ class BulkUpdateRequest < ApplicationRecord
belongs_to :approver, optional: true, class_name: "User"
before_validation :normalize_text
before_validation :update_tags
validates_presence_of :reason, on: :create
validates_presence_of :script
validates_presence_of :title, if: ->(rec) {rec.forum_topic_id.blank?}
validates_presence_of :forum_topic, if: ->(rec) {rec.forum_topic_id.present?}
validates_inclusion_of :status, :in => %w(pending approved rejected)
validate :validate_script, :on => :create
validate :validate_script, if: :script_changed?
before_save :update_tags, if: :script_changed?
after_create :create_forum_topic
scope :pending_first, -> { order(Arel.sql("(case status when 'pending' then 0 when 'approved' then 1 else 2 end)")) }
@@ -108,17 +108,14 @@ class BulkUpdateRequest < ApplicationRecord
end
end
module ValidationMethods
def validate_script
processor.validate!
rescue BulkUpdateRequestProcessor::Error => e
errors[:base] << e.message
def validate_script
if processor.invalid?
errors[:base] << processor.errors.full_messages.join("; ")
end
end
extend SearchMethods
include ApprovalMethods
include ValidationMethods
def normalize_text
self.script = script.downcase
@@ -133,7 +130,7 @@ class BulkUpdateRequest < ApplicationRecord
end
def processor
@processor ||= BulkUpdateRequestProcessor.new(script, forum_topic_id: forum_topic_id, skip_secondary_validations: skip_secondary_validations)
@processor ||= BulkUpdateRequestProcessor.new(self)
end
def is_tag_move_allowed?