Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Disable validations on destroy #110

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/permanent_records.rb
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ def set_deleted_at(value, force = nil)
# ActiveRecord::RecordInvalid error will be raised if the record isn't
# valid. (This prevents reviving records that disregard validation
# constraints,)
if PermanentRecords.should_ignore_validations?(force)
if PermanentRecords.should_ignore_validations?(force) || value.present?
record.save(validate: false)
else
record.save!
Expand Down
2 changes: 1 addition & 1 deletion permanent_records.gemspec
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ Gem::Specification.new do |s|
s.add_development_dependency 'rspec', '>= 3.5.0'
s.add_development_dependency 'rubocop', '~> 0.68.0' # freeze to ensure ruby 2.2 compatibility
s.add_development_dependency 'rubocop-performance'
s.add_development_dependency 'sqlite3', '~> 1.3.13' # freeze to ensure specs are working
s.add_development_dependency 'sqlite3', '~> 1.4' # freeze to ensure specs are working
end
24 changes: 12 additions & 12 deletions spec/permanent_records_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@
before do
allow_any_instance_of(Hole).to receive(:valid?).and_return(false)
end
it 'raises' do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
it 'does not raise' do
expect { subject }.not_to raise_error(ActiveRecord::RecordInvalid)
end

context 'with validation opt-out' do
Expand Down Expand Up @@ -122,9 +122,9 @@

context 'when error occurs' do
before { allow_any_instance_of(Hole).to receive(:valid?).and_return(false) }
it 'does not mark records as deleted' do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
expect(record.muskrats.not_deleted.count).to eq(1)
it 'marks records as deleted' do
expect { subject }.not_to raise_error(ActiveRecord::RecordInvalid)
expect(record.muskrats.not_deleted.count).to eq(0)
end
end

Expand All @@ -150,9 +150,9 @@

context 'when error occurs' do
before { allow_any_instance_of(Hole).to receive(:valid?).and_return(false) }
it('does not mark records as deleted') do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
expect(record.reload.location).not_to be_deleted
it('marks records as deleted') do
expect { subject }.not_to raise_error(ActiveRecord::RecordInvalid)
expect(record.reload.location).to be_deleted
end
end

Expand All @@ -178,9 +178,9 @@

context 'when error occurs' do
before { allow_any_instance_of(Hole).to receive(:valid?).and_return(false) }
it 'does not mark records as deleted' do
expect { subject }.to raise_error(ActiveRecord::RecordInvalid)
expect(record.dirt).not_to be_deleted
it 'marks records as deleted' do
expect { subject }.not_to raise_error(ActiveRecord::RecordInvalid)
expect(record.dirt).to be_deleted
end
end

Expand Down Expand Up @@ -293,7 +293,7 @@
end

context 'that were deleted previously' do
before { muskrat.update_attributes! deleted_at: 2.minutes.ago }
before { muskrat.update_attribute :deleted_at, 2.minutes.ago }
it 'does not restore' do
expect { subject }.to_not change(muskrat, :deleted?)
end
Expand Down