-1

We have a generic logging function that is in application.rb under our controllers. This function is not found by active job though (I'm assuming as because our email jobs extend ActiveJob::Base vs our controllers that reference ActionController which then references ActionController::Base)

Where would the right place be to put the logging function so we can keep are code as DRY as possible?

Hizqeel
  • 947
  • 3
  • 20
  • 23
CogitoErgoSum
  • 2,879
  • 5
  • 32
  • 45

1 Answers1

1

So after some talking with others the best course was decided by using the lib folder and creating a module within it.

We created a folder called trackers and a file called tracker.rb in it. Below is a basic snippet of what it looks like

module Trackers

  module_function 

  mattr_accessor :controller

  def track_action(event_name, event_params)

    event_params["time"]    ||= Time.now.utc.to_i

    # Controller scope only - this only gets executed if the function is called via a controller vs an ActiveJob
    if controller
      event_params["controller_name"]  ||=   controller_name
      event_params["action_name"]      ||=   action_name
    end

#Other stuff redacted

  end
end

Within the application.rb file we modified the code to include the folder as such:

config.autoload_paths += %W(
 #{config.root}/lib/
 #{config.root}/lib/trackers/
)

Within a method in a Controller or ActiveJob it is called as such -

  Trackers.track_action("eventName", {
    "someVar"     => "someValue",
  })

Another alternative was using a model but I felt this is much more of a lib function - we may turn it into a gem later on.

Hope this helps others in the future.

CogitoErgoSum
  • 2,879
  • 5
  • 32
  • 45