-
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.
add destroy objects during save (#87)
- Loading branch information
1 parent
4c9ea35
commit 315356a
Showing
5 changed files
with
129 additions
and
2 deletions.
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
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,21 @@ | ||
class UserDestroyForm < YAAF::Form | ||
attr_accessor :email, :name | ||
|
||
before_save :mark_user_for_destruction | ||
|
||
def initialize(args) | ||
super(args) | ||
|
||
@models = [user] | ||
end | ||
|
||
def user | ||
@user ||= User.find_or_initialize_by(email: email) | ||
end | ||
|
||
private | ||
|
||
def mark_user_for_destruction | ||
user.mark_for_destruction | ||
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,101 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe 'UserDestroyForm form' do | ||
let(:user_destroy_form) { UserDestroyForm.new(args) } | ||
let(:args) do | ||
{ email: '[email protected]', name: 'John' } | ||
end | ||
let(:user_args) { args } | ||
let!(:user) { User.create(user_args) } | ||
|
||
describe '#save' do | ||
subject { user_destroy_form.save } | ||
|
||
context 'when the user exists' do | ||
before do | ||
expect(user_destroy_form).to be_valid | ||
end | ||
|
||
it 'returns true' do | ||
expect(subject).to eq true | ||
end | ||
|
||
it 'deletes the user' do | ||
expect { subject }.to change { User.count }.by(-1) | ||
end | ||
end | ||
|
||
context 'when the user doesn\'t exist' do | ||
let(:user_args) do | ||
{ email: '[email protected]', name: 'John' } | ||
end | ||
|
||
it 'returns false' do | ||
expect(subject).to eq false | ||
end | ||
|
||
it 'doesn\'t delete the user' do | ||
expect { subject }.not_to change { User.count } | ||
end | ||
end | ||
end | ||
|
||
describe '#save!' do | ||
subject { user_destroy_form.save! } | ||
|
||
context 'when the user exists' do | ||
before do | ||
expect(user_destroy_form).to be_valid | ||
end | ||
|
||
it 'returns true' do | ||
expect(subject).to eq true | ||
end | ||
|
||
it 'deletes the user' do | ||
expect { subject }.to change { User.count }.by(-1) | ||
end | ||
end | ||
|
||
context 'when the user doesn\'t exist' do | ||
let(:user_args) do | ||
{ email: '[email protected]', name: 'John' } | ||
end | ||
|
||
it 'raises an exception' do | ||
expect { subject }.to raise_error(ActiveModel::ValidationError) | ||
end | ||
|
||
it 'doesn\'t delete the user' do | ||
expect { subject rescue nil }.not_to change { User.count } | ||
end | ||
end | ||
end | ||
|
||
describe '#valid?' do | ||
subject { user_destroy_form.valid? } | ||
|
||
it { is_expected.to be true } | ||
end | ||
|
||
describe '#invalid?' do | ||
subject { user_destroy_form.invalid? } | ||
|
||
it { is_expected.to be false } | ||
end | ||
|
||
describe '#errors' do | ||
subject do | ||
user_destroy_form.valid? | ||
user_destroy_form.errors | ||
end | ||
|
||
it 'returns the correct class' do | ||
expect(subject.class).to eq(ActiveModel::Errors) | ||
end | ||
|
||
it 'is empty' do | ||
expect(subject.messages).to be_empty | ||
end | ||
end | ||
end |