From 2713b77e280ebd9af995f498548d866bbdb126d7 Mon Sep 17 00:00:00 2001 From: Robin Ward Date: Tue, 21 Apr 2015 13:16:05 -0400 Subject: [PATCH] FIX: If validations were disabled some rate limits continued to run --- lib/post_creator.rb | 1 + lib/rate_limiter/on_create_record.rb | 16 +++++++++++++++- 2 files changed, 16 insertions(+), 1 deletion(-) diff --git a/lib/post_creator.rb b/lib/post_creator.rb index ae261c02213..c9cac0374ee 100644 --- a/lib/post_creator.rb +++ b/lib/post_creator.rb @@ -276,6 +276,7 @@ class PostCreator end def save_post + @post.disable_rate_limits! if skip_validations? saved = @post.save(validate: !skip_validations?) rollback_from_errors!(@post) unless saved end diff --git a/lib/rate_limiter/on_create_record.rb b/lib/rate_limiter/on_create_record.rb index 764aae73a4d..15155b727ce 100644 --- a/lib/rate_limiter/on_create_record.rb +++ b/lib/rate_limiter/on_create_record.rb @@ -23,6 +23,16 @@ class RateLimiter def self.included(base) 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 module ClassMethods @@ -30,7 +40,9 @@ class RateLimiter 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) rate_limiter.performed! @performed ||= {} @@ -39,12 +51,14 @@ class RateLimiter end self.after_destroy do + next if @rate_limits_disabled if rate_limiter = send(limiter_method) rate_limiter.rollback! end end self.after_rollback do + next if @rate_limits_disabled if rate_limiter = send(limiter_method) if @performed.present? && @performed[limiter_method] rate_limiter.rollback!