Sha256: 10a7859587d0f61e1e1112e0e6eaf430536bd7505c288597691aab8d4d7a2290

Contents?: true

Size: 1.36 KB

Versions: 7

Compression:

Stored size: 1.36 KB

Contents

# frozen_string_literal: true

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

RailsAdmin::Config::Fields.register_factory do |parent, properties, fields|
  if defined?(::ActiveStorage) && properties.try(:association?) && (match = /\A(.+)_attachments?\Z/.match properties.name) && properties.klass.to_s == 'ActiveStorage::Attachment'
    name = match[1]
    field = RailsAdmin::Config::Fields::Types.load(
      properties.type == :has_many ? :multiple_active_storage : :active_storage,
    ).new(parent, name, properties)
    fields << field
    associations =
      if properties.type == :has_many
        [:"#{name}_attachments", :"#{name}_blobs"]
      else
        [:"#{name}_attachment", :"#{name}_blob"]
      end
    children_fields = associations.map do |child_name|
      child_association = parent.abstract_model.associations.detect { |p| p.name.to_sym == child_name }
      next unless child_association

      child_field = fields.detect { |f| f.name == child_name } || RailsAdmin::Config::Fields.default_factory.call(parent, child_association, fields)
      child_field.hide unless field == child_field
      child_field.filterable(false) unless field == child_field
      child_field.name
    end.flatten.compact
    field.children_fields(children_fields)
    true
  else
    false
  end
end

Version data entries

7 entries across 7 versions & 1 rubygems

Version Path
rails_admin-3.3.0 lib/rails_admin/config/fields/factories/active_storage.rb
rails_admin-3.2.1 lib/rails_admin/config/fields/factories/active_storage.rb
rails_admin-3.2.0 lib/rails_admin/config/fields/factories/active_storage.rb
rails_admin-3.2.0.rc lib/rails_admin/config/fields/factories/active_storage.rb
rails_admin-3.2.0.beta lib/rails_admin/config/fields/factories/active_storage.rb
rails_admin-3.1.4 lib/rails_admin/config/fields/factories/active_storage.rb
rails_admin-3.1.3 lib/rails_admin/config/fields/factories/active_storage.rb