Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add timezone support #81

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,12 +54,16 @@ config.action_mailer.delivery_method = :letter_opener_web
config.action_mailer.delivery_method = ENV['USER'] == 'vagrant' ? :letter_opener_web : :letter_opener
```

If you're using `:letter_opener_web` as your delivery method, you can change the location of the letters by adding the
If you're using `:letter_opener_web` as your delivery method and you want to customize some options then you can add the
following to an initializer (or in development.rb):

```ruby
LetterOpenerWeb.configure do |config|
# change the location of the letters
config.letters_location = Rails.root.join('your', 'new', 'path')

# set to true if you want to use the timezone of the application
config.letters_in_time_zone = false
end
```

Expand Down
39 changes: 33 additions & 6 deletions app/models/letter_opener_web/letter.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

module LetterOpenerWeb
class Letter
attr_reader :id, :sent_at
attr_reader :id

def self.letters_location
@letters_location ||= LetterOpenerWeb.config.letters_location
Expand All @@ -15,7 +15,7 @@ def self.letters_location=(directory)

def self.search
letters = Dir.glob("#{LetterOpenerWeb.config.letters_location}/*").map do |folder|
new(id: File.basename(folder), sent_at: File.mtime(folder))
new(id: File.basename(folder))
end
letters.sort_by(&:sent_at).reverse
end
Expand All @@ -24,21 +24,30 @@ def self.find(id)
new(id: id)
end

def sent_at
@sent_at ||= begin
if LetterOpenerWeb.config.letters_in_time_zone && mtime.respond_to?(:in_time_zone)
mtime.in_time_zone(Rails.configuration.time_zone)
else
mtime
end
end
end

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

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

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

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

def to_param
Expand Down Expand Up @@ -69,6 +78,10 @@ def base_dir
"#{LetterOpenerWeb.config.letters_location}/#{id}"
end

def mtime
@mtime ||= File.mtime(base_dir) if exists?
end

def read_file(style)
File.read("#{base_dir}/#{style}.html")
end
Expand All @@ -77,6 +90,10 @@ def style_exists?(style)
File.exist?("#{base_dir}/#{style}.html")
end

def adjust_contents(contents)
adjust_link_targets(adjust_date(contents))
end

def adjust_link_targets(contents)
# We cannot feed the whole file to an XML parser as some mails are
# "complete" (as in they have the whole <html> structure) and letter_opener
Expand Down Expand Up @@ -104,5 +121,15 @@ def fix_link_html(link_html)
end
end
end

def adjust_date(contents)
message_headers = contents.scan(%r{<div\sid="message_headers">(?:.|\s)*?</div>}).first
if message_headers.present?
xml = REXML::Document.new(message_headers).root
sent_at_element = xml.elements['dl/dd[4]']
contents.gsub!(sent_at_element.to_s, "<dd>#{sent_at}</dd>") if sent_at_element.present?
end
contents
end
end
end
3 changes: 2 additions & 1 deletion lib/letter_opener_web.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,13 @@

module LetterOpenerWeb
class Config
attr_accessor :letters_location
attr_accessor :letters_location, :letters_in_time_zone
end

def self.config
@config ||= Config.new.tap do |conf|
conf.letters_location = Rails.root.join('tmp', 'letter_opener')
conf.letters_in_time_zone = false
end
end

Expand Down