require 'administrate/field/base' require 'rails' module Administrate module Field class ActiveStorage < Administrate::Field::Base class Engine < ::Rails::Engine end def url_only? options.fetch(:url_only, false) end def destroyable? options.key?(:destroy_path) end def show_in_index? options.fetch(:show_in_index, false) end def index_display_count? options.fetch(:index_display_count) { attached? && attachments.count != 1 } end def show_preview_size options.fetch(:show_preview_size, [1080, 1920]) end def many? # find a way to use instance_of # data.class.name == 'ActiveStorage::Attached::Many' data.is_a? ::ActiveStorage::Attached::Many end def direct? options.fetch(:direct_upload, false) end def drop_js? options.fetch(:drop_js, false) end def show_drop_js_on_show? options.fetch(:show_drop_js_on_show, false) end def drop_max_file? options.fetch(:drop_max_file, many? == true ? 10 : 1) end def drop_max_file_size? options.fetch(:drop_max_file_size, 10) end def drop_accepted_files? options.fetch(:drop_accepted_files, ".jpeg,.jpg,.png,.gif,.svg") end def destroy_url options.fetch(:destroy_url, nil) 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 can_add_attachment? many? || attachments.blank? end # this methode is no longer used ness to clean safety 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 # delegate :attached?, to: :data # delegate :attachments, to: :data def attached? data.present? && data.attached? end def attachments data.attachments if attached? end end end end