Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to add attribute to existing Notifications payload?

In Rails notifications, I am subscribing to "process_action.action_controller", and would like to add more attributes to the payload. How can I do that?

I have tried using append_info_to_payload, but this seems to do nothing.

module AppendExceptionPayload
  module ControllerRuntime
    extend ActiveSupport::Concern

    protected 

    def append_info_to_payload(payload)
      super
      payload[:happy] = "HAPPY"
    end
  end
end

The subscription and above code is in a Rails engine, so this is where I make the call to add it:

require 'append_exception_payload'

module Instrument
  class Engine < ::Rails::Engine

    ActiveSupport.on_load :action_controller do
      include AppendExceptionPayload::ControllerRuntime
    end

  end
end
like image 571
Carson Cole Avatar asked Nov 29 '12 19:11

Carson Cole


2 Answers

After putting up the bounty, I found a solution myself. Rails handles this really cleanly.

Basically, the append_info_to_payload method is meant exactly for this.

So to include session information and signed_in user information I added this to my application_controller.rb:

def append_info_to_payload(payload)
    super
    payload[:session] = request.session_options[:id] rescue ""
    payload[:user_id] = session[:user_id] rescue "unknown"
end
like image 86
vvohra87 Avatar answered Nov 19 '22 11:11

vvohra87


So i jumped in and had a look at the api for the process_action method (private) and the append_info_to_payload instance method (public) and the proccess action method seems to call append_info_to_payload in its code like so:

ActiveSupport::Notifications.instrument("process_action.action_controller", raw_payload) do |payload|
    result = super
    payload[:status] = response.status
    append_info_to_payload(payload)
    result
  end

and append_info_to_payload works something like this

def append_info_to_payload(payload) #:nodoc:
  payload[:view_runtime] = view_runtime
end

I can suggest trying payload[:view_runtime] instead of payload[:happy] or trying to use payload[:status]

Let me know how you get on and I will try help more, unfortunately there is really no documentation for this stuff.

like image 2
Jesse Whitham Avatar answered Nov 19 '22 11:11

Jesse Whitham