Class: Attachment

Inherits:
Object
  • Object
show all
Defined in:
lib/yodel/models/core/attachments/attachment.rb

Direct Known Subclasses

Image

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(value, record, field) ⇒ Attachment

Returns a new instance of Attachment.



4
5
6
7
8
9
10
# File 'lib/yodel/models/core/attachments/attachment.rb', line 4

def initialize(value, record, field)
  @field = field
  @record = record
  value ||= {}
  @name = value['name']
  @mime = value['mime']
end

Instance Attribute Details

#fieldObject

Returns the value of attribute field.



2
3
4
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2

def field
  @field
end

#mimeObject

Returns the value of attribute mime.



2
3
4
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2

def mime
  @mime
end

#nameObject

Returns the value of attribute name.



2
3
4
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2

def name
  @name
end

#recordObject

Returns the value of attribute record.



2
3
4
# File 'lib/yodel/models/core/attachments/attachment.rb', line 2

def record
  @record
end

Instance Method Details

#directory_pathObject



38
39
40
# File 'lib/yodel/models/core/attachments/attachment.rb', line 38

def directory_path
  @directory_path ||= File.join(@record.site.attachments_directory, relative_directory_path)
end

#empty?Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/yodel/models/core/attachments/attachment.rb', line 47

def empty?
  !exist?
end

#exist?Boolean

Returns:

  • (Boolean)


42
43
44
45
# File 'lib/yodel/models/core/attachments/attachment.rb', line 42

def exist?
  return false if @name.nil?
  File.exist?(path)
end

#lengthObject



79
80
81
82
# File 'lib/yodel/models/core/attachments/attachment.rb', line 79

def length
  return 0 unless self.exist?
  return File.size(path)
end

#pathObject



34
35
36
# File 'lib/yodel/models/core/attachments/attachment.rb', line 34

def path
  @path ||= File.join(@record.site.attachments_directory, relative_path)
end

#relative_directory_pathObject



24
25
26
27
28
29
30
31
32
# File 'lib/yodel/models/core/attachments/attachment.rb', line 24

def relative_directory_path
  @relative_directory_path ||= begin
    if @record.is_a?(EmbeddedRecord)
      File.join("#{@record.embedded_field.name}_#{@field.name}", @record.parent_record.id.to_s, @record.id.to_s)
    else
      File.join(@field.name, @record.id.to_s)
    end
  end
end

#relative_pathObject



20
21
22
# File 'lib/yodel/models/core/attachments/attachment.rb', line 20

def relative_path
  @relative_path ||= File.join(relative_directory_path, @name)
end

#remove_filesObject



51
52
53
# File 'lib/yodel/models/core/attachments/attachment.rb', line 51

def remove_files
  FileUtils.rm_r directory_path if exist?
end

#reset_memoised_valuesObject



55
56
57
# File 'lib/yodel/models/core/attachments/attachment.rb', line 55

def reset_memoised_values
  @url = @relative_path = @relative_directory_path = @path = @directory_path = nil
end

#set_file(file) ⇒ Object



59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/yodel/models/core/attachments/attachment.rb', line 59

def set_file(file)
  # delete the old file and reset memoised paths
  unless @record.new?
    remove_files
    reset_memoised_values
  end

  # reset the name and mime type of the attachment
  @name = file[:filename]
  @mime = file[:type]
  temp  = file[:tempfile]
  temp_path = temp.path
  temp.close
  
  # for simplicity we move the uploaded file (from /tmp) rather than copying
  FileUtils.mkpath directory_path
  FileUtils.mv(temp_path, path)
  FileUtils.chmod(0664, path) # (owner: rw, group: rw, other: r)
end

#to_hashObject



12
13
14
# File 'lib/yodel/models/core/attachments/attachment.rb', line 12

def to_hash
  {'name' => name, 'mime' => mime}
end

#urlObject



16
17
18
# File 'lib/yodel/models/core/attachments/attachment.rb', line 16

def url
  @url ||= Pathname.new('/').join(Yodel::ATTACHMENTS_DIRECTORY_NAME, relative_path)
end