Class: Dscf::Core::Attachable::SingleAttachmentProxy

Inherits:
Object
  • Object
show all
Defined in:
app/models/concerns/dscf/core/attachable.rb

Overview

SingleAttachmentProxy - handles has_one_file attachments

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(record, name, options) ⇒ SingleAttachmentProxy

Returns a new instance of SingleAttachmentProxy.



109
110
111
112
113
114
115
# File 'app/models/concerns/dscf/core/attachable.rb', line 109

def initialize(record, name, options)
  @record = record
  @name = name.to_s
  @options = options
  @errors = []
  @uploader_context = nil
end

Instance Attribute Details

#errorsArray<String> (readonly)

Get upload errors

Returns:

  • (Array<String>)


194
195
196
# File 'app/models/concerns/dscf/core/attachable.rb', line 194

def errors
  @errors
end

Instance Method Details

#as_json(options = {}) ⇒ Hash?

Serialize for JSON

Returns:

  • (Hash, nil)


198
199
200
201
202
# File 'app/models/concerns/dscf/core/attachable.rb', line 198

def as_json(options = {})
  return nil unless attached?

  attachment.as_json(options)
end

#attach(file) ⇒ Boolean

Attach a file to this record

Parameters:

  • file (ActionDispatch::Http::UploadedFile, File, Hash, nil)

Returns:

  • (Boolean)

    success status



128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
# File 'app/models/concerns/dscf/core/attachable.rb', line 128

def attach(file)
  @errors = []
  return detach if file.nil?

  # Validate and upload the file (pass record class for engine detection)
  uploader = FileStorage::Uploader.new(uploader_options.merge(record_class: @record.class))
  result = uploader.upload(file)

  unless result
    @errors = uploader.errors
    return false
  end

  # Remove existing attachment (if any)
  existing_attachment&.purge

  # Create new attachment record
  @record.file_attachments.create!(
    name: @name,
    file_key: result[:file_key],
    filename: result[:filename],
    content_type: result[:content_type],
    size: result[:size],
    metadata: result[:metadata] || {},
    uploaded_by: @uploader_context
  )

  @attachment = nil # Reset cache
  @uploader_context = nil # Reset uploader context
  true
rescue StandardError => e
  Rails.logger.error("Failed to attach file: #{e.message}")
  @errors << "Failed to attach file: #{e.message}"
  false
end

#attached?Boolean

Check if a file is attached

Returns:

  • (Boolean)


176
177
178
# File 'app/models/concerns/dscf/core/attachable.rb', line 176

def attached?
  attachment.present? && attachment.attached?
end

#attachmentFileAttachment?

Get the underlying FileAttachment record

Returns:



182
183
184
# File 'app/models/concerns/dscf/core/attachable.rb', line 182

def attachment
  @attachment ||= @record.file_attachments.with_name(@name).first
end

#detachBoolean Also known as: purge

Remove the attached file

Returns:

  • (Boolean)


166
167
168
169
170
# File 'app/models/concerns/dscf/core/attachable.rb', line 166

def detach
  existing_attachment&.purge
  @attachment = nil
  true
end

#exists?Boolean

Check if file exists in storage

Returns:

  • (Boolean)


188
189
190
# File 'app/models/concerns/dscf/core/attachable.rb', line 188

def exists?
  attachment&.exists_in_storage? || false
end

#uploaded_by(user) ⇒ self

Set the user who is uploading the file

Parameters:

  • user (ActiveRecord::Base)

    the user performing the upload

Returns:

  • (self)


120
121
122
123
# File 'app/models/concerns/dscf/core/attachable.rb', line 120

def uploaded_by(user)
  @uploader_context = user
  self
end