Sha256: 18d5f7b4e99ddecd2926f599e7b09482bcaaa8da1046e046b0a88665b2d5d6d7

Contents?: true

Size: 1.38 KB

Versions: 4

Compression:

Stored size: 1.38 KB

Contents

module ActiveScaffold
  module Bridges
    class ActiveStorage
      module ActiveStorageBridge
        def initialize(model_id)
          super
          return unless ActiveScaffold::Bridges::ActiveStorage::ActiveStorageBridgeHelpers.klass_has_active_storage_fields?(model)

          model.send :extend, ActiveScaffold::Bridges::ActiveStorage::ActiveStorageBridgeHelpers

          # include the "delete" helpers for use with active scaffold, unless they are already included
          model.generate_delete_helpers

          update.multipart = true
          create.multipart = true

          model.active_storage_has_one_fields.each { |field| configure_active_storage_field(field.to_sym, :has_one) }
          model.active_storage_has_many_fields.each { |field| configure_active_storage_field(field.to_sym, :has_many) }
        end

        private

        def configure_active_storage_field(field, field_type)
          columns << field
          columns.exclude :"#{field}_attachment#{'s' if field_type == :has_many}"
          columns.exclude :"#{field}_blob#{'s' if field_type == :has_many}"
          columns[field].includes ||= [:"#{field}_attachment#{'s' if field_type == :has_many}", :"#{field}_blob#{'s' if field_type == :has_many}"]
          columns[field].form_ui ||= :"active_storage_#{field_type}"
          columns[field].params.add "delete_#{field}"
        end
      end
    end
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
active_scaffold-4.0.0 lib/active_scaffold/bridges/active_storage/active_storage_bridge.rb
active_scaffold-4.0.0.rc3 lib/active_scaffold/bridges/active_storage/active_storage_bridge.rb
active_scaffold-4.0.0.rc2 lib/active_scaffold/bridges/active_storage/active_storage_bridge.rb
active_scaffold-4.0.0.rc1 lib/active_scaffold/bridges/active_storage/active_storage_bridge.rb