Skip to content

Commit dd19fc1

Browse files
jmilljr24maebeale
andauthored
[WIP] #17 Migrate from paperclip to active storage (#137)
* install active storage and migrate * temp fix user update Some methods for params were commented out for an unknown reason. This add's them back and the update method works properly. * add feature flag for active_storage setup initial change for user avatar * ignore local active storage attachments fix var * add feature flag to workshop attachments * add feature flag to all models using paperclip * check if avatar is attached * add active storage validations gem * add attachment validations * add feature flag to test config * fix jpeg content type * add check for attached avatar * update tests * add validation gem to test group revert * fix merge * add validation tests * move spec * fix rails 8.1 upgrade issues with enums * revert * clean up clean up * ignore local storage * update schema * clean up * update set_has_attachment * Remove duplicate gem * Remove active storage conditionals * Remove more active_storage env var conditionals * Remove merge conflict text * Remove 'image/jpg' type bc it's invalid * Make migration reversible * WIP: testing image usage * Update tests to activestorage (and always show image in navbar) --------- Co-authored-by: maebeale <[email protected]>
1 parent 9509332 commit dd19fc1

40 files changed

+1147
-437
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,3 +48,5 @@ node_modules
4848
*.local
4949

5050
.vite
51+
# Ignore local Active Storage
52+
storage

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,8 @@ gem "country_select"
4646
gem "turbo-rails", "~> 2.0"
4747
gem "stimulus-rails", "~> 1.3"
4848

49+
gem "active_storage_validations", "~> 3.0"
50+
4951
group :development, :test do
5052
gem "better_errors"
5153
gem "brakeman", require: false

Gemfile.lock

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,12 @@ GEM
4747
erubi (~> 1.11)
4848
rails-dom-testing (~> 2.2)
4949
rails-html-sanitizer (~> 1.6)
50+
active_storage_validations (3.0.2)
51+
activejob (>= 6.1.4)
52+
activemodel (>= 6.1.4)
53+
activestorage (>= 6.1.4)
54+
activesupport (>= 6.1.4)
55+
marcel (>= 1.0.3)
5056
activejob (8.1.0.beta1)
5157
activesupport (= 8.1.0.beta1)
5258
globalid (>= 0.3.6)
@@ -446,6 +452,7 @@ PLATFORMS
446452
ruby
447453

448454
DEPENDENCIES
455+
active_storage_validations (~> 3.0)
449456
apipie-rails (~> 1.5.0)
450457
aws-sdk-s3
451458
bcrypt (= 3.1.16)

app/decorators/workshop_decorator.rb

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -148,18 +148,17 @@ def main_image
148148
end
149149

150150
def thumbnail_image
151-
if legacy && !thumbnail.exists?
152-
main_image
153-
else
151+
# TODO Figure out if we need main_image
152+
if thumbnail.attached?
154153
thumbnail
155154
end
156155
end
157156

158157
def header_image
159-
if !header.exists?
160-
thumbnail_image
161-
else
158+
if header.attached?
162159
header
160+
elsif thumbnail.attached?
161+
thumbnail
163162
end
164163
end
165164

app/models/attachment.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
class Attachment < ApplicationRecord
22
belongs_to :owner, polymorphic: true
33

4-
has_attached_file :file
5-
validates_attachment :file, content_type: { content_type: %w(application/pdf application/msword image/gif image/jpg image/jpeg image/png) }
4+
ACCEPTED_CONTENT_TYPES = %w[application/pdf application/msword image/gif image/jpeg image/png].freeze
5+
has_one_attached :file
6+
validates :file, content_type: ACCEPTED_CONTENT_TYPES
67

78
def name
8-
'Pdf Attachment'
9+
"Pdf Attachment"
910
end
1011
end

app/models/ckeditor/attachment_file.rb

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,6 @@
11
class Ckeditor::AttachmentFile < ApplicationRecord # Ckeditor::Asset
2-
# has_attached_file :data,
3-
# :url => "/ckeditor_assets/attachments/:id/:filename",
4-
# :path => "/ckeditor_assets/attachments/:id/:filename"
5-
#
6-
# validates_attachment_presence :data
7-
# validates_attachment_size :data, :less_than => 100.megabytes
8-
# do_not_validate_attachment_file_type :data
2+
has_one_attached :data
3+
validates :data, size: {less_than: 100.megabytes}, attached: true
94

105
def url_thumb
116
@url_thumb ||= nil # Ckeditor::Utils.filethumb(filename)

app/models/ckeditor/picture.rb

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,7 @@
11
class Ckeditor::Picture < ApplicationRecord # Ckeditor::Asset
2-
# has_attached_file :data,
3-
# :url => "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
4-
# :path => "/ckeditor_assets/pictures/:id/:style_:basename.:extension",
5-
# :styles => { :content => '800>', :thumb => '118x100#' }
6-
#
7-
# validates_attachment_presence :data
8-
# validates_attachment_size :data, :less_than => 2.megabytes
9-
# validates_attachment_content_type :data, :content_type => /\Aimage/
2+
ACCEPTED_CONTENT_TYPES = ["image/jpeg", "image/png"].freeze
3+
has_one_attached :data
4+
validates :data, size: {less_than: 2.megabytes}, content_type: ACCEPTED_CONTENT_TYPES, attached: true
105

116
def url_content
127
url(:content)
@@ -20,6 +15,5 @@ def url(param)
2015
else
2116
actual_url.gsub("/original", "/thumb")
2217
end
23-
2418
end
2519
end

app/models/image.rb

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,7 @@ class Image < ApplicationRecord
22
belongs_to :owner, polymorphic: true
33
belongs_to :report, optional: true
44

5-
has_attached_file :file,
6-
styles: { medium: '300x300>', thumb: '100x100>' },
7-
default_url:
8-
ActionController::Base.helpers.asset_path(
9-
'workshop_default.png'
10-
)
11-
validates_attachment :file, content_type: { content_type: ["image/jpg", "image/jpeg", "image/png", "image/gif"] }
5+
ACCEPTED_CONTENT_TYPES = ["image/jpeg", "image/png", "image/gif"].freeze
6+
has_one_attached :file
7+
validates :file, content_type: ACCEPTED_CONTENT_TYPES
128
end

app/models/media_file.rb

Lines changed: 7 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,11 @@ class MediaFile < ApplicationRecord
33
belongs_to :workshop, optional: true
44
belongs_to :workshop_log, optional: true
55

6-
has_attached_file :file
7-
8-
FORM_FILE_CONTENT_TYPES = ["image/jpeg", "image/jpg", "image/png",
9-
"application/pdf", "application/msword",
10-
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
11-
"application/vnd.ms-excel",
12-
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]
13-
14-
validates_attachment :file, content_type: { content_type: FORM_FILE_CONTENT_TYPES }
6+
FORM_FILE_CONTENT_TYPES = ["image/jpeg", "image/png",
7+
"application/pdf", "application/msword",
8+
"application/vnd.openxmlformats-officedocument.wordprocessingml.document",
9+
"application/vnd.ms-excel",
10+
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"]
11+
has_one_attached :file
12+
validates :file, content_type: FORM_FILE_CONTENT_TYPES
1513
end

app/models/project_user.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ class ProjectUser < ApplicationRecord
33
belongs_to :project
44
belongs_to :user
55

6-
scope :liaisons, -> { where(position: 1) }
6+
scope :liaisons, -> { where(position: "liaison") }
77
# Validations
88
validates_presence_of :project_id
99

0 commit comments

Comments
 (0)