BigW Consortium Gitlab
Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
G
gitlab-ce
Project
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
Registry
Registry
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Commits
Issue Boards
Open sidebar
Forest Godfrey
gitlab-ce
Commits
2b3fc5e6
Commit
2b3fc5e6
authored
Apr 30, 2017
by
Douwe Maan
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add download button to project snippets
parent
54040ce0
Hide whitespace changes
Inline
Side-by-side
Showing
11 changed files
with
129 additions
and
115 deletions
+129
-115
snippets_actions.rb
app/controllers/concerns/snippets_actions.rb
+3
-1
snippets_controller.rb
app/controllers/snippets_controller.rb
+3
-11
blob_helper.rb
app/helpers/blob_helper.rb
+4
-4
_blob.html.haml
app/views/shared/snippets/_blob.html.haml
+1
-2
show.html.haml
app/views/snippets/show.html.haml
+1
-1
dm-snippet-download-button.yml
changelogs/unreleased/dm-snippet-download-button.yml
+4
-0
project.rb
config/routes/project.rb
+1
-1
snippets.rb
config/routes/snippets.rb
+1
-2
snippets_controller_spec.rb
spec/controllers/snippets_controller_spec.rb
+87
-93
show_spec.rb
spec/features/projects/snippets/show_spec.rb
+12
-0
show_spec.rb
spec/features/snippets/show_spec.rb
+12
-0
No files found.
app/controllers/concerns/snippets_actions.rb
View file @
2b3fc5e6
...
...
@@ -5,10 +5,12 @@ module SnippetsActions
end
def
raw
disposition
=
params
[
:inline
]
==
'false'
?
'attachment'
:
'inline'
send_data
(
convert_line_endings
(
@snippet
.
content
),
type:
'text/plain; charset=utf-8'
,
disposition:
'inline'
,
disposition:
disposition
,
filename:
@snippet
.
sanitized_file_name
)
end
...
...
app/controllers/snippets_controller.rb
View file @
2b3fc5e6
...
...
@@ -5,10 +5,10 @@ class SnippetsController < ApplicationController
include
MarkdownPreview
include
RendersBlob
before_action
:snippet
,
only:
[
:show
,
:edit
,
:destroy
,
:update
,
:raw
,
:download
]
before_action
:snippet
,
only:
[
:show
,
:edit
,
:destroy
,
:update
,
:raw
]
# Allow read snippet
before_action
:authorize_read_snippet!
,
only:
[
:show
,
:raw
,
:download
]
before_action
:authorize_read_snippet!
,
only:
[
:show
,
:raw
]
# Allow modify snippet
before_action
:authorize_update_snippet!
,
only:
[
:edit
,
:update
]
...
...
@@ -16,7 +16,7 @@ class SnippetsController < ApplicationController
# Allow destroy snippet
before_action
:authorize_admin_snippet!
,
only:
[
:destroy
]
skip_before_action
:authenticate_user!
,
only:
[
:index
,
:show
,
:raw
,
:download
]
skip_before_action
:authenticate_user!
,
only:
[
:index
,
:show
,
:raw
]
layout
'snippets'
respond_to
:html
...
...
@@ -83,14 +83,6 @@ class SnippetsController < ApplicationController
redirect_to
snippets_path
end
def
download
send_data
(
convert_line_endings
(
@snippet
.
content
),
type:
'text/plain; charset=utf-8'
,
filename:
@snippet
.
sanitized_file_name
)
end
def
preview_markdown
render_markdown_preview
(
params
[
:text
],
skip_project_check:
true
)
end
...
...
app/helpers/blob_helper.rb
View file @
2b3fc5e6
...
...
@@ -118,15 +118,15 @@ module BlobHelper
icon
(
"
#{
file_type_icon_class
(
'file'
,
mode
,
name
)
}
fw"
)
end
def
blob_raw_url
def
blob_raw_url
(
params
=
{})
if
@snippet
if
@snippet
.
project_id
raw_namespace_project_snippet_path
(
@project
.
namespace
,
@project
,
@snippet
)
raw_namespace_project_snippet_path
(
@project
.
namespace
,
@project
,
@snippet
,
params
)
else
raw_snippet_path
(
@snippet
)
raw_snippet_path
(
@snippet
,
params
)
end
elsif
@blob
namespace_project_raw_path
(
@project
.
namespace
,
@project
,
@id
)
namespace_project_raw_path
(
@project
.
namespace
,
@project
,
@id
,
params
)
end
end
...
...
app/views/shared/snippets/_blob.html.haml
View file @
2b3fc5e6
...
...
@@ -18,7 +18,6 @@
=
copy_blob_source_button
(
blob
)
=
open_raw_blob_button
(
blob
)
-
if
defined?
(
download_path
)
&&
download_path
=
link_to
icon
(
'download'
),
download_path
,
class:
"btn btn-sm has-tooltip"
,
title:
'Download'
,
data:
{
container:
'body'
}
=
link_to
icon
(
'download'
),
blob_raw_url
(
inline:
false
),
target:
'_blank'
,
class:
"btn btn-sm has-tooltip"
,
title:
'Download'
,
data:
{
container:
'body'
}
=
render
'projects/blob/content'
,
blob:
blob
app/views/snippets/show.html.haml
View file @
2b3fc5e6
...
...
@@ -3,7 +3,7 @@
=
render
'shared/snippets/header'
%article
.file-holder.snippet-file-content
=
render
'shared/snippets/blob'
,
download_path:
download_snippet_path
(
@snippet
)
=
render
'shared/snippets/blob'
.row-content-block.top-block.content-component-block
=
render
'award_emoji/awards_block'
,
awardable:
@snippet
,
inline:
true
changelogs/unreleased/dm-snippet-download-button.yml
0 → 100644
View file @
2b3fc5e6
---
title
:
Add download button to project snippets
merge_request
:
author
:
config/routes/project.rb
View file @
2b3fc5e6
...
...
@@ -44,7 +44,7 @@ constraints(ProjectUrlConstrainer.new) do
resources
:snippets
,
concerns: :awardable
,
constraints:
{
id:
/\d+/
}
do
member
do
get
'raw'
get
:raw
post
:mark_as_spam
end
end
...
...
config/routes/snippets.rb
View file @
2b3fc5e6
resources
:snippets
,
concerns: :awardable
do
member
do
get
'raw'
get
'download'
get
:raw
post
:mark_as_spam
post
:preview_markdown
end
...
...
spec/controllers/snippets_controller_spec.rb
View file @
2b3fc5e6
...
...
@@ -350,144 +350,138 @@ describe SnippetsController do
end
end
%w(raw download)
.
each
do
|
action
|
describe
"GET
#{
action
}
"
do
context
'when the personal snippet is private'
do
let
(
:personal_snippet
)
{
create
(
:personal_snippet
,
:private
,
author:
user
)
}
describe
"GET #raw"
do
context
'when the personal snippet is private'
do
let
(
:personal_snippet
)
{
create
(
:personal_snippet
,
:private
,
author:
user
)
}
context
'when signed in'
do
before
do
sign_in
(
user
)
end
context
'when signed in'
do
before
do
sign_in
(
user
)
end
context
'when signed in user is not the author'
do
let
(
:other_author
)
{
create
(
:author
)
}
let
(
:other_personal_snippet
)
{
create
(
:personal_snippet
,
:private
,
author:
other_author
)
}
context
'when signed in user is not the author'
do
let
(
:other_author
)
{
create
(
:author
)
}
let
(
:other_personal_snippet
)
{
create
(
:personal_snippet
,
:private
,
author:
other_author
)
}
it
'responds with status 404'
do
get
action
,
id:
other_personal_snippet
.
to_param
it
'responds with status 404'
do
get
:raw
,
id:
other_personal_snippet
.
to_param
expect
(
response
).
to
have_http_status
(
404
)
end
expect
(
response
).
to
have_http_status
(
404
)
end
end
context
'when signed in user is the author'
do
before
{
get
action
,
id:
personal_snippet
.
to_param
}
context
'when signed in user is the author'
do
before
{
get
:raw
,
id:
personal_snippet
.
to_param
}
it
'responds with status 200'
do
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
it
'responds with status 200'
do
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
it
'has expected headers'
do
expect
(
response
.
header
[
'Content-Type'
]).
to
eq
(
'text/plain; charset=utf-8'
)
it
'has expected headers'
do
expect
(
response
.
header
[
'Content-Type'
]).
to
eq
(
'text/plain; charset=utf-8'
)
if
action
==
:download
expect
(
response
.
header
[
'Content-Disposition'
]).
to
match
(
/attachment/
)
elsif
action
==
:raw
expect
(
response
.
header
[
'Content-Disposition'
]).
to
match
(
/inline/
)
end
end
expect
(
response
.
header
[
'Content-Disposition'
]).
to
match
(
/inline/
)
end
end
end
context
'when not signed in'
do
it
'redirects to the sign in page'
do
get
action
,
id:
personal_snippet
.
to_param
context
'when not signed in'
do
it
'redirects to the sign in page'
do
get
:raw
,
id:
personal_snippet
.
to_param
expect
(
response
).
to
redirect_to
(
new_user_session_path
)
end
expect
(
response
).
to
redirect_to
(
new_user_session_path
)
end
end
end
context
'when the personal snippet is internal'
do
let
(
:personal_snippet
)
{
create
(
:personal_snippet
,
:internal
,
author:
user
)
}
context
'when the personal snippet is internal'
do
let
(
:personal_snippet
)
{
create
(
:personal_snippet
,
:internal
,
author:
user
)
}
context
'when signed in'
do
before
do
sign_in
(
user
)
end
context
'when signed in'
do
before
do
sign_in
(
user
)
end
it
'responds with status 200'
do
get
action
,
id:
personal_snippet
.
to_param
it
'responds with status 200'
do
get
:raw
,
id:
personal_snippet
.
to_param
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
end
context
'when not signed in'
do
it
'redirects to the sign in page'
do
get
action
,
id:
personal_snippet
.
to_param
context
'when not signed in'
do
it
'redirects to the sign in page'
do
get
:raw
,
id:
personal_snippet
.
to_param
expect
(
response
).
to
redirect_to
(
new_user_session_path
)
end
expect
(
response
).
to
redirect_to
(
new_user_session_path
)
end
end
end
context
'when the personal snippet is public'
do
let
(
:personal_snippet
)
{
create
(
:personal_snippet
,
:public
,
author:
user
)
}
context
'when the personal snippet is public'
do
let
(
:personal_snippet
)
{
create
(
:personal_snippet
,
:public
,
author:
user
)
}
context
'when signed in'
do
before
do
sign_in
(
user
)
end
context
'when signed in'
do
before
do
sign_in
(
user
)
end
it
'responds with status 200'
do
get
action
,
id:
personal_snippet
.
to_param
it
'responds with status 200'
do
get
:raw
,
id:
personal_snippet
.
to_param
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
context
'CRLF line ending'
do
let
(
:personal_snippet
)
do
create
(
:personal_snippet
,
:public
,
author:
user
,
content:
"first line
\r\n
second line
\r\n
third line"
)
end
context
'CRLF line ending'
do
let
(
:personal_snippet
)
do
create
(
:personal_snippet
,
:public
,
author:
user
,
content:
"first line
\r\n
second line
\r\n
third line"
)
end
it
'returns LF line endings by default'
do
get
action
,
id:
personal_snippet
.
to_param
it
'returns LF line endings by default'
do
get
:raw
,
id:
personal_snippet
.
to_param
expect
(
response
.
body
).
to
eq
(
"first line
\n
second line
\n
third line"
)
end
expect
(
response
.
body
).
to
eq
(
"first line
\n
second line
\n
third line"
)
end
it
'does not convert line endings when parameter present'
do
get
action
,
id:
personal_snippet
.
to_param
,
line_ending: :raw
it
'does not convert line endings when parameter present'
do
get
:raw
,
id:
personal_snippet
.
to_param
,
line_ending: :raw
expect
(
response
.
body
).
to
eq
(
"first line
\r\n
second line
\r\n
third line"
)
end
expect
(
response
.
body
).
to
eq
(
"first line
\r\n
second line
\r\n
third line"
)
end
end
end
context
'when not signed in'
do
it
'responds with status 200'
do
get
action
,
id:
personal_snippet
.
to_param
context
'when not signed in'
do
it
'responds with status 200'
do
get
:raw
,
id:
personal_snippet
.
to_param
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
expect
(
assigns
(
:snippet
)).
to
eq
(
personal_snippet
)
expect
(
response
).
to
have_http_status
(
200
)
end
end
end
context
'when the personal snippet does not exist'
do
context
'when signed in'
do
before
do
sign_in
(
user
)
end
context
'when the personal snippet does not exist'
do
context
'when signed in'
do
before
do
sign_in
(
user
)
end
it
'responds with status 404'
do
get
action
,
id:
'doesntexist'
it
'responds with status 404'
do
get
:raw
,
id:
'doesntexist'
expect
(
response
).
to
have_http_status
(
404
)
end
expect
(
response
).
to
have_http_status
(
404
)
end
end
context
'when not signed in'
do
it
'responds with status 404'
do
get
action
,
id:
'doesntexist'
context
'when not signed in'
do
it
'responds with status 404'
do
get
:raw
,
id:
'doesntexist'
expect
(
response
).
to
have_http_status
(
404
)
end
expect
(
response
).
to
have_http_status
(
404
)
end
end
end
...
...
spec/features/projects/snippets/show_spec.rb
View file @
2b3fc5e6
...
...
@@ -30,6 +30,12 @@ feature 'Project snippet', :js, feature: true do
# shows an enabled copy button
expect
(
page
).
to
have_selector
(
'.js-copy-blob-source-btn:not(.disabled)'
)
# shows a raw button
expect
(
page
).
to
have_link
(
'Open raw'
)
# shows a download button
expect
(
page
).
to
have_link
(
'Download'
)
end
end
end
...
...
@@ -59,6 +65,12 @@ feature 'Project snippet', :js, feature: true do
# shows a disabled copy button
expect
(
page
).
to
have_selector
(
'.js-copy-blob-source-btn.disabled'
)
# shows a raw button
expect
(
page
).
to
have_link
(
'Open raw'
)
# shows a download button
expect
(
page
).
to
have_link
(
'Download'
)
end
end
...
...
spec/features/snippets/show_spec.rb
View file @
2b3fc5e6
...
...
@@ -24,6 +24,12 @@ feature 'Snippet', :js, feature: true do
# shows an enabled copy button
expect
(
page
).
to
have_selector
(
'.js-copy-blob-source-btn:not(.disabled)'
)
# shows a raw button
expect
(
page
).
to
have_link
(
'Open raw'
)
# shows a download button
expect
(
page
).
to
have_link
(
'Download'
)
end
end
end
...
...
@@ -53,6 +59,12 @@ feature 'Snippet', :js, feature: true do
# shows a disabled copy button
expect
(
page
).
to
have_selector
(
'.js-copy-blob-source-btn.disabled'
)
# shows a raw button
expect
(
page
).
to
have_link
(
'Open raw'
)
# shows a download button
expect
(
page
).
to
have_link
(
'Download'
)
end
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