Jon
Jon

Reputation: 179

Use model callbacks or observers for implementing activity feed?

I'm implementing an activity feed for a client similar to Twitter's (it's only activity that pertains to the current signed in user -- i.e. who favorite his/her post, mentions, etc..).. It won't rely on 'push' but instead, the user will have to refresh the page in order to see new activity (for now).

I've been googling & searching around SO for the past hour to find the best way to implement this, and observers keep coming up in the solutions. I also notice that many of these are using push notification. I noticed the approach R Bates took in his public activity railscast btw, which is why I'm asking this question.

What if I don't want to use push notification, would callbacks be ok or even better? Do you think I would still need to use implement other things outside rails for scalability? (like how you may use "Pushapp" for push notifications)

Any suggestions on better solutions or light shed would be helpful.


This is for @gg_s

I'm assuming in this case you're saying I have an activity_feed table (receiver_id, sender_id, activity_type, & activity_id) (belongs to user, belongs_to activity_type (???), :polymorphic => true)

# application_controller.rb

def publish_to_user_feed(message)
  current_user.activity_feed << message
end

# favorites_controller.rb

def create
  # blah blah blah
  publish_to_user_feed "This just happened."
end

In the the favorites_controller's 'create' action, "This just happened" could == "@favorite.user just favorited @favorite.post by @favorite.post.user"

Again, I hope I'm not being too pesky & am pretty sure what I'm asking is obvious, but I think this will help clear things up for me & also help future visitors.

Thanks again


For anyone that wants to know, I'm still working on this.. Just took a little break.. My main concern is how heavy it'll be on the db & other performance issues so if anyone wants to better this (using the code above), feel free :)


Solution: I don't want to overcomplicate things so I'm taking ap's advice.

Upvotes: 0

Views: 431

Answers (1)

Substantial
Substantial

Reputation: 6682

Use neither.

Callbacks and observers are more complex in this case than you might think. The only automation they provide is the ability to be triggered upon model events. That's it. You are responsible to implement logic determining:

  • what just happened?
  • should it be reported?
  • what to report?

Extending this logic to support several types of activities is needlessly complex. Ditch the automation and publish activities from the controller as they happen on an as-needed basis.

Create a helper method to keep things DRY:

# application_controller.rb

def publish_to_user_feed(message)
  current_user.activity_feed << message
end

Then manually post to a user's feed when and where necessary:

# some_controller.rb

def some_action
  # perform some action
  publish_to_user_feed "This just happened."
end

Reporting directly from the controller is clear, readable, DRY, maintainable, and adhere's to Rails' MVC pattern. No complex callback chains or observers to write.

As a bonus, it is trivial to perform activities without posting to activity feeds, e.g. administrative activity or user privacy settings.

Upvotes: 1

Related Questions