Better email formatting for events

This commit is contained in:
Andrew Cantino 2013-08-19 14:24:45 -07:00
parent 3efaed7e2a
commit 7877ba23d4
5 changed files with 29 additions and 21 deletions

View file

@ -2,7 +2,7 @@ class SystemMailer < ActionMailer::Base
default :from => ENV['EMAIL_FROM_ADDRESS'] || 'you@example.com'
def send_message(options)
@lines = options[:lines]
@groups = options[:groups]
@headline = options[:headline]
mail :to => options[:to], :subject => options[:subject]
end

View file

@ -37,30 +37,28 @@ module Agents
def check
if self.memory[:queue] && self.memory[:queue].length > 0
lines = self.memory[:queue].map {|item| present(item) }
groups = self.memory[:queue].map { |payload| present(payload) }
puts "Sending mail to #{user.email}..." unless Rails.env.test?
SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :lines => lines)
SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :groups => groups)
self.memory[:queue] = []
end
end
def present(item)
if item.is_a?(Hash)
def present(payload)
if payload.is_a?(Hash)
payload = ActiveSupport::HashWithIndifferentAccess.new(payload)
MAIN_KEYS.each do |key|
if item.has_key?(key)
return "#{item[key]}" + ((item.length > 1 && item.length < 5) ? " (#{present_hash item, key})" : "")
elsif item.has_key?(key.to_s)
return "#{item[key.to_s]}" + ((item.length > 1 && item.length < 5) ? " (#{present_hash item, key.to_s})" : "")
end
return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
end
present_hash item
{ :title => "Event", :entries => present_hash(payload) }
else
item.to_s
{ :title => payload.to_s, :entries => [] }
end
end
def present_hash(hash, skip_key = nil)
hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless [skip_key].include?(k) }.compact.to_sentence
hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
end
end
end

View file

@ -7,10 +7,15 @@
<% if @headline %>
<h1><%= @headline %></h1>
<% end %>
<% @lines.each do |line| %>
<p>
<%= line %>
</p>
<% @groups.each do |group| %>
<div style='margin-bottom: 10px;'>
<div><%= group[:title] %></div>
<% group[:entries].each do |entry| %>
<div style='margin-left: 10px;'>
<%= group[:entry] %>
</div>
<% end %>
</div>
<% end %>
</body>
</html>

View file

@ -1,5 +1,7 @@
<% if @headline %><%= @headline %>
<% end %><% @lines.each do |line| %><%= line %>
<% end %><% @groups.each do |group| %><%= group[:title] %>
<% group[:entries].each do |entry| %> <%= entry %>
<% end %>
<% end %>

View file

@ -33,13 +33,16 @@ describe Agents::DigestEmailAgent do
Agents::DigestEmailAgent.async_check(@checker.id)
ActionMailer::Base.deliveries.should == []
@checker.memory[:queue] = ["Something you should know about", { :title => "Foo", :url => "http://google.com", :bar => 2 }, { "message" => "hi", :woah => "there" }]
@checker.memory[:queue] = ["Something you should know about",
{ :title => "Foo", :url => "http://google.com", :bar => 2 },
{ "message" => "hi", :woah => "there" },
{ "test" => 2 }]
@checker.save!
Agents::DigestEmailAgent.async_check(@checker.id)
ActionMailer::Base.deliveries.last.to.should == ["bob@example.com"]
ActionMailer::Base.deliveries.last.subject.should == "something interesting"
get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Something you should know about\n\nFoo (bar: 2 and url: http://google.com)\n\nhi (woah: there)"
get_message_part(ActionMailer::Base.deliveries.last, /plain/).strip.should == "Something you should know about\n\nFoo\n bar: 2\n url: http://google.com\n\nhi\n woah: there\n\nEvent\n test: 2"
@checker.reload.memory[:queue].should == []
end
end