Sha256: bb8144f5e909e5d28b2ebc90bad4dfd5a590e765a6d8c0e06b5dcf5c29054230
Contents?: true
Size: 1.19 KB
Versions: 35
Compression:
Stored size: 1.19 KB
Contents
# frozen_string_literal: true module Motor class ActiveStorageAttachmentsController < ApiBaseController include Motor::WrapIoParams wrap_parameters :data, except: %i[include fields] load_and_authorize_resource :attachment, class: 'ActiveStorage::Attachment', parent: false def create if attachable?(record) record.public_send(@attachment.name).attach(file_params) head :ok else head :unprocessable_entity end end private def record record_pk = @attachment.record.class.primary_key Motor::Resources::FetchConfiguredModel.call( @attachment.record.class, cache_key: Motor::Resource.maximum(:updated_at) ).find_by(record_pk => @attachment.record[record_pk]) end def attachable?(record) record.respond_to?("#{@attachment.name}_attachment=") || record.respond_to?("#{@attachment.name}_attachments=") end def file_params params.require(:data).require(:file).permit(:io, :filename).to_h.symbolize_keys end def attachment_params if params[:data].present? params.require(:data).except(:file).permit! else {} end end end end
Version data entries
35 entries across 35 versions & 1 rubygems