Skip to content

A platform for community discussion. Free, open, simple.

FIX: Don’t run validations when invalidating invites

Loïc Guitaut37b71c5903f3
diff --git a/app/models/invite.rb b/app/models/invite.rb
index 66697d34dfb..191da0818ca 100644
--- a/app/models/invite.rb
+++ b/app/models/invite.rb
@@ -274,10 +274,12 @@ class Invite < ActiveRecord::Base
end
def self.invalidate_for_email(email)
- invite = Invite.find_by(email: Email.downcase(email))
- invite.update!(invalidated_at: Time.zone.now) if invite
+ Invite.find_by(email: Email.downcase(email))&.invalidate!
+ end
- invite
+ def invalidate!
+ update_attribute(:invalidated_at, Time.current)
+ self
end
def resend_invite
diff --git a/spec/models/invite_spec.rb b/spec/models/invite_spec.rb
index d467b30e8f4..e93f09561a1 100644
--- a/spec/models/invite_spec.rb
+++ b/spec/models/invite_spec.rb
@@ -478,7 +478,7 @@ RSpec.describe Invite do
end
end
- describe "#invalidate_for_email" do
+ describe ".invalidate_for_email" do
it "returns nil if there is no invite for the given email" do
invite = Invite.invalidate_for_email("[email protected]")
expect(invite).to eq(nil)
@@ -600,4 +600,29 @@ RSpec.describe Invite do
end
end
end
+
+ describe "#invalidate!" do
+ subject(:invalidate) { invite.invalidate! }
+
+ fab!(:invite) { Fabricate(:invite) }
+
+ before { freeze_time }
+
+ it "invalidates the invite" do
+ expect { invalidate }.to change { invite.invalidated_at }.to Time.current
+ end
+
+ it "returns the invite" do
+ expect(invalidate).to eq invite
+ end
+
+ context "when the invite is in an invalid state" do
+ before { invite.update_attribute(:custom_message, "a" * 2000) }
+
+ it "still invalidates the invite" do
+ expect(invite).to be_invalid
+ expect { invalidate }.to change { invite.invalidated_at }.to Time.current
+ end
+ end
+ end
end