Skip to content

Commit 542f9b3

Browse files
committed
Add active_storage_validations gem for robust attachment validations for image block and other attachments
1 parent e855a90 commit 542f9b3

File tree

4 files changed

+25
-12
lines changed

4 files changed

+25
-12
lines changed

Gemfile.lock

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ PATH
2020
remote: .
2121
specs:
2222
better_together (0.5.0)
23+
active_storage_validations
2324
activerecord-import
2425
activerecord-postgis-adapter
2526
bootstrap (~> 5.3.2)
@@ -102,6 +103,11 @@ GEM
102103
erubi (~> 1.11)
103104
rails-dom-testing (~> 2.2)
104105
rails-html-sanitizer (~> 1.6)
106+
active_storage_validations (1.1.4)
107+
activejob (>= 5.2.0)
108+
activemodel (>= 5.2.0)
109+
activestorage (>= 5.2.0)
110+
activesupport (>= 5.2.0)
105111
activejob (7.1.3.4)
106112
activesupport (= 7.1.3.4)
107113
globalid (>= 0.3.6)
@@ -140,7 +146,7 @@ GEM
140146
mime-types (>= 2.99)
141147
unf
142148
ast (2.4.2)
143-
autoprefixer-rails (10.4.16.0)
149+
autoprefixer-rails (10.4.19.0)
144150
execjs (~> 2)
145151
aws-eventstream (1.3.0)
146152
aws-partitions (1.973.0)
@@ -289,7 +295,7 @@ GEM
289295
ruby-progressbar (~> 1.4)
290296
globalid (1.2.1)
291297
activesupport (>= 6.1)
292-
google-protobuf (4.27.1-x86_64-linux)
298+
google-protobuf (4.28.0-x86_64-linux)
293299
bigdecimal
294300
rake (>= 13)
295301
highline (3.1.0)
@@ -377,7 +383,7 @@ GEM
377383
nio4r (2.7.3)
378384
nokogiri (1.16.7-x86_64-linux)
379385
racc (~> 1.4)
380-
noticed (2.4.1)
386+
noticed (2.4.3)
381387
rails (>= 6.1.0)
382388
optimist (3.1.0)
383389
orm_adapter (0.5.0)
@@ -550,12 +556,12 @@ GEM
550556
ffi (~> 1.12)
551557
logger
552558
rubyzip (2.3.2)
553-
sass-embedded (1.77.4-x86_64-linux-gnu)
554-
google-protobuf (>= 3.25, < 5.0)
559+
sass-embedded (1.78.0-x86_64-linux-gnu)
560+
google-protobuf (~> 4.27)
555561
sassc (2.4.0)
556562
ffi (~> 1.9)
557-
sassc-embedded (1.76.0)
558-
sass-embedded (~> 1.76)
563+
sassc-embedded (1.78.0)
564+
sass-embedded (~> 1.78)
559565
selenium-webdriver (4.24.0)
560566
base64 (~> 0.2)
561567
logger (~> 1.4)
@@ -607,7 +613,7 @@ GEM
607613
unicode-display_width (>= 1.1.1, < 3)
608614
thor (1.3.2)
609615
thread_safe (0.3.6)
610-
tilt (2.3.0)
616+
tilt (2.4.0)
611617
timeout (0.4.1)
612618
tins (1.33.0)
613619
bigdecimal

app/models/better_together/content/image.rb

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@ module Content
44
class Image < Block
55
include Translatable
66

7+
CONTENT_TYPES = %w[image/jpeg image/png image/gif image/webp image/svg+xml].freeze
8+
79
has_one_attached :media
810

911
delegate :url, to: :media
@@ -12,7 +14,13 @@ class Image < Block
1214
translates :alt_text, type: :string
1315
translates :caption, type: :string
1416

15-
validate :acceptable_media
17+
validates :media,
18+
presence: true,
19+
attached: true,
20+
processable_image: true,
21+
content_type: CONTENT_TYPES,
22+
size: { less_than: 100.megabytes , message: 'is too large' }
23+
1624

1725
validates :attribution_url,
1826
format: {
@@ -21,9 +29,6 @@ class Image < Block
2129
message: 'must be a valid URL starting with "http" or "https"'
2230
}
2331

24-
def acceptable_media
25-
return unless media.attached?
26-
end
2732
end
2833
end
2934
end

better_together.gemspec

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ Gem::Specification.new do |spec|
3030

3131
spec.add_dependency 'activerecord-import'
3232
spec.add_dependency 'activerecord-postgis-adapter'
33+
spec.add_dependency 'active_storage_validations'
3334
spec.add_dependency 'bootstrap', '~> 5.3.2'
3435
spec.add_dependency 'dartsass-sprockets', '~> 3.1'
3536
spec.add_dependency 'devise'

lib/better_together/engine.rb

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require 'action_cable/engine'
44
require 'action_text/engine'
55
require 'active_storage/engine'
6+
require 'active_storage_validations'
67
require 'activerecord-import'
78
require 'better_together/column_definitions'
89
require 'better_together/migration_helpers'

0 commit comments

Comments
 (0)