Rails Public Activity send email notification

452 views Asked by At

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?

2

There are 2 answers

1
theterminalguy On

You can put the mailer in a background job and then call that in the controller index.

1
kiddorails On

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