Class: ActiveStorage::Attached::Changes::CreateOne

Inherits:
Object
  • Object
show all
Defined in:
activestorage/lib/active_storage/attached/changes/create_one.rb

Overview

:nodoc:

Direct Known Subclasses

CreateOneOfMany

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name, record, attachable) ⇒ CreateOne

Returns a new instance of CreateOne.



10
11
12
13
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 10

def initialize(name, record, attachable)
  @name, @record, @attachable = name, record, attachable
  blob.identify_without_saving
end

Instance Attribute Details

#attachableObject (readonly)

Returns the value of attribute attachable.



8
9
10
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 8

def attachable
  @attachable
end

#nameObject (readonly)

Returns the value of attribute name.



8
9
10
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 8

def name
  @name
end

#recordObject (readonly)

Returns the value of attribute record.



8
9
10
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 8

def record
  @record
end

Instance Method Details

#attachmentObject



15
16
17
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 15

def attachment
  @attachment ||= find_or_build_attachment
end

#blobObject



19
20
21
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 19

def blob
  @blob ||= find_or_build_blob
end

#saveObject



48
49
50
51
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 48

def save
  record.public_send("#{name}_attachment=", attachment)
  record.public_send("#{name}_blob=", blob)
end

#uploadObject



23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'activestorage/lib/active_storage/attached/changes/create_one.rb', line 23

def upload
  case attachable
  when ActionDispatch::Http::UploadedFile
    blob.upload_without_unfurling(attachable.open)
  when Rack::Test::UploadedFile
    blob.upload_without_unfurling(
      attachable.respond_to?(:open) ? attachable.open : attachable
    )
  when Hash
    blob.upload_without_unfurling(attachable.fetch(:io))
  when File
    blob.upload_without_unfurling(attachable)
  when Pathname
    blob.upload_without_unfurling(attachable.open)
  when ActiveStorage::Blob
  when String
  else
    raise(
      ArgumentError,
      "Could not upload: expected attachable, " \
        "got #{attachable.inspect}"
    )
  end
end