Reputation: 1758
I have setup Public Activity to show notifications. Notifications are created with right recipient.
Now I want to send out email to notification recipient.
Normally I'd call the mailer on controller create. But on notifications controller I got only index with:
def index
@activities = PublicActivity::Activity.where(recipient_id: current_user.id).includes(:owner).includes(:trackable).all.reverse
end
And for instance on comment.rb I have:
include PublicActivity::Model
tracked
tracked owner: Proc.new{ |controller, model| controller.current_user }
tracked recipient: ->(controller, model) { model && model.commentable.user }
And on view comment_create
<li class="list-group-item">
<span class="glyphicon glyphicon-plus"></span>
<small class="text-muted"><%= a.created_at.strftime('%H:%M:%S %-d %B %Y') %></small><br/>
<strong><%= activity.owner ? activity.owner.name : current_user.name %></strong> commented
- <%= a.trackable.body %> - to
<%= link_to 'this item you posted.', poll_path(a.trackable.commentable_id) %>
</li>
I guess the creation of the notification is done internally by the gem.
Where should I call the mailer then?
Upvotes: 0
Views: 456
Reputation: 13014
Add new file config/initializers/activity.rb
module PublicActivity
class Activity < inherit_orm("Activity")
after_create :send_email
def send_email
# Your code to send mail based on activity's data goes here
end
end
end
Upvotes: 1
Reputation: 1951
You can put the mailer in a background job and then call that in the controller index.
Upvotes: -1