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
1c9ecae7
Commit
1c9ecae7
authored
Mar 20, 2019
by
GitLab Bot
Browse files
Options
Browse Files
Download
Plain Diff
Automatic merge of gitlab-org/gitlab-ce master
parents
e486e6e9
f7fcfc77
Changes
5
Show whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
58 additions
and
16 deletions
+58
-16
app/models/broadcast_message.rb
app/models/broadcast_message.rb
+1
-1
app/models/concerns/cache_markdown_field.rb
app/models/concerns/cache_markdown_field.rb
+18
-4
spec/lib/gitlab/json_cache_spec.rb
spec/lib/gitlab/json_cache_spec.rb
+23
-7
spec/models/broadcast_message_spec.rb
spec/models/broadcast_message_spec.rb
+6
-0
spec/models/concerns/cache_markdown_field_spec.rb
spec/models/concerns/cache_markdown_field_spec.rb
+10
-4
No files found.
app/models/broadcast_message.rb
View file @
1c9ecae7
...
...
@@ -4,7 +4,7 @@ class BroadcastMessage < ActiveRecord::Base
include
CacheMarkdownField
include
Sortable
cache_markdown_field
:message
,
pipeline: :broadcast_message
cache_markdown_field
:message
,
pipeline: :broadcast_message
,
whitelisted:
true
validates
:message
,
presence:
true
validates
:starts_at
,
presence:
true
...
...
app/models/concerns/cache_markdown_field.rb
View file @
1c9ecae7
...
...
@@ -7,6 +7,7 @@
# cache_markdown_field :foo
# cache_markdown_field :bar
# cache_markdown_field :baz, pipeline: :single_line
# cache_markdown_field :baz, whitelisted: true
#
# Corresponding foo_html, bar_html and baz_html fields should exist.
module
CacheMarkdownField
...
...
@@ -37,7 +38,15 @@ module CacheMarkdownField
end
def
html_fields
markdown_fields
.
map
{
|
field
|
html_field
(
field
)
}
markdown_fields
.
map
{
|
field
|
html_field
(
field
)
}
end
def
html_fields_whitelisted
markdown_fields
.
each_with_object
([])
do
|
field
,
fields
|
if
@data
[
field
].
fetch
(
:whitelisted
,
false
)
fields
<<
html_field
(
field
)
end
end
end
end
...
...
@@ -149,13 +158,18 @@ module CacheMarkdownField
alias_method
:attributes_before_markdown_cache
,
:attributes
def
attributes
attrs
=
attributes_before_markdown_cache
html_fields
=
cached_markdown_fields
.
html_fields
whitelisted
=
cached_markdown_fields
.
html_fields_whitelisted
exclude_fields
=
html_fields
-
whitelisted
attrs
.
delete
(
'cached_markdown_version'
)
cached_markdown_fields
.
html_fields
.
each
do
|
field
|
exclude_fields
.
each
do
|
field
|
attrs
.
delete
(
field
)
end
if
whitelisted
.
empty?
attrs
.
delete
(
'cached_markdown_version'
)
end
attrs
end
...
...
spec/lib/gitlab/json_cache_spec.rb
View file @
1c9ecae7
...
...
@@ -146,6 +146,18 @@ describe Gitlab::JsonCache do
expect
(
cache
.
read
(
key
,
BroadcastMessage
)).
to
be_nil
end
it
'gracefully handles excluded fields from attributes during serialization'
do
allow
(
backend
).
to
receive
(
:read
)
.
with
(
expanded_key
)
.
and_return
(
broadcast_message
.
attributes
.
except
(
"message_html"
).
to_json
)
result
=
cache
.
read
(
key
,
BroadcastMessage
)
BroadcastMessage
.
cached_markdown_fields
.
html_fields
.
each
do
|
field
|
expect
(
result
.
public_send
(
field
)).
to
be_nil
end
end
end
context
'when the cached value is an array'
do
...
...
@@ -327,7 +339,9 @@ describe Gitlab::JsonCache do
.
with
(
expanded_key
)
.
and_return
(
'{'
)
expect
(
cache
.
read
(
key
,
BroadcastMessage
)).
to
be_nil
result
=
cache
.
fetch
(
key
,
as:
BroadcastMessage
)
{
'block result'
}
expect
(
result
).
to
eq
'block result'
end
it
'gracefully handles an empty hash'
do
...
...
@@ -335,7 +349,7 @@ describe Gitlab::JsonCache do
.
with
(
expanded_key
)
.
and_return
(
'{}'
)
expect
(
cache
.
read
(
key
,
BroadcastMessage
)).
to
be_a
(
BroadcastMessage
)
expect
(
cache
.
fetch
(
key
,
as:
BroadcastMessage
)).
to
be_a
(
BroadcastMessage
)
end
it
'gracefully handles unknown attributes'
do
...
...
@@ -343,17 +357,19 @@ describe Gitlab::JsonCache do
.
with
(
expanded_key
)
.
and_return
(
broadcast_message
.
attributes
.
merge
(
unknown_attribute:
1
).
to_json
)
expect
(
cache
.
read
(
key
,
BroadcastMessage
)).
to
be_nil
result
=
cache
.
fetch
(
key
,
as:
BroadcastMessage
)
{
'block result'
}
expect
(
result
).
to
eq
'block result'
end
it
'gracefully handles excluded fields from attributes during serialization'
do
backend
.
write
(
expanded_key
,
broadcast_message
.
to_json
)
allow
(
backend
).
to
receive
(
:read
)
.
with
(
expanded_key
)
.
and_return
(
broadcast_message
.
attributes
.
except
(
"message_html"
).
to_json
)
result
=
cache
.
fetch
(
key
,
as:
BroadcastMessage
)
{
'block result'
}
excluded_fields
=
BroadcastMessage
.
cached_markdown_fields
.
html_fields
(
excluded_fields
+
[
'cached_markdown_version'
]).
each
do
|
field
|
BroadcastMessage
.
cached_markdown_fields
.
html_fields
.
each
do
|
field
|
expect
(
result
.
public_send
(
field
)).
to
be_nil
end
end
...
...
spec/models/broadcast_message_spec.rb
View file @
1c9ecae7
...
...
@@ -95,6 +95,12 @@ describe BroadcastMessage do
end
end
describe
'#attributes'
do
it
'includes message_html field'
do
expect
(
subject
.
attributes
.
keys
).
to
include
(
"cached_markdown_version"
,
"message_html"
)
end
end
describe
'#active?'
do
it
'is truthy when started and not ended'
do
message
=
build
(
:broadcast_message
)
...
...
spec/models/concerns/cache_markdown_field_spec.rb
View file @
1c9ecae7
...
...
@@ -23,6 +23,7 @@ describe CacheMarkdownField do
include
CacheMarkdownField
cache_markdown_field
:foo
cache_markdown_field
:baz
,
pipeline: :single_line
cache_markdown_field
:zoo
,
whitelisted:
true
def
self
.
add_attr
(
name
)
self
.
attribute_names
+=
[
name
]
...
...
@@ -35,7 +36,7 @@ describe CacheMarkdownField do
add_attr
:cached_markdown_version
[
:foo
,
:foo_html
,
:bar
,
:baz
,
:baz_html
].
each
do
|
name
|
[
:foo
,
:foo_html
,
:bar
,
:baz
,
:baz_html
,
:zoo
,
:zoo_html
].
each
do
|
name
|
add_attr
(
name
)
end
...
...
@@ -84,8 +85,8 @@ describe CacheMarkdownField do
end
describe
'.attributes'
do
it
'excludes cache attributes'
do
expect
(
thing
.
attributes
.
keys
.
sort
).
to
eq
(
%w[bar baz
foo
]
)
it
'excludes cache attributes
that is blacklisted by default
'
do
expect
(
thing
.
attributes
.
keys
.
sort
).
to
eq
(
%w[bar baz
cached_markdown_version foo zoo zoo_html
]
)
end
end
...
...
@@ -297,7 +298,12 @@ describe CacheMarkdownField do
it
'saves the changes using #update_columns'
do
expect
(
thing
).
to
receive
(
:persisted?
).
and_return
(
true
)
expect
(
thing
).
to
receive
(
:update_columns
)
.
with
(
"foo_html"
=>
updated_html
,
"baz_html"
=>
""
,
"cached_markdown_version"
=>
cache_version
)
.
with
(
"foo_html"
=>
updated_html
,
"baz_html"
=>
""
,
"zoo_html"
=>
""
,
"cached_markdown_version"
=>
cache_version
)
thing
.
refresh_markdown_cache!
end
...
...
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