0

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?

catmal
  • 1,728
  • 1
  • 16
  • 32
  • `after_save` is a model callback method, not controller. Controller apis is in form of actions - `before_action` and `after_action`. Depends on how you have written logic for notifications#create, you need to place your mailer logic accordingly. It's unclear what you are precisely looking for. – kiddorails Nov 30 '17 at 18:49
  • Yes the problem is that as I am using a gem to create notifications I don't have actions like create on controller. I don't have a notification model either. I haven't created mysel the logic to create notifications. Hence my confusion. – catmal Dec 01 '17 at 02:02
  • Which gem are you using, if I may ask? – kiddorails Dec 01 '17 at 05:10
  • Public Activity as stated on question title and question. – catmal Dec 01 '17 at 05:16
  • you can patch `Activity` - https://github.com/chaps-io/public_activity/blob/1-5-stable/lib/public_activity/models/activity.rb and add `after_create` there where you send mail. This will ensure that after each activity is created, you are at right location to send the notification. – kiddorails Dec 01 '17 at 10:06
  • try something like https://stackoverflow.com/a/47591012/1376448 – kiddorails Dec 01 '17 at 10:08

2 Answers2

1

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
kiddorails
  • 12,961
  • 2
  • 32
  • 41
  • This stopped working for me in upgrading to Rails 5.2. It still works when manually creating/saving a `PublicActivity::Activity` object from the console. – Micah Jan 09 '19 at 16:10
-1

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

theterminalguy
  • 1,842
  • 18
  • 20
  • I have tried to understand this. But I don't get how can I trigger the mailer when a comment is created for instance, by calling the job on index. – catmal Dec 01 '17 at 02:21