Class: LetterOpenerWeb::Letter

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

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params) ⇒ Letter

Returns a new instance of Letter.



31
32
33
34
# File 'app/models/letter_opener_web/letter.rb', line 31

def initialize(params)
  @id      = params.fetch(:id)
  @sent_at = params[:sent_at]
end

Instance Attribute Details

#idObject (readonly)

Returns the value of attribute id.



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

def id
  @id
end

#sent_atObject (readonly)

Returns the value of attribute sent_at.



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

def sent_at
  @sent_at
end

Class Method Details

.destroy_allObject



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

def self.destroy_all
  FileUtils.rm_rf(LetterOpenerWeb.config.letters_location)
end

.find(id) ⇒ Object



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

def self.find(id)
  new(id: id)
end

.letters_locationObject



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

def self.letters_location
  @letters_location ||= LetterOpenerWeb.config.letters_location
end

.letters_location=(directory) ⇒ Object



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

def self.letters_location=(directory)
  LetterOpenerWeb.configure { |config| config.letters_location = directory }
  @letters_location = nil
end

.searchObject



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

def self.search
  letters = Dir.glob("#{LetterOpenerWeb.config.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



52
53
54
55
56
# File 'app/models/letter_opener_web/letter.rb', line 52

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

#default_styleObject



48
49
50
# File 'app/models/letter_opener_web/letter.rb', line 48

def default_style
  style_exists?('rich') ? 'rich' : 'plain'
end

#deleteObject



58
59
60
# File 'app/models/letter_opener_web/letter.rb', line 58

def delete
  FileUtils.rm_rf("#{LetterOpenerWeb.config.letters_location}/#{id}")
end

#exists?Boolean

Returns:

  • (Boolean)


62
63
64
# File 'app/models/letter_opener_web/letter.rb', line 62

def exists?
  File.exist?(base_dir)
end

#plain_textObject



36
37
38
# File 'app/models/letter_opener_web/letter.rb', line 36

def plain_text
  @plain_text ||= adjust_link_targets(read_file(:plain))
end

#rich_textObject



40
41
42
# File 'app/models/letter_opener_web/letter.rb', line 40

def rich_text
  @rich_text ||= adjust_link_targets(read_file(:rich))
end

#to_paramObject



44
45
46
# File 'app/models/letter_opener_web/letter.rb', line 44

def to_param
  id
end