mirror of
https://github.com/openfoodfoundation/openfoodnetwork
synced 2026-01-24 20:36:49 +00:00
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.
60 lines
1.7 KiB
Ruby
60 lines
1.7 KiB
Ruby
# frozen_string_literal: true
|
|
|
|
module Admin
|
|
class ContentsController < Spree::Admin::BaseController
|
|
def edit
|
|
@preference_sections = preference_sections.map do |preference_section|
|
|
{ name: preference_section.name, preferences: preference_section.preferences }
|
|
end
|
|
end
|
|
|
|
def update
|
|
params.each do |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
|
|
|
|
flash[:success] =
|
|
t(:successfully_updated, resource: I18n.t('admin.contents.edit.your_content'))
|
|
|
|
redirect_to main_app.edit_admin_contents_path
|
|
end
|
|
|
|
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,
|
|
PreferenceSections::HomePageSection.new,
|
|
PreferenceSections::ProducerSignupPageSection.new,
|
|
PreferenceSections::HubSignupPageSection.new,
|
|
PreferenceSections::GroupSignupPageSection.new,
|
|
PreferenceSections::MainLinksSection.new,
|
|
PreferenceSections::FooterAndExternalLinksSection.new,
|
|
PreferenceSections::UserGuideSection.new,
|
|
PreferenceSections::MapSection.new
|
|
]
|
|
end
|
|
end
|
|
end
|