Class: Fiona7::AttributeWriters::BinaryAsBinary

Inherits:
AttributeWriter show all
Defined in:
lib/fiona7/attribute_writers/binary_as_binary.rb

Instance Method Summary collapse

Methods inherited from AttributeWriter

#initialize

Constructor Details

This class inherits a constructor from Fiona7::AttributeWriters::AttributeWriter

Instance Method Details

#call(value, claimed_type = nil) ⇒ Object



9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/fiona7/attribute_writers/binary_as_binary.rb', line 9

def call(value, claimed_type=nil)
  if value.kind_of?(File)
    self.special_upload_handling(self.attr_name, value)
  elsif value.kind_of?(ActionDispatch::Http::UploadedFile)
    self.special_upload_uploaded_handling(self.attr_name, value)
  elsif value.nil?
    self.obj.set(self.attr_name, '')
  elsif value.kind_of?(Hash)
    source_blob_id = value["id"] || value["id_to_copy"]
    filename       = value["filename"]
    content_type   = value["content_type"]

    # There is a little bit of magic behind this.
    # This will pass some data directly to the crul_obj
    # bypassing self.obj.set(), but calling self.obj.save
    # afterwards still persists the data correctly
    #
    # This works the same way as Reactor::Tools::Uploader
    Fiona7::Builder::LazyBlobCopier.new({
      destination_obj:  self.obj,
      attr_name:        self.attr_name,
      source_blob_id:   source_blob_id,
      filename:         filename,
      content_type:     content_type
    }).call

    # NOTE: no self.obj.set() required here
  else
    raise Scrivito::ClientError.new("Invalid input for binary field", 422)
  end
end