Skip to content
Projects
Groups
Snippets
Help
Loading...
Help
Support
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
G
gitlab-ce
Project overview
Project overview
Details
Activity
Releases
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Issues
0
Issues
0
List
Boards
Labels
Milestones
Merge Requests
1
Merge Requests
1
Analytics
Analytics
Repository
Value Stream
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Create a new issue
Commits
Issue Boards
Open sidebar
nexedi
gitlab-ce
Commits
ca1aafee
Commit
ca1aafee
authored
Aug 04, 2020
by
Markus Koller
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Allow passing a block to safe_find_or_create_by
parent
c2155083
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
20 additions
and
4 deletions
+20
-4
app/models/application_record.rb
app/models/application_record.rb
+4
-4
spec/models/application_record_spec.rb
spec/models/application_record_spec.rb
+16
-0
No files found.
app/models/application_record.rb
View file @
ca1aafee
...
...
@@ -42,15 +42,15 @@ class ApplicationRecord < ActiveRecord::Base
limit
(
count
)
end
def
self
.
safe_find_or_create_by!
(
*
args
)
safe_find_or_create_by
(
*
args
).
tap
do
|
record
|
def
self
.
safe_find_or_create_by!
(
*
args
,
&
block
)
safe_find_or_create_by
(
*
args
,
&
block
).
tap
do
|
record
|
record
.
validate!
unless
record
.
persisted?
end
end
def
self
.
safe_find_or_create_by
(
*
args
)
def
self
.
safe_find_or_create_by
(
*
args
,
&
block
)
safe_ensure_unique
(
retries:
1
)
do
find_or_create_by
(
*
args
)
find_or_create_by
(
*
args
,
&
block
)
end
end
...
...
spec/models/application_record_spec.rb
View file @
ca1aafee
...
...
@@ -38,6 +38,14 @@ RSpec.describe ApplicationRecord do
expect
{
Suggestion
.
safe_find_or_create_by
(
build
(
:suggestion
).
attributes
)
}
.
to
change
{
Suggestion
.
count
}.
by
(
1
)
end
it
'passes a block to find_or_create_by'
do
attributes
=
build
(
:suggestion
).
attributes
expect
do
|
block
|
Suggestion
.
safe_find_or_create_by
(
attributes
,
&
block
)
end
.
to
yield_with_args
(
an_object_having_attributes
(
attributes
))
end
end
describe
'.safe_find_or_create_by!'
do
...
...
@@ -51,6 +59,14 @@ RSpec.describe ApplicationRecord do
it
'raises a validation error if the record was not persisted'
do
expect
{
Suggestion
.
find_or_create_by!
(
note:
nil
)
}.
to
raise_error
(
ActiveRecord
::
RecordInvalid
)
end
it
'passes a block to find_or_create_by'
do
attributes
=
build
(
:suggestion
).
attributes
expect
do
|
block
|
Suggestion
.
safe_find_or_create_by!
(
attributes
,
&
block
)
end
.
to
yield_with_args
(
an_object_having_attributes
(
attributes
))
end
end
describe
'.underscore'
do
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment