Sha256: b657aa457117faeef4517c4dad2ca86f3a33a10d24be20c685a9cda1f7764bb6

Contents?: true

Size: 1.37 KB

Versions: 5

Compression:

Stored size: 1.37 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: '100x100>'}
          end

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

          register_instance_option :image? do
            if value
              value.filename.to_s.split('.').last =~ /jpg|jpeg|png|gif|svg/i
            end
          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 && attachment.attached?
          end
        end
      end
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
rails_admin-2.0.2 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-2.0.1 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-2.0.0 lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-2.0.0.rc lib/rails_admin/config/fields/types/active_storage.rb
rails_admin-2.0.0.beta lib/rails_admin/config/fields/types/active_storage.rb