Class: Dscf::Core::Attachable::SingleAttachmentProxy
- Inherits:
-
Object
- Object
- Dscf::Core::Attachable::SingleAttachmentProxy
- Defined in:
- app/models/concerns/dscf/core/attachable.rb
Overview
SingleAttachmentProxy - handles has_one_file attachments
Instance Attribute Summary collapse
-
#errors ⇒ Array<String>
readonly
Get upload errors.
Instance Method Summary collapse
-
#as_json(options = {}) ⇒ Hash?
Serialize for JSON.
-
#attach(file) ⇒ Boolean
Attach a file to this record.
-
#attached? ⇒ Boolean
Check if a file is attached.
-
#attachment ⇒ FileAttachment?
Get the underlying FileAttachment record.
-
#detach ⇒ Boolean
(also: #purge)
Remove the attached file.
-
#exists? ⇒ Boolean
Check if file exists in storage.
-
#initialize(record, name, options) ⇒ SingleAttachmentProxy
constructor
A new instance of SingleAttachmentProxy.
-
#uploaded_by(user) ⇒ self
Set the user who is uploading the file.
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, ) @record = record @name = name.to_s @options = @errors = [] @uploader_context = nil end |
Instance Attribute Details
#errors ⇒ Array<String> (readonly)
Get upload errors
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
198 199 200 201 202 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 198 def as_json( = {}) return nil unless attached? .as_json() end |
#attach(file) ⇒ Boolean
Attach a file to this record
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(.merge(record_class: @record.class)) result = uploader.upload(file) unless result @errors = uploader.errors return false end # Remove existing attachment (if any) &.purge # Create new attachment record @record..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.}") @errors << "Failed to attach file: #{e.}" false end |
#attached? ⇒ Boolean
Check if a file is attached
176 177 178 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 176 def attached? .present? && .attached? end |
#attachment ⇒ FileAttachment?
Get the underlying FileAttachment record
182 183 184 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 182 def @attachment ||= @record..with_name(@name).first end |
#detach ⇒ Boolean Also known as: purge
Remove the attached file
166 167 168 169 170 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 166 def detach &.purge @attachment = nil true end |
#exists? ⇒ Boolean
Check if file exists in storage
188 189 190 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 188 def exists? &.exists_in_storage? || false end |
#uploaded_by(user) ⇒ self
Set the user who is uploading the file
120 121 122 123 |
# File 'app/models/concerns/dscf/core/attachable.rb', line 120 def uploaded_by(user) @uploader_context = user self end |