2

I'm using the merit gem for rails, and would like to have a timeline of reputation changes, badges earned etc. For example

  • +1 22Feb PostTitle voted up
  • +3 22Feb PostTitle favorited
  • -1 22Feb PostTitle voted down ....and so on.

Based on the readme, I created the following:

config/initializers/merit.rb

config.add_observer 'ReputationChangeObserver'

reputation_change_observer.rb

class ReputationChangeObserver
  def update(changed_data)
    # `changed_data[:description]` holds information on what changed
    # badges granted or removed, points changed.

    # `changed_data[:merit_object]` reputation related object created by merit.
    # It responds to `sash_id` and `sash`. From there you can get to your
    # application object that had it's reputation changed, for example:
    # sash_id = changed_data[:merit_object].sash_id
    # User.where(sash_id: sash_id).first

    # You may use this to fill a timeline with notifications for users, send
    # emails, etc.

  end
end

Question is, what next? How do I use the observer to display a timeline of the current_user.changed_data?

Ryan.lay
  • 1,741
  • 2
  • 17
  • 30

1 Answers1

2

API seems a little uncomfortable for now, anyway here's sample code that works in current merit master (1515c6463f92aaace6298015c0f8e70064885779):

class ReputationChangeObserver
  def update(changed_data)
    # description will be something like:
    #   granted 5 points
    #   granted just-registered badge
    #   removed autobiographer badge
    description = changed_data[:description]

    # If user is your meritable model, you can grab it like:
    if changed_data[:merit_object]
      sash_id = changed_data[:merit_object].sash_id
      user = User.where(sash_id: sash_id).first
    end

    # To know where and when it happened:
    merit_action = Merit::Action.find changed_data[:merit_action_id]
    controller = merit_action.target_model
    action = merit_action.action_method
    when = merit_action.created_at

    # From here on, you can create a new Notification assuming that's an
    # ActiveRecord Model in your app, send an email, etc. For example:
    Notification.create(
      user: user,
      what: description,
      where: "#{controller}##{action}",
      when: when)
  end
end

EDIT: Please note that API has changed since this answer, see https://github.com/tute/merit#getting-notifications.

TuteC
  • 4,342
  • 30
  • 40
  • Thank tute! Love the gem. Just to be clear, I'll have to create a Notification model and migration with user_id, what, where and when columns? – Ryan.lay Feb 27 '14 at 08:48
  • I think to implement a notifications system as you describe in your post you'll need a migration along those lines: `rails g migration notifications user_id:integer, what, where, when:datetime`. Best! – TuteC Feb 27 '14 at 13:27
  • ok, will give it a go and post up the complete code when I get it working. Thanks a lot! – Ryan.lay Mar 01 '14 at 04:12
  • Is it possible that this is not triggered when using the manual add_points and subtract_points? – Danny Mar 02 '14 at 14:25
  • Yes, that should be clear from the docs: `add_points` and `subtract_points` doesn't call the observer. – TuteC Mar 02 '14 at 18:01