Skip to content

Commit 7613700

Browse files
committed
feat (project): add validations for banner and preprocess variants
1 parent 23ae3fa commit 7613700

File tree

3 files changed

+53
-2
lines changed

3 files changed

+53
-2
lines changed

Gemfile

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ gem "kamal", require: false
3838
gem "thruster", require: false
3939

4040
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
41-
# gem "image_processing", "~> 1.2"
41+
gem "image_processing", "~> 1.2"
4242

4343
group :development, :test do
4444
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
@@ -86,3 +86,5 @@ gem "slack-ruby-client"
8686
gem "tailwindcss-ruby", "~> 4.1"
8787

8888
gem "tailwindcss-rails", "~> 4.3"
89+
90+
gem "active_storage_validations"

Gemfile.lock

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,12 @@ GEM
4646
erubi (~> 1.11)
4747
rails-dom-testing (~> 2.2)
4848
rails-html-sanitizer (~> 1.6)
49+
active_storage_validations (3.0.2)
50+
activejob (>= 6.1.4)
51+
activemodel (>= 6.1.4)
52+
activestorage (>= 6.1.4)
53+
activesupport (>= 6.1.4)
54+
marcel (>= 1.0.3)
4955
activejob (8.0.3)
5056
activesupport (= 8.0.3)
5157
globalid (>= 0.3.6)
@@ -153,6 +159,13 @@ GEM
153159
multipart-post (~> 2.0)
154160
faraday-net_http (3.4.1)
155161
net-http (>= 0.5.0)
162+
ffi (1.17.2-aarch64-linux-gnu)
163+
ffi (1.17.2-aarch64-linux-musl)
164+
ffi (1.17.2-arm-linux-gnu)
165+
ffi (1.17.2-arm-linux-musl)
166+
ffi (1.17.2-arm64-darwin)
167+
ffi (1.17.2-x86_64-linux-gnu)
168+
ffi (1.17.2-x86_64-linux-musl)
156169
fiddle (1.1.8)
157170
fugit (1.11.2)
158171
et-orbi (~> 1, >= 1.2.11)
@@ -164,6 +177,9 @@ GEM
164177
hashie (5.0.0)
165178
i18n (1.14.7)
166179
concurrent-ruby (~> 1.0)
180+
image_processing (1.14.0)
181+
mini_magick (>= 4.9.5, < 6)
182+
ruby-vips (>= 2.0.17, < 3)
167183
importmap-rails (2.2.2)
168184
actionpack (>= 6.0.0)
169185
activesupport (>= 6.0.0)
@@ -209,6 +225,8 @@ GEM
209225
net-smtp
210226
marcel (1.1.0)
211227
matrix (0.4.3)
228+
mini_magick (5.3.1)
229+
logger
212230
mini_mime (1.1.5)
213231
minitest (5.26.0)
214232
msgpack (1.8.0)
@@ -392,6 +410,9 @@ GEM
392410
rubocop-performance (>= 1.24)
393411
rubocop-rails (>= 2.30)
394412
ruby-progressbar (1.13.0)
413+
ruby-vips (2.2.5)
414+
ffi (~> 1.12)
415+
logger
395416
rubyzip (3.1.1)
396417
securerandom (0.4.1)
397418
selenium-webdriver (4.35.0)
@@ -500,6 +521,7 @@ PLATFORMS
500521

501522
DEPENDENCIES
502523
aasm
524+
active_storage_validations
503525
annotaterb
504526
blind_index
505527
bootsnap
@@ -508,6 +530,7 @@ DEPENDENCIES
508530
capybara
509531
debug
510532
erb_lint
533+
image_processing (~> 1.2)
511534
importmap-rails
512535
jbuilder
513536
kamal

app/models/project.rb

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,34 @@ class Project < ApplicationRecord
1717
has_many :users, through: :memberships
1818

1919
has_one_attached :demo_video
20-
has_one_attached :banner
20+
# https://github.com/rails/rails/pull/39135
21+
has_one_attached :banner do |attachable|
22+
# using resize_to_fill instead of resize_to_limit because consistency. might change it to resize_to_limit
23+
# we're preprocessing them because its likely going to be used
24+
25+
# for explore and projects#index
26+
attachable.variant :card,
27+
resize_to_fill: [ 1600, 900 ],
28+
format: :webp,
29+
preprocess: true,
30+
saver: { strip: true, quality: 75 }
31+
32+
# attachable.variant :not_sure,
33+
# resize_to_fill: [ 1200, 630 ],
34+
# format: :webp,
35+
# saver: { strip: true, quality: 75 }
36+
37+
# for voting
38+
attachable.variant :thumb,
39+
resize_to_fill: [ 400, 210 ],
40+
format: :webp,
41+
preprocess: true,
42+
saver: { strip: true, quality: 75 }
43+
end
2144

2245
validates :title, presence: true, length: { maximum: 120 }
2346
validates :description, length: { maximum: 1_000 }, allow_blank: true
47+
validates :banner,
48+
content_type: [ "image/jpeg", "image/png", "image/webp", "image/heic", "image/heif" ],
49+
size: { less_than: 10.megabytes, message: "is too large (max 10 MB)" }
2450
end

0 commit comments

Comments
 (0)