2

我正在使用轨道的功绩宝石,并希望有一个声誉变化的时间表,获得的徽章等。例如

  • +1 2 月 22 日 PostTitle 投票赞成
  • +3 2 月 22 日帖子标题收藏
  • -1 22Feb PostTitle 投了反对票……等等。

根据自述文件,我创建了以下内容:

配置/初始化程序/merit.rb

config.add_observer 'ReputationChangeObserver'

信誉变化观察者.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

问题是,接下来呢?如何使用观察者显示 current_user.changed_data 的时间线?

4

1 回答 1

2

API 现在似乎有点不舒服,无论如何这里的示例代码适用于当前的优点大师(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

编辑:请注意,自此答案以来 API 已更改,请参阅https://github.com/tute/merit#getting-notifications

于 2014-02-26T21:47:01.670 回答