diff --git a/lib/post_creator.rb b/lib/post_creator.rb index ae261c022..c9cac0374 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 764aae73a..15155b727 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!