Skip to content

Commit 756e642

Browse files
authored
Merge pull request #203 from joyofrails/feat/code-screenshots
Support Snippet Screenshots and Twitter sharing
2 parents f145657 + 796c3bc commit 756e642

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

72 files changed

+1599
-462
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -72,3 +72,5 @@ config/litestream/*.yml
7272
spec/failures.txt
7373

7474
/coverage
75+
76+
.rspec

.rspec

Lines changed: 0 additions & 1 deletion
This file was deleted.

Gemfile

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ gem "postmark-rails" # Postmark Rails gem [https://github.com/ActiveCampaign/pos
2929
gem "scout_apm" # Scout APM Ruby Agent [https://scoutapm.com]
3030

3131
# Rendering
32-
# gem "image_processing" # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
32+
gem "image_processing", group: [:default, :wasm] # Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
3333
gem "inline_svg" # Embed SVGs in Rails views and style them with CSS [https://github.com/jamesmartin/inline_svg
3434
gem "rouge", group: [:default, :wasm] # Pure Ruby syntaix highlighter [https://github.com/rouge-ruby/rouge
3535
gem "sitepress-rails", group: [:default, :wasm] # Static site generator for Rails [https://sitepress.cc/getting-started/rails]
@@ -47,6 +47,7 @@ gem "honeybadger" # Error monitoring and uptime reporting [https://www.honeybadg
4747
gem "litestream" # Standalone streaming replication for SQLite [https://litestream.io]
4848
gem "web-push" # Web Push library for Ruby [https://github.com/pushpad/web-push]
4949
gem "fog-aws", require: false # Module for the 'fog' gem to support Amazon Web Services [https://github.com/fog/fog-aws]
50+
gem "aws-sdk-s3" # Official AWS Ruby gem for Amazon S3 [https://github.com/aws/aws-sdk-ruby]
5051

5152
# Admin
5253
gem "flipper-ui" # UI for the Flipper gem [https://www.flippercloud.io/docs/ui]

Gemfile.lock

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,22 @@ GEM
8383
addressable (2.8.7)
8484
public_suffix (>= 2.0.2, < 7.0)
8585
ast (2.4.2)
86+
aws-eventstream (1.3.0)
87+
aws-partitions (1.970.0)
88+
aws-sdk-core (3.202.2)
89+
aws-eventstream (~> 1, >= 1.3.0)
90+
aws-partitions (~> 1, >= 1.651.0)
91+
aws-sigv4 (~> 1.9)
92+
jmespath (~> 1, >= 1.6.1)
93+
aws-sdk-kms (1.88.0)
94+
aws-sdk-core (~> 3, >= 3.201.0)
95+
aws-sigv4 (~> 1.5)
96+
aws-sdk-s3 (1.159.0)
97+
aws-sdk-core (~> 3, >= 3.201.0)
98+
aws-sdk-kms (~> 1)
99+
aws-sigv4 (~> 1.5)
100+
aws-sigv4 (1.9.1)
101+
aws-eventstream (~> 1, >= 1.0.2)
86102
base64 (0.2.0)
87103
bcrypt (3.1.20)
88104
better_html (2.1.1)
@@ -191,6 +207,8 @@ GEM
191207
concurrent-ruby (~> 1.1)
192208
webrick (~> 1.7)
193209
websocket-driver (~> 0.7)
210+
ffi (1.17.0-arm64-darwin)
211+
ffi (1.17.0-x86_64-linux-gnu)
194212
flipper (1.3.0)
195213
concurrent-ruby (< 2)
196214
flipper-active_record (1.3.0)
@@ -231,6 +249,9 @@ GEM
231249
http-2 (>= 1.0.0)
232250
i18n (1.14.5)
233251
concurrent-ruby (~> 1.0)
252+
image_processing (1.13.0)
253+
mini_magick (>= 4.9.5, < 5)
254+
ruby-vips (>= 2.0.17, < 3)
234255
importmap-rails (2.0.1)
235256
actionpack (>= 6.0.0)
236257
activesupport (>= 6.0.0)
@@ -244,6 +265,7 @@ GEM
244265
irb (1.14.0)
245266
rdoc (>= 4.0.0)
246267
reline (>= 0.4.2)
268+
jmespath (1.6.2)
247269
js (2.6.2)
248270
json (2.7.2)
249271
jwt (2.8.2)
@@ -262,6 +284,7 @@ GEM
262284
logfmt (>= 0.0.10)
263285
sqlite3
264286
logfmt (0.0.10)
287+
logger (1.6.0)
265288
loofah (2.22.0)
266289
crass (~> 1.0.2)
267290
nokogiri (>= 1.12.0)
@@ -277,6 +300,7 @@ GEM
277300
mime-types (3.5.2)
278301
mime-types-data (~> 3.2015)
279302
mime-types-data (3.2024.0702)
303+
mini_magick (4.13.2)
280304
mini_mime (1.1.5)
281305
mini_portile2 (2.8.7)
282306
minitest (5.24.1)
@@ -430,6 +454,9 @@ GEM
430454
rubocop (>= 1.48.1, < 2.0)
431455
rubocop-ast (>= 1.31.1, < 2.0)
432456
ruby-progressbar (1.13.0)
457+
ruby-vips (2.2.2)
458+
ffi (~> 1.12)
459+
logger
433460
ruby_wasm (2.6.2-arm64-darwin)
434461
ruby_wasm (2.6.2-x86_64-linux)
435462
sanitize (6.1.1)
@@ -547,6 +574,7 @@ PLATFORMS
547574
DEPENDENCIES
548575
activerecord-enhancedsqlite3-adapter
549576
activerecord-nulldb-adapter
577+
aws-sdk-s3
550578
bcrypt (~> 3.1.7)
551579
bootsnap
552580
brakeman
@@ -568,6 +596,7 @@ DEPENDENCIES
568596
fog-aws
569597
honeybadger
570598
httpx
599+
image_processing
571600
inline_svg
572601
invisible_captcha
573602
js

app/content/layouts/article.html.erb

Lines changed: 0 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,13 @@
22
title: current_page.data.title!,
33
description: current_page.data.description,
44
keywords: %w[rails hotwire],
5-
index: true,
6-
icon: "/favicon.ico",
75
og: {
8-
title: :title,
9-
description: :description,
106
type: "article",
117
url: current_page.url,
12-
site_name: "Joy of Rails",
13-
locale: "en_US",
148
image: current_page.data.meta_image && asset_url(current_page.data.meta_image)
159
},
1610
twitter: {
1711
card: current_page.data.meta_image ? "summary_large_image" : "summary",
18-
site: "@joyofrails",
1912
}
2013
) %>
2114
<%= render "application/skip_to_content" %>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
class Share::SnippetScreenshotsController < ApplicationController
2+
using Refinements::Emojoy
3+
4+
def show
5+
@snippet = Snippet.find(params[:snippet_id])
6+
7+
render layout: false
8+
end
9+
10+
def new
11+
@snippet = Snippet.find(params[:snippet_id])
12+
end
13+
14+
def create
15+
@snippet = Snippet.find(params[:snippet_id])
16+
@snippet.attach_screenshot_from_base64(params[:screenshot])
17+
redirect_to new_share_snippet_tweet_path(@snippet, auto: true), notice: "Screenshot attached".emojoy
18+
end
19+
end
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
class Share::SnippetTweetsController < ApplicationController
2+
using Refinements::Emojoy
3+
4+
def new
5+
@snippet = Snippet.find(params[:snippet_id])
6+
@auto = params[:auto] == "true"
7+
end
8+
end
Lines changed: 87 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,87 @@
1+
class Share::SnippetsController < ApplicationController
2+
using Refinements::Emojoy
3+
4+
before_action :feature_enabled!, except: %i[index show]
5+
before_action :authenticate_user!, only: %i[new create edit update destroy]
6+
7+
# GET /snippets
8+
def index
9+
@snippets = Snippet.all.with_attached_screenshot.includes(:author).order(created_at: :desc)
10+
end
11+
12+
# GET /snippets/1
13+
def show
14+
@snippet = Snippet.find(params[:id])
15+
16+
fresh_when(@snippet, public: true) unless user_signed_in?
17+
end
18+
19+
# GET /snippets/new
20+
def new
21+
default_params = {
22+
filename: "app/models/user.rb",
23+
source: "class User < ApplicationRecord\n\s\shas_many :posts\nend",
24+
language: "ruby"
25+
}
26+
@snippet = current_user.snippets.new(snippet_params.presence || default_params)
27+
end
28+
29+
# GET /snippets/1/edit
30+
def edit
31+
@snippet = current_user.snippets.find(params[:id])
32+
@snippet.assign_attributes(snippet_params)
33+
end
34+
35+
# POST /snippets
36+
def create
37+
@snippet = current_user.snippets.new(snippet_params)
38+
39+
if @snippet.save
40+
redirect_to share_snippet_redirect_url(@snippet), notice: "Your snippet has been saved".emojoy, status: :see_other
41+
else
42+
render :new, status: :unprocessable_entity
43+
end
44+
end
45+
46+
# PATCH/PUT /snippets/1
47+
def update
48+
@snippet = current_user.snippets.find(params[:id])
49+
if @snippet.update(snippet_params)
50+
@snippet.attach_screenshot_from_base64(params[:screenshot]) if params[:screenshot]
51+
52+
redirect_to share_snippet_redirect_url(@snippet), notice: "Your snippet has been saved".emojoy, status: :see_other
53+
else
54+
render :edit, status: :unprocessable_entity
55+
end
56+
end
57+
58+
# DELETE /snippets/1
59+
def destroy
60+
@snippet = current_user.snippets.find(params[:id])
61+
@snippet.destroy!
62+
redirect_to share_snippets_url, notice: "Your snippet has been deleted permanently".emojoy, status: :see_other
63+
end
64+
65+
private
66+
67+
def share_snippet_redirect_url(snippet)
68+
case params[:commit]
69+
when "Share"
70+
new_share_snippet_screenshot_url(snippet)
71+
else
72+
edit_share_snippet_url(snippet)
73+
end
74+
end
75+
76+
# Only allow a list of trusted parameters through.
77+
def snippet_params
78+
params.fetch(:snippet, {}).permit(:filename, :source, :language)
79+
end
80+
81+
def feature_enabled!
82+
return if user_signed_in? &&
83+
Flipper.enabled?(:snippets, current_user)
84+
85+
raise ActionController::RoutingError.new("Not Found")
86+
end
87+
end

app/controllers/snippets_controller.rb

Lines changed: 0 additions & 66 deletions
This file was deleted.

app/helpers/application_helper.rb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,19 @@ def seo_meta_tags
66
{rel: "apple-touch-icon", href: "/apple-touch-icon.png?v1.0"},
77
{rel: "manifest", href: "/manifest.webmanifest?v1.0"}
88
]
9+
set_meta_tags index: true
10+
set_meta_tags icon: "/favicon.ico"
11+
set_meta_tags og: {
12+
title: :title,
13+
description: :description,
14+
type: "article",
15+
url: url_for,
16+
site_name: "Joy of Rails",
17+
locale: "en_US"
18+
}
19+
set_meta_tags twitter: {
20+
site: "@joyofrails"
21+
}
922
display_meta_tags site: "Joy of Rails"
1023
end
1124
end

0 commit comments

Comments
 (0)