Better email formatting for events

Andrew Cantino преди 11 години
родител
ревизия
7877ba23d4

+ 1 - 1
app/mailers/system_mailer.rb

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

+ 10 - 12
app/models/agents/digest_email_agent.rb

@@ -37,30 +37,28 @@ module Agents
37 37
 
38 38
     def check
39 39
       if self.memory[:queue] && self.memory[:queue].length > 0
40
-        lines = self.memory[:queue].map {|item| present(item) }
40
+        groups = self.memory[:queue].map { |payload| present(payload) }
41 41
         puts "Sending mail to #{user.email}..." unless Rails.env.test?
42
-        SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :lines => lines)
42
+        SystemMailer.delay.send_message(:to => user.email, :subject => options[:subject], :headline => options[:headline], :groups => groups)
43 43
         self.memory[:queue] = []
44 44
       end
45 45
     end
46 46
 
47
-    def present(item)
48
-      if item.is_a?(Hash)
47
+    def present(payload)
48
+      if payload.is_a?(Hash)
49
+        payload = ActiveSupport::HashWithIndifferentAccess.new(payload)
49 50
         MAIN_KEYS.each do |key|
50
-          if item.has_key?(key)
51
-            return "#{item[key]}" + ((item.length > 1 && item.length < 5) ? " (#{present_hash item, key})" : "")
52
-          elsif item.has_key?(key.to_s)
53
-            return "#{item[key.to_s]}" + ((item.length > 1 && item.length < 5) ? " (#{present_hash item, key.to_s})" : "")
54
-          end
51
+          return { :title => payload[key].to_s, :entries => present_hash(payload, key) } if payload.has_key?(key)
55 52
         end
56
-        present_hash item
53
+
54
+        { :title => "Event", :entries => present_hash(payload) }
57 55
       else
58
-        item.to_s
56
+        { :title => payload.to_s, :entries => [] }
59 57
       end
60 58
     end
61 59
 
62 60
     def present_hash(hash, skip_key = nil)
63
-      hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless [skip_key].include?(k) }.compact.to_sentence
61
+      hash.to_a.sort_by {|a| a.first.to_s }.map { |k, v| "#{k}: #{v}" unless k.to_s == skip_key.to_s }.compact
64 62
     end
65 63
   end
66 64
 end

+ 9 - 4
app/views/system_mailer/send_message.html.erb

@@ -7,10 +7,15 @@
7 7
     <% if @headline %>
8 8
       <h1><%= @headline %></h1>
9 9
     <% end %>
10
-    <% @lines.each do |line| %>
11
-      <p>
12
-        <%= line %>
13
-      </p>
10
+    <% @groups.each do |group| %>
11
+      <div style='margin-bottom: 10px;'>
12
+        <div><%= group[:title] %></div>
13
+        <% group[:entries].each do |entry| %>
14
+          <div style='margin-left: 10px;'>
15
+            <%= group[:entry] %>
16
+          </div>
17
+        <% end %>
18
+      </div>
14 19
     <% end %>
15 20
   </body>
16 21
 </html>

+ 4 - 2
app/views/system_mailer/send_message.text.erb

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

+ 5 - 2
spec/models/agents/digest_email_agent_spec.rb

@@ -33,13 +33,16 @@ describe Agents::DigestEmailAgent do
33 33
       Agents::DigestEmailAgent.async_check(@checker.id)
34 34
       ActionMailer::Base.deliveries.should == []
35 35
 
36
-      @checker.memory[:queue] = ["Something you should know about", { :title => "Foo", :url => "http://google.com", :bar => 2 }, { "message" => "hi", :woah => "there" }]
36
+      @checker.memory[:queue] = ["Something you should know about",
37
+                                 { :title => "Foo", :url => "http://google.com", :bar => 2 },
38
+                                 { "message" => "hi", :woah => "there" },
39
+                                 { "test" => 2 }]
37 40
       @checker.save!
38 41
 
39 42
       Agents::DigestEmailAgent.async_check(@checker.id)
40 43
       ActionMailer::Base.deliveries.last.to.should == ["bob@example.com"]
41 44
       ActionMailer::Base.deliveries.last.subject.should == "something interesting"
42
-      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)"
45
+      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"
43 46
       @checker.reload.memory[:queue].should == []
44 47
     end
45 48
   end