def perform(user = nil)
start_date = 1.week.ago.beginning_of_week
end_date = Time.current.end_of_week
- bookmarks = Post.bookmarks.where(created_at: start_date..end_date)
+ bookmarks = Post.where(created_at: start_date..end_date)
# or do User.find_each do |user| for everyone, but that'll break fastmail
if user.present?
DigestMailer.weekly_bookmarks_digest(user, bookmarks).deliver_now
def weekly_bookmarks_digest(user, bookmarks)
@user = user
@bookmarks = bookmarks
- mail(to: @user.email, subject: 'mind reader :: weekly bookmarks digest')
+ mail(to: @user.email, subject: 'mind reader :: weekly digest')
end
end
-<h1>Your weekly bookmarks digest</h1>
+<p>Dear <%= @user.first_name %>,</p>
-<p>Hello <%= @user.first_name %>,</p>
+<p>Here's what's new from mind reader this week:</p>
-<p>Here are the bookmarks added this week:</p>
+<% @bookmarks.group_by { |bookmark| bookmark.created_at.to_date }.each do |date, bookmarks| %>
+ <p>On <%= date.strftime('%B %e, %Y') %>:</p>
-<ul>
- <% @bookmarks.each do |bookmark| %>
- <li>
- <strong><%= link_to bookmark.title, bookmark.url %></strong>
- <% if bookmark.content.present? %>
- <br><em><%= bookmark.content %></em>
- <% end %>
- </li>
- <% end %>
-</ul>
+ <ul>
+ <% bookmarks.each do |bookmark| %>
+ <li>
+ <% if bookmark.url.present? %>
+ <strong><a href="<%= bookmark.url %>" target="_blank"><%= bookmark.title %></a></strong>
+ <% else %>
+ <strong><%= bookmark.title %></strong>
+ <% end %>
+ <% if bookmark.content.present? %>
+ <br><em><%= bookmark.content %></em>
+ <% end %>
+ <% if bookmark.created_at.present? %>
+ <br><small>Added at <%= bookmark.created_at.strftime('%l:%M%P') %></small>
+ <% end %>
+ </li>
+ <% end %>
+ </ul>
+<% end %>
<p>Have a great weekend,<br>
-Aidan.</p>
\ No newline at end of file
+Aidan.</p>