FIX: If validations were disabled some rate limits continued to run

This commit is contained in:
Robin Ward 2015-04-21 13:16:05 -04:00
parent b2ab95f9c2
commit 2713b77e28
2 changed files with 16 additions and 1 deletions

View file

@ -276,6 +276,7 @@ class PostCreator
end end
def save_post def save_post
@post.disable_rate_limits! if skip_validations?
saved = @post.save(validate: !skip_validations?) saved = @post.save(validate: !skip_validations?)
rollback_from_errors!(@post) unless saved rollback_from_errors!(@post) unless saved
end end

View file

@ -23,6 +23,16 @@ class RateLimiter
def self.included(base) def self.included(base)
base.extend(ClassMethods) base.extend(ClassMethods)
base.include(InstanceMethods)
end
module InstanceMethods
# For the lifetime of this instance, don't enforce rate limits.
def disable_rate_limits!
@rate_limits_disabled = true
end
end end
module ClassMethods module ClassMethods
@ -30,7 +40,9 @@ class RateLimiter
limiter_method = limiter_method || :default_rate_limiter limiter_method = limiter_method || :default_rate_limiter
self.after_create do self.after_create do |*args|
next if @rate_limits_disabled
if rate_limiter = send(limiter_method) if rate_limiter = send(limiter_method)
rate_limiter.performed! rate_limiter.performed!
@performed ||= {} @performed ||= {}
@ -39,12 +51,14 @@ class RateLimiter
end end
self.after_destroy do self.after_destroy do
next if @rate_limits_disabled
if rate_limiter = send(limiter_method) if rate_limiter = send(limiter_method)
rate_limiter.rollback! rate_limiter.rollback!
end end
end end
self.after_rollback do self.after_rollback do
next if @rate_limits_disabled
if rate_limiter = send(limiter_method) if rate_limiter = send(limiter_method)
if @performed.present? && @performed[limiter_method] if @performed.present? && @performed[limiter_method]
rate_limiter.rollback! rate_limiter.rollback!