Sha256: a2ccf8d4a0f9de1c75b4091d5196bb9a310d73f34b17e3f6e0533844df98fe40

Contents?: true

Size: 1.54 KB

Versions: 7

Compression:

Stored size: 1.54 KB

Contents

require 'rails_admin/config/fields/types/file_upload'

module RailsAdmin
  module Config
    module Fields
      module Types
        class ActiveStorage < RailsAdmin::Config::Fields::Types::FileUpload
          RailsAdmin::Config::Fields::Types.register(self)

          register_instance_option :thumb_method do
            {resize_to_limit: [100, 100]}
          end

          register_instance_option :delete_method do
            "remove_#{name}" if bindings[:object].respond_to?("remove_#{name}")
          end

          register_instance_option :image? do
            if value
              mime_type = Mime::Type.lookup_by_extension(value.filename.extension_without_delimiter)
              mime_type.to_s.match?(/^image/)
            end
          end

          register_instance_option :eager_load do
            {"#{name}_attachment": :blob}
          end

          def resource_url(thumb = false)
            return nil unless value

            if thumb && value.variable?
              thumb = thumb_method if thumb == true
              variant = value.variant(thumb)
              Rails.application.routes.url_helpers.rails_blob_representation_path(
                variant.blob.signed_id, variant.variation.key, variant.blob.filename, only_path: true
              )
            else
              Rails.application.routes.url_helpers.rails_blob_path(value, only_path: true)
            end
          end

          def value
            attachment = super
            attachment if attachment&.attached?
          end
        end
      end
    end
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails_admin-3.0.0 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-3.0.0.rc4 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-3.0.0.rc3 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-3.0.0.rc2 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-3.0.0.rc lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-3.0.0.beta2 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-3.0.0.beta lib/rails_admin/config/fields/types/active_storage.rb