summaryrefslogtreecommitdiff
path: root/app/helpers/snippets_helper.rb
diff options
context:
space:
mode:
Diffstat (limited to 'app/helpers/snippets_helper.rb')
-rw-r--r--app/helpers/snippets_helper.rb71
1 files changed, 59 insertions, 12 deletions
diff --git a/app/helpers/snippets_helper.rb b/app/helpers/snippets_helper.rb
index 6ccc1fb2ed1..0a77741c457 100644
--- a/app/helpers/snippets_helper.rb
+++ b/app/helpers/snippets_helper.rb
@@ -11,22 +11,49 @@ module SnippetsHelper
end
end
- def reliable_snippet_path(snippet, opts = nil)
- if snippet.project_id?
- project_snippet_path(snippet.project, snippet, opts)
- else
- snippet_path(snippet, opts)
+ def reliable_snippet_path(snippet, opts = {})
+ reliable_snippet_url(snippet, opts, only_path: true)
+ end
+
+ def reliable_raw_snippet_path(snippet, opts = {})
+ reliable_raw_snippet_url(snippet, opts, only_path: true)
+ end
+
+ def reliable_snippet_url(snippet, opts = {}, only_path: false)
+ reliable_snippet_helper(snippet, opts) do |updated_opts|
+ if snippet.project_id?
+ project_snippet_url(snippet.project, snippet, nil, updated_opts.merge({ only_path: only_path }))
+ else
+ snippet_url(snippet, nil, updated_opts.merge({ only_path: only_path }))
+ end
end
end
- def download_snippet_path(snippet)
- if snippet.project_id
- raw_project_snippet_path(@project, snippet, inline: false)
- else
- raw_snippet_path(snippet, inline: false)
+ def reliable_raw_snippet_url(snippet, opts = {}, only_path: false)
+ reliable_snippet_helper(snippet, opts) do |updated_opts|
+ if snippet.project_id?
+ raw_project_snippet_url(snippet.project, snippet, nil, updated_opts.merge({ only_path: only_path }))
+ else
+ raw_snippet_url(snippet, nil, updated_opts.merge({ only_path: only_path }))
+ end
end
end
+ def reliable_snippet_helper(snippet, opts)
+ opts[:secret] = snippet.secret if snippet.secret?
+
+ yield(opts)
+ end
+
+ def download_raw_snippet_button(snippet)
+ link_to(icon('download'), reliable_raw_snippet_path(snippet, inline: false), target: '_blank', rel: 'noopener noreferrer', class: "btn btn-sm has-tooltip", title: 'Download', data: { container: 'body' })
+ end
+
+ def shareable_snippets_link(snippet)
+ url = reliable_snippet_url(snippet)
+ link_to(url, url, id: 'shareable_link_url', title: 'Open')
+ end
+
# Return the path of a snippets index for a user or for a project
#
# @returns String, path to snippet index
@@ -114,8 +141,28 @@ module SnippetsHelper
{ snippet_object: snippet, snippet_chunks: snippet_chunks }
end
- def snippet_embed
- "<script src=\"#{url_for(only_path: false, overwrite_params: nil)}.js\"></script>"
+ def snippet_embed(snippet)
+ content_tag(:script, nil, src: reliable_snippet_url(snippet))
+ end
+
+ def snippet_badge(snippet)
+ attrs = snippet_badge_attributes(snippet)
+ if attrs
+ css_class, text = attrs
+ tag.span(class: ['badge', 'badge-gray']) do
+ concat(tag.i(class: ['fa', css_class]))
+ concat(' ')
+ concat(_(text))
+ end
+ end
+ end
+
+ def snippet_badge_attributes(snippet)
+ if snippet.private?
+ ['fa-lock', 'private']
+ elsif snippet.secret?
+ ['fa-user-secret', 'secret']
+ end
end
def embedded_snippet_raw_button