Class: LetterOpenerWeb::Letter

Inherits:
BaseLetter show all
Defined in:
app/models/letter_opener_web/letter.rb

Instance Attribute Summary

Attributes inherited from BaseLetter

#id, #sent_at

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from BaseLetter

#default_style, find, #headers, #initialize, letters_location, #plain_text, #rich_text, #to_param

Constructor Details

This class inherits a constructor from LetterOpenerWeb::BaseLetter

Class Method Details

.destroy_allObject



12
13
14
# File 'app/models/letter_opener_web/letter.rb', line 12

def self.destroy_all
  FileUtils.rm_rf(letters_location)
end

.searchObject



5
6
7
8
9
10
# File 'app/models/letter_opener_web/letter.rb', line 5

def self.search
  letters = Dir.glob("#{letters_location}/*").map do |folder|
    new(id: File.basename(folder), sent_at: File.mtime(folder))
  end
  letters.sort_by(&:sent_at).reverse
end

Instance Method Details

#attachmentsObject



16
17
18
19
20
# File 'app/models/letter_opener_web/letter.rb', line 16

def attachments
  @attachments ||= Dir["#{base_dir}/attachments/*"].each_with_object({}) do |file, hash|
    hash[File.basename(file)] = File.expand_path(file)
  end
end

#deleteObject



22
23
24
25
26
# File 'app/models/letter_opener_web/letter.rb', line 22

def delete
  return unless valid?

  FileUtils.rm_rf(base_dir.to_s)
end

#valid?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/models/letter_opener_web/letter.rb', line 28

def valid?
  exists? && base_dir_within_letters_location?
end