0

i'm on the way of redesigning my activity feed, i already implemented the logic with redis and rails (wich works great by the way) but i'm still unsure how to create/trigger the events.

In my first approach i used observer, which had the downside of not having current_user available. and, using observer is a bad idea anyways :)

My preferred method would be to create/trigger the events in the controller, which should look sth like:

class UserController < LocationController
  def invite
    ...
    if user.save
      trigger! UserInvitedEvent, {creator: current_user, ...}, :create
      ....
    end
  end
end

The trigger method should

  • create the UserInvitedEvent with some params. (:create can be default option)
  • could be deactivate (e.g. deactivate for testing)
  • could be executed with e.g. resque

i looked in some gems (fnordmetrics, ...) but i could not find a slick implementation for that.

fluxsaas
  • 997
  • 2
  • 11
  • 27

2 Answers2

1

I'd build something like the following:

# config/initializers/event_tracking.rb
modlue EventTracking

  attr_accessor :enabled

  def enable
    @enabled = true
  end

  def disable
    @enabled = false
  end

  module_function

  def Track(event, options)
    if EventTracking.enabled
      event.classify.constantize.new(options)
    end
  end

end

include EventTracking
EventTracking.enable unless Rails.env.test?

The module_function hack let's us have the Track() function globally, and exports it to the global namespace, you (key thing is that the method is copied to the global scope, so it's effectively global, read more here: http://www.ruby-doc.org/core-1.9.3/Module.html#method-i-module_function)

Then we enable tracking for all modes except production, we call event.classify.constantize in Rails that should turn something like :user_invited_event into UserInvitedEvent, and offers the possibility of namespacing, for example Track(:'users/invited'). The semantics of this are defined by ActiveSupport's inflection module.

I think that should be a decent start to your tracking code I've been using that in a project with a lot of success until now!

Lee Hambley
  • 6,270
  • 5
  • 49
  • 81
1

With the (new) rails intrumentation and ActiveSupport::Notifications system you can completely decouple the notification and the actual feed construction.

See http://railscasts.com/episodes/249-notifications-in-rails-3?view=asciicast

rewritten
  • 16,280
  • 2
  • 47
  • 50
  • yeah, i thought about that too. i gonna check it out. – fluxsaas Oct 01 '12 at 13:52
  • I'd recomment against AS:Notifications, they're an integral part of Rails, and because of that their usage might change in the future, it would be foolish to bind an application function as important as Event Tracking to the Raisl internal notification system. – Lee Hambley Oct 01 '12 at 14:44
  • @Beaks so you'd recommend against ActiveRecord because it's their internal ORM? Or against using the Rails.cache abstraction over memcached because it's provided by rails? – rewritten Oct 01 '12 at 16:06