-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Prevent double validations on save/save! (#70)
* Prevent double validations on save/save! Originally, calling `form.save/!` would cause model validations to fire off twice. The first time when the form's validations are being run (as the form has a validation to ensure that all `models` are valid`) The second time is when the `models` are looped over and they each receive `save!`. For validations that are done with in-memory data, such as `validate :attribute, presence: true`, this isn't a big deal. But this could be problematic for any validation that is dependent on the state of the database (it will effectively hit the database twice). This instead passes `validate: false` into the saving of models so that their validations aren't run a second time. * Update README to reflect line count
- Loading branch information
Showing
4 changed files
with
43 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,9 @@ | ||
class User < ActiveRecord::Base | ||
validates :name, presence: true | ||
|
||
validate :custom_validation | ||
|
||
def custom_validation | ||
'A validation method used in the specs' | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'Model validations' do | ||
let(:form) { WithValidationCallbacksForm.new(args) } | ||
let(:email) { '[email protected]' } | ||
let(:name) { 'John' } | ||
let(:args) do | ||
{ email: email, name: name, before_counter: 0, after_counter: 0 } | ||
end | ||
|
||
context 'by default' do | ||
let(:options) { {} } | ||
|
||
%i[save save!].each do |persistence_method| | ||
it "calling #{persistence_method} runs the model validations only once" do | ||
expect(form.user).to receive(:custom_validation).once | ||
|
||
form.send(persistence_method, options) | ||
end | ||
end | ||
end | ||
|
||
context 'with validate: false' do | ||
let(:options) { { validate: false } } | ||
|
||
%i[save save!].each do |persistence_method| | ||
it "calling #{persistence_method} doesn't run model validations" do | ||
expect(form.user).not_to receive(:custom_validation) | ||
|
||
form.send(persistence_method, options) | ||
end | ||
end | ||
end | ||
end |