Skip to content

Commit 75d24b6

Browse files
committed
Remove carrierwave dependency from Spotlight, relying on ActiveStorage
1 parent 9c13409 commit 75d24b6

File tree

16 files changed

+66
-137
lines changed

16 files changed

+66
-137
lines changed

app/models/spotlight/attachment.rb

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,18 @@ module Spotlight
44
##
55
# Sir-trevor image upload attachments
66
class Attachment < ActiveRecord::Base
7+
# Open to alternatives on how to do this, but this works
8+
include Rails.application.routes.url_helpers
9+
710
belongs_to :exhibit
8-
mount_uploader :file, Spotlight::AttachmentUploader
11+
has_one_attached :file
912

10-
def as_json(options = nil)
11-
file.as_json(options).merge(name: name, uid: uid, attachment: to_global_id)
13+
def as_json(_options = nil)
14+
# as_json has problems with single has_one_attached content
15+
# https://github.com/rails/rails/issues/33036
16+
{
17+
name: name, uid: uid, attachment: to_global_id, url: rails_blob_path(file, only_path: true)
18+
}
1219
end
1320
end
1421
end

app/models/spotlight/bulk_update.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
module Spotlight
44
class BulkUpdate < ActiveRecord::Base
5-
mount_uploader :file, Spotlight::BulkUpdatesUploader
5+
has_one_attached :file
66
belongs_to :exhibit
77
end
88
end

app/models/spotlight/featured_image.rb

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,16 @@ module Spotlight
44
##
55
# Featured images for browse categories, feature pages, and exhibits
66
class FeaturedImage < ActiveRecord::Base
7-
mount_uploader :image, Spotlight::FeaturedImageUploader
7+
has_one_attached :image
88

99
before_validation do
1010
next unless upload_id.present? && source == 'remote'
1111

1212
# copy the image from the temp upload
1313
temp_image = Spotlight::TemporaryImage.find(upload_id)
14-
self.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(temp_image.image.read),
15-
filename: temp_image.image.filename || temp_image.image.identifier,
16-
content_type: temp_image.image.content_type
14+
image.attach(io: StringIO.new(temp_image.image.read),
15+
filename: temp_image.image.filename || temp_image.image.identifier,
16+
content_type: temp_image.image.content_type)
1717

1818
# Unset the incoming iiif_tilesource, which points at the temp image
1919
self.iiif_tilesource = nil
@@ -24,13 +24,6 @@ class FeaturedImage < ActiveRecord::Base
2424
Spotlight::TemporaryImage.find(upload_id).delete if upload_id.present?
2525
end
2626

27-
after_save do
28-
if image.present?
29-
image.cache! unless image.cached?
30-
image.store!
31-
end
32-
end
33-
3427
attr_accessor :upload_id
3528

3629
def iiif_url
@@ -60,7 +53,7 @@ def document
6053
end
6154

6255
def file_present?
63-
image.file.present?
56+
image.blob.present?
6457
end
6558

6659
def iiif_tilesource

app/services/spotlight/carrierwave_file_resolver.rb

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,10 @@ def initialize
99
end
1010

1111
def pattern(id)
12-
uploaded_file = Spotlight::FeaturedImage.find(id).image.file
12+
uploaded_file = Spotlight::FeaturedImage.find(id).image.blob
1313
raise Riiif::ImageNotFoundError, "unable to find file for #{id}" if uploaded_file.nil?
1414

15-
uploaded_file.file
15+
ActiveStorage::Blob.service.path_for(uploaded_file.key)
1616
end
1717
end
1818
end

app/services/spotlight/exhibit_import_export_service.rb

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -186,9 +186,9 @@ def from_hash!(hash)
186186

187187
# dedupe by something??
188188
ar = exhibit.attachments.build(attr)
189-
ar.file = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
190-
filename: file[:filename],
191-
content_type: file[:content_type]
189+
ar.file.attach io: StringIO.new(Base64.decode64(file[:content])),
190+
filename: file[:filename],
191+
content_type: file[:content_type]
192192
end
193193

194194
hash[:languages].each do |attr|
@@ -212,9 +212,9 @@ def deserialize_featured_image(obj, method, data)
212212
image = obj.public_send("build_#{method}")
213213
image.update(data)
214214
if file
215-
image.image = CarrierWave::SanitizedFile.new tempfile: StringIO.new(Base64.decode64(file[:content])),
216-
filename: file[:filename],
217-
content_type: file[:content_type]
215+
image.image.attach io: StringIO.new(Base64.decode64(file[:content])),
216+
filename: file[:filename],
217+
content_type: file[:content_type]
218218
# Unset the iiif_tilesource field as the new image should be different, because
219219
# the source has been reloaded
220220
image.iiif_tilesource = nil

app/uploaders/spotlight/attachment_uploader.rb

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

app/uploaders/spotlight/bulk_updates_uploader.rb

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

app/uploaders/spotlight/featured_image_uploader.rb

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

blacklight-spotlight.gemspec

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,6 @@ these collections.)
2828
s.add_dependency 'bootstrap_form', '~> 4.1'
2929
s.add_dependency 'breadcrumbs_on_rails', '>= 3.0', '< 5'
3030
s.add_dependency 'cancancan'
31-
s.add_dependency 'carrierwave', '~> 2.2'
3231
s.add_dependency 'clipboard-rails', '~> 1.5'
3332
s.add_dependency 'devise', '~> 4.1'
3433
s.add_dependency 'devise_invitable'

lib/generators/spotlight/install_generator.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,11 @@ def add_js
2929
run 'bundle exec rails webpacker:install'
3030
end
3131

32+
def setup_activestorage
33+
run 'bundle exec rails active_storage:install'
34+
run 'bundle exec rails db:migrate'
35+
end
36+
3237
def inject_spotlight_routes
3338
route "mount Spotlight::Engine, at: 'spotlight'"
3439
gsub_file 'config/routes.rb', /^\s*root.*/ do |match|

0 commit comments

Comments
 (0)