mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-03-13 04:00:21 +00:00
Replace Paperclippable ContentConfig
The old Paperclip configuration was very clever and easy to use but it was also a complicated implementation building on the complicated Spree preference system. I simplified this with Active Storage, storing simple references to blob ids and default URLs as backup.
This commit is contained in:
@@ -10,14 +10,14 @@ module Admin
|
||||
|
||||
def update
|
||||
params.each do |name, value|
|
||||
if ContentConfig.has_preference?(name) || ContentConfig.has_attachment?(name)
|
||||
ContentConfig.public_send("#{name}=", value)
|
||||
if value.is_a?(ActionDispatch::Http::UploadedFile)
|
||||
blob = store_file(value)
|
||||
update_preference("#{name}_blob_id", blob.id)
|
||||
else
|
||||
update_preference(name, value)
|
||||
end
|
||||
end
|
||||
|
||||
# Save any uploaded images
|
||||
ContentConfig.save
|
||||
|
||||
flash[:success] =
|
||||
t(:successfully_updated, resource: I18n.t('admin.contents.edit.your_content'))
|
||||
|
||||
@@ -26,6 +26,22 @@ module Admin
|
||||
|
||||
private
|
||||
|
||||
def store_file(attachable)
|
||||
ActiveStorage::Blob.create_and_upload!(
|
||||
io: attachable.open,
|
||||
filename: attachable.original_filename,
|
||||
content_type: attachable.content_type,
|
||||
service_name: :local,
|
||||
identify: false,
|
||||
)
|
||||
end
|
||||
|
||||
def update_preference(name, value)
|
||||
return unless ContentConfig.has_preference?(name)
|
||||
|
||||
ContentConfig.public_send("#{name}=", value)
|
||||
end
|
||||
|
||||
def preference_sections
|
||||
[
|
||||
PreferenceSections::HeaderSection.new,
|
||||
|
||||
Reference in New Issue
Block a user