Class: LetterOpenerWeb::Letter
- Inherits:
-
Object
- Object
- LetterOpenerWeb::Letter
- Defined in:
- app/models/letter_opener_web/letter.rb
Instance Attribute Summary collapse
-
#id ⇒ Object
readonly
Returns the value of attribute id.
-
#sent_at ⇒ Object
readonly
Returns the value of attribute sent_at.
Class Method Summary collapse
- .destroy_all ⇒ Object
- .find(id) ⇒ Object
- .letters_location ⇒ Object
- .letters_location=(directory) ⇒ Object
- .search ⇒ Object
Instance Method Summary collapse
- #attachments ⇒ Object
- #default_style ⇒ Object
- #delete ⇒ Object
- #exists? ⇒ Boolean
-
#initialize(params) ⇒ Letter
constructor
A new instance of Letter.
- #plain_text ⇒ Object
- #rich_text ⇒ Object
- #to_param ⇒ Object
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
#id ⇒ Object (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_at ⇒ Object (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_all ⇒ Object
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_location ⇒ Object
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 |
.search ⇒ Object
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
#attachments ⇒ Object
52 53 54 55 56 |
# File 'app/models/letter_opener_web/letter.rb', line 52 def @attachments ||= Dir["#{base_dir}/attachments/*"].each_with_object({}) do |file, hash| hash[File.basename(file)] = File.(file) end end |
#default_style ⇒ Object
48 49 50 |
# File 'app/models/letter_opener_web/letter.rb', line 48 def default_style style_exists?('rich') ? 'rich' : 'plain' end |
#delete ⇒ Object
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
62 63 64 |
# File 'app/models/letter_opener_web/letter.rb', line 62 def exists? File.exist?(base_dir) end |
#plain_text ⇒ Object
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_text ⇒ Object
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_param ⇒ Object
44 45 46 |
# File 'app/models/letter_opener_web/letter.rb', line 44 def to_param id end |