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
0
Merge Requests
0
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
Boxiang Sun
gitlab-ce
Commits
e96f2f24
Commit
e96f2f24
authored
Feb 26, 2019
by
Douglas Barbosa Alexandre
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Mark cached value as persisted when is an instance of ActiveRecord::Base
parent
790a51a4
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
42 additions
and
2 deletions
+42
-2
lib/gitlab/json_cache.rb
lib/gitlab/json_cache.rb
+15
-1
spec/lib/gitlab/json_cache_spec.rb
spec/lib/gitlab/json_cache_spec.rb
+27
-1
No files found.
lib/gitlab/json_cache.rb
View file @
e96f2f24
...
...
@@ -71,7 +71,21 @@ module Gitlab
end
def
parse_entry
(
raw
,
klass
)
klass
.
new
(
raw
)
if
valid_entry?
(
raw
,
klass
)
return
unless
valid_entry?
(
raw
,
klass
)
return
klass
.
new
(
raw
)
unless
klass
.
ancestors
.
include?
(
ActiveRecord
::
Base
)
# When the cached value is a persisted instance of ActiveRecord::Base in
# some cases a relation can return an empty collection becauses scope.none!
# is being applied on ActiveRecord::Associations::CollectionAssociation#scope
# when the new_record? method incorrectly returns false.
#
# See https://gitlab.com/gitlab-org/gitlab-ee/issues/9903#note_145329964
attributes
=
klass
.
attributes_builder
.
build_from_database
(
raw
,
{})
klass
.
allocate
.
init_with
(
"attributes"
=>
attributes
,
"new_record"
=>
new_record?
(
raw
,
klass
))
end
def
new_record?
(
raw
,
klass
)
raw
.
fetch
(
klass
.
primary_key
,
nil
).
blank?
end
def
valid_entry?
(
raw
,
klass
)
...
...
spec/lib/gitlab/json_cache_spec.rb
View file @
e96f2f24
...
...
@@ -297,13 +297,39 @@ describe Gitlab::JsonCache do
expect
(
result
).
to
eq
(
broadcast_message
)
end
context
'when the cached value is an instance of ActiveRecord::Base'
do
it
'returns a persisted record when id is set'
do
backend
.
write
(
expanded_key
,
broadcast_message
.
to_json
)
result
=
cache
.
fetch
(
key
,
as:
BroadcastMessage
)
{
'block result'
}
expect
(
result
).
to
be_persisted
end
it
'returns a new record when id is nil'
do
backend
.
write
(
expanded_key
,
build
(
:broadcast_message
).
to_json
)
result
=
cache
.
fetch
(
key
,
as:
BroadcastMessage
)
{
'block result'
}
expect
(
result
).
to
be_new_record
end
it
'returns a new record when id is missing'
do
backend
.
write
(
expanded_key
,
build
(
:broadcast_message
).
attributes
.
except
(
'id'
).
to_json
)
result
=
cache
.
fetch
(
key
,
as:
BroadcastMessage
)
{
'block result'
}
expect
(
result
).
to
be_new_record
end
end
it
"returns the result of the block when 'as' option is nil"
do
result
=
cache
.
fetch
(
key
,
as:
nil
)
{
'block result'
}
expect
(
result
).
to
eq
(
'block result'
)
end
it
"returns the result of the block when 'as' option is
not informed
"
do
it
"returns the result of the block when 'as' option is
missing
"
do
result
=
cache
.
fetch
(
key
)
{
'block result'
}
expect
(
result
).
to
eq
(
'block result'
)
...
...
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