Module: Dscf::Core::Attachable

Extended by:
ActiveSupport::Concern
Defined in:
app/models/concerns/dscf/core/attachable.rb

Overview

Include this concern in any model to add file attachment capabilities Works exactly like ActiveStorage’s has_one_attached and has_many_attached NO MIGRATION NEEDED per model - just include and declare!

Examples:

Single file attachment

class UserProfile < ApplicationRecord
  include Dscf::Core::Attachable

  has_one_file :avatar
  has_one_file :cover_photo, max_size: 10.megabytes
end

profile.avatar.attach(params[:avatar])
profile.avatar.attached? # => true
profile.avatar.url       # => "/dscf/core/files/..."
profile.avatar.download  # => binary data
profile.avatar.purge     # deletes file

Multiple files attachment

class Product < ApplicationRecord
  include Dscf::Core::Attachable

  has_many_files :images
  has_many_files :documents, allowed_types: %w[application/pdf]
end

product.images.attach(params[:images])
product.images.attached? # => true
product.images.count     # => 3
product.images.first.url # => "/dscf/core/files/..."
product.images.purge_all # deletes all

Defined Under Namespace

Classes: MultipleAttachmentProxy, SingleAttachmentProxy