Fix magic numbers, extra param references

This commit is contained in:
riking 2014-08-31 19:36:31 -07:00
parent 1833b43ae2
commit 808460a28f
2 changed files with 5 additions and 3 deletions

View file

@ -33,15 +33,15 @@ class Badge < ActiveRecord::Base
UserChange = 8
def self.is_none?(trigger)
[0].include? trigger
[None].include? trigger
end
def self.uses_user_ids?(trigger)
[4, 8].include? trigger
[TrustLevelChange, UserChange].include? trigger
end
def self.uses_post_ids?(trigger)
[1, 2].include? trigger
[PostAction, PostRevision].include? trigger
end
end

View file

@ -139,9 +139,11 @@ class BadgeGranter
return unless sql.present?
if Badge::Trigger.uses_post_ids?(opts[:trigger])
raise "Contract violation:\nQuery triggers on posts, but does not reference the ':post_ids' array" unless sql.match /:post_ids/
raise "Contract violation:\nQuery triggers on posts, but references the ':user_ids' array" if sql.match /:user_ids/
end
if Badge::Trigger.uses_user_ids?(opts[:trigger])
raise "Contract violation:\nQuery triggers on users, but does not reference the ':user_ids' array" unless sql.match /:user_ids/
raise "Contract violation:\nQuery triggers on users, but references the ':post_ids' array" if sql.match /:post_ids/
end
if opts[:trigger] && !Badge::Trigger.is_none?(opts[:trigger])
raise "Contract violation:\nQuery is triggered, but does not reference the ':backfill' parameter.\n(Hint: if :backfill is TRUE, you should ignore the :post_ids/:user_ids)" unless sql.match /:backfill/