Class: Attach::Attachment

Inherits:
ActiveRecord::Base
  • Object
show all
Defined in:
lib/attach/attachment.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#backend=(value) ⇒ Object

Sets the attribute backend

Parameters:

  • value

    the value to set the attribute backend to.



16
17
18
# File 'lib/attach/attachment.rb', line 16

def backend=(value)
  @backend = value
end

Class Method Details

.for(role) ⇒ Object



166
167
168
# File 'lib/attach/attachment.rb', line 166

def for(role)
  where(role: role).first
end

Instance Method Details

#add_child(role, &block) ⇒ Object

rubocop:disable Metrics/AbcSize



82
83
84
85
86
87
88
89
90
91
92
93
94
95
# File 'lib/attach/attachment.rb', line 82

def add_child(role, &block)
  attachment = children.build
  attachment.role = role
  attachment.owner = owner
  attachment.file_name = file_name
  attachment.file_type = file_type
  attachment.disposition = disposition
  attachment.cache_type = cache_type
  attachment.cache_max_age = cache_max_age
  attachment.serve = serve
  attachment.type = type
  block.call(attachment)
  attachment.save!
end

#blobObject



41
42
43
44
45
46
# File 'lib/attach/attachment.rb', line 41

def blob
  return @blob if instance_variable_defined?('@blob')
  return nil unless persisted?

  @blob = backend.read(self)
end

#blob=(blob) ⇒ Object



48
49
50
51
52
53
54
# File 'lib/attach/attachment.rb', line 48

def blob=(blob)
  unless blob.nil? || blob.is_a?(BlobTypes::File) || blob.is_a?(BlobTypes::Raw)
    raise ArgumentError, 'Only nil or a File/Raw blob type can be set as a blob for an attachment'
  end

  @blob = blob
end

#child(role) ⇒ Object



68
69
70
71
72
73
74
75
# File 'lib/attach/attachment.rb', line 68

def child(role)
  @cached_children ||= {}
  if @cached_children.key?(role.to_sym)
    return @cached_children[role.to_sym]
  end

  @cached_children[role.to_sym] = children.where(role: role).first
end

#copy_attributes_from_file(file) ⇒ Object

rubocop:disable Metrics/AbcSize



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/attach/attachment.rb', line 99

def copy_attributes_from_file(file)
  case file.class.name
  when 'ActionDispatch::Http::UploadedFile'
    self.blob = BlobTypes::File.new(file.tempfile)
    self.file_name = file.original_filename
    self.file_type = file.content_type
  when 'Attach::File'
    self.blob = BlobTypes::Raw.new(file.data)
    self.file_name = file.name
    self.file_type = file.type
  else
    self.blob = BlobTypes::Raw.new(file)
    self.file_name = 'untitled'
    self.file_type = 'application/octet-stream'
  end
end

#image?Boolean

Returns:

  • (Boolean)


60
61
62
# File 'lib/attach/attachment.rb', line 60

def image?
  file_type =~ /\Aimage\//
end

#processorObject



64
65
66
# File 'lib/attach/attachment.rb', line 64

def processor
  @processor ||= Processor.new(self)
end

#try(role) ⇒ Object



77
78
79
# File 'lib/attach/attachment.rb', line 77

def try(role)
  child(role) || self
end

#urlObject



56
57
58
# File 'lib/attach/attachment.rb', line 56

def url
  backend.url(self)
end