Skip to content

Commit af855ad

Browse files
committed
Add download snippet screenshot link
1 parent 35f34ea commit af855ad

File tree

5 files changed

+76
-5
lines changed

5 files changed

+76
-5
lines changed

app/controllers/share/snippet_screenshots_controller.rb

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,15 @@ def new
1515
def create
1616
@snippet = Snippet.find(params[:snippet_id])
1717
@snippet.attach_screenshot_from_base64(params[:screenshot])
18-
redirect_to new_share_snippet_tweet_path(@snippet, auto: true), notice: "Screenshot successful".emojoy
18+
redirect_to intent_url, notice: "Screenshot successful".emojoy
19+
end
20+
21+
def intent_url
22+
case params[:intent]
23+
when "share"
24+
new_share_snippet_tweet_url(@snippet, auto: true)
25+
else
26+
share_snippet_url(@snippet)
27+
end
1928
end
2029
end

app/javascript/controllers/snippets/screenshot.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,13 @@ export default class extends Controller<HTMLFormElement> {
4040
const data = await this.drawScreenshot();
4141

4242
event.detail.fetchOptions.body.append('screenshot', data);
43+
44+
const searchParams = new URLSearchParams(window.location.search);
45+
['auto', 'intent'].forEach((key) => {
46+
if (searchParams.get(key)) {
47+
event.detail.fetchOptions.body.append(key, searchParams.get(key));
48+
}
49+
});
4350
}
4451

4552
event.detail.resume();

app/views/share/snippets/toolbar.rb

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,19 @@ def view_template
1212
flex_block do
1313
render Share::SnippetTweets::TweetButton.new(@snippet)
1414

15+
a(
16+
href: download_url,
17+
class: "button transparent"
18+
) { "Download" }
19+
1520
if @current_user&.can_edit?(@snippet)
16-
link_to "Edit this snippet", edit_share_snippet_path(@snippet),
21+
a(
22+
href: edit_share_snippet_path(@snippet),
1723
class: "button secondary",
1824
data: {turbo_frame: "snippet_form"}
25+
) do
26+
"Edit this snippet"
27+
end
1928
end
2029
end
2130
end
@@ -24,7 +33,15 @@ def share_url
2433
if @snippet.screenshot.attached?
2534
new_share_snippet_tweet_path(@snippet, auto: "true")
2635
else
27-
new_share_snippet_screenshot_path(@snippet, auto: "true")
36+
new_share_snippet_screenshot_path(@snippet, auto: "true", intent: "share")
37+
end
38+
end
39+
40+
def download_url
41+
if @snippet.screenshot.attached?
42+
rails_blob_url(@snippet.screenshot, disposition: "attachment")
43+
else
44+
new_share_snippet_screenshot_path(@snippet, auto: "true", intent: "download")
2845
end
2946
end
3047
end
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
require "rails_helper"
2+
3+
RSpec.describe "/snippets/:id/screenshot", type: :request do
4+
describe "GET show" do
5+
it "is successful" do
6+
get share_snippet_screenshot_path(FactoryBot.create(:snippet))
7+
8+
expect(response).to have_http_status(:success)
9+
end
10+
end
11+
12+
describe "GET new" do
13+
it "is successful" do
14+
get new_share_snippet_screenshot_path(FactoryBot.create(:snippet))
15+
16+
expect(response).to have_http_status(:success)
17+
end
18+
end
19+
20+
describe "POST create" do
21+
let(:png_data_uri) { "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAYAAAAfFcSJAAAACklEQVR4nGMAAQAABQABDQottAAAAABJRU5ErkJggg==" }
22+
23+
it "attaches screenshot and redirects to show" do
24+
post share_snippet_screenshot_path(FactoryBot.create(:snippet)), params: {screenshot: png_data_uri}
25+
26+
expect(response).to have_http_status(:found)
27+
expect(response).to redirect_to(share_snippet_path(Snippet.last))
28+
end
29+
30+
it "attaches screenshot and redirects to share" do
31+
post share_snippet_screenshot_path(FactoryBot.create(:snippet)), params: {intent: "share", screenshot: png_data_uri}
32+
33+
expect(response).to have_http_status(:found)
34+
expect(response).to redirect_to(new_share_snippet_tweet_url(Snippet.last, auto: true))
35+
end
36+
end
37+
end

spec/system/snippets_spec.rb

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@
5050

5151
click_link "Share"
5252

53-
expect(page).to have_content("Screenshot")
53+
expect(page).to have_content("Download")
5454
end
5555

5656
it "can share Snippet" do
@@ -63,6 +63,7 @@
6363
click_link "Share"
6464
end
6565

66-
expect(page).to have_content("Screenshot")
66+
expect(page).to have_content("Download")
67+
click_link "Download"
6768
end
6869
end

0 commit comments

Comments
 (0)