Sha256: 82c968d14b5686ca512e57a4562d61b52e2fad113bccf1a2a35b253e29549c9f

Contents?: true

Size: 893 Bytes

Versions: 2

Compression:

Stored size: 893 Bytes

Contents

module LetterOpenerWeb
  class Letter
    cattr_accessor :letters_location do
      Rails.root.join("tmp", "letter_opener")
    end

    attr_reader :id, :sent_at

    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

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

    def self.destroy_all
      FileUtils.rm_rf(letters_location)
    end

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

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

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

    def to_param
      id
    end

    private

    def read_file(style)
      File.read("#{letters_location}/#{id}/#{style}.html")
    end
  end
end

Version data entries

2 entries across 2 versions & 1 rubygems

Version Path
letter_opener_web-0.0.2 app/models/letter_opener_web/letter.rb
letter_opener_web-0.0.1 app/models/letter_opener_web/letter.rb