module Trestle::ActiveStorage::ControllerConcern

Private Instance Methods

define_attachment_accessors() click to toggle source
# File lib/trestle/active_storage/controller_concern.rb, line 12
def define_attachment_accessors
  self.instance = admin.find_instance(params)

  admin.active_storage_fields.each do |field|
    attachment = instance.send(field)

    if attachment.respond_to?(:each)
      attachment.each do |att|
        instance.class.send(:attr_accessor, "delete_#{field}_#{att.blob_id}")
      end
    else
      instance.class.send(:attr_accessor, "delete_#{field}")
    end
  end
end
purge_attachments() click to toggle source
# File lib/trestle/active_storage/controller_concern.rb, line 28
def purge_attachments
  admin.active_storage_fields.each do |field|
    attachment = instance.send(field)

    if attachment.respond_to?(:each)
      attachment.each do |att|
        att.purge if instance.try("delete_#{field}_#{att.blob_id}") == '1'
      end
    else
      instance.send(field).purge if instance.try("delete_#{field}") == '1'
    end
  end
end