require 'administrate/field/base' require 'rails' module Administrate module Field class ActiveStorage < Administrate::Field::Base class Engine < ::Rails::Engine end def destroyable? options.key?(:destroy_path) end def index_display_preview? options.fetch(:index_display_preview, true) end def index_preview_size options.fetch(:index_preview_size, [150, 150]) end def index_display_count? options.fetch(:index_display_count) { attachments.count != 1 } end def show_display_preview? options.fetch(:show_display_preview, true) end def show_preview_size options.fetch(:show_preview_size, [800, 800]) end def many? # find a way to use instance_of data.class.name == 'ActiveStorage::Attached::Many' end def direct? options.fetch(:direct_upload, false) end # def destroy_path? # options.fetch(:destroy_path, false).present? # end # currently we are using Rails.application.routes.url_helpers # without including the namespace because it runs into an # exception # work around since calling data.preview(options) # returns "/images/" which isnt the url def preview(attachment, options) Rails.application.routes.url_helpers.rails_representation_path(attachment.preview(options), only_path: true) end def variant(attachment, options) Rails.application.routes.url_helpers.rails_representation_path(attachment.variant(combine_options: options), only_path: true) end def url(attachment) Rails.application.routes.url_helpers.rails_blob_path(attachment, only_path: true) end def blob_url(attachment) Rails.application.routes.url_helpers.rails_blob_path(attachment, disposition: :attachment, only_path: true) end def destroy_path(field, attachment) destroy_path_helper = options.fetch(:destroy_path) record_id = field.data.record.id attachment_id = attachment.id Rails.application.routes.url_helpers.send(destroy_path_helper, {:record_id => record_id, :attachment_id => attachment_id}) end def can_add_attachment? many? || attachments.empty? end def attached? data.present? && data.attached? end delegate :attachments, to: :data end end end