0

目前在我的观察者中,我根据分配给用户的徽章向用户添加积分。

class NewBadgeObserver
  def update(changed_data)
    return unless changed_data[:merit_object].is_a?(Merit::BadgesSash)

    description = changed_data[:description]

    user = User.where(sash_id: changed_data[:sash_id]).first
    badge = changed_data[:merit_object].badge
    granted_at = changed_data[:granted_at]

    case badge.name
    when "added-1-observation", "added-1-issue", "added-observation-at-same-location"
      user.add_points(10)
    when "added-10-observations", "added-10-issues", "added-10-observations-at-same-location"
      user.add_points(100)
    when "added-25-observations", "added-25-issues", "added-25-observations-at-same-location"
      user.add_points(250)
    when "added-50-observations", "added-50-issues", "added-50-observations-at-same-location", "resolved-an-issue"
      user.add_points(500)
    when "added-100-observations", "added-100-issues", "added-100-observations-at-same-location"
      user.add_points(1000)
    else
      user.add_points(0)
    end

  end
end

根据被移除的徽章,我将如何在观察者中移除这些点?我可以使用描述或类似的东西来匹配吗?从代码中并不清楚在观察者上移除徽章时会发生什么。我想我可以在销毁方法的控制器级别上执行这些操作,但是规则变得非常复杂,以删除正确数量的点。

编辑:我在上面并不完全清楚,它是从控制器上的销毁方法中删除徽章时,而不是临时徽章被删除时。

case description
  when "removed added-1-observation badge", "removed added-1-issue badge", "removed added-observation-at-same-location badge"
    user.subtract_points(10)
  when "removed added-10-observations badge", "removed added-10-issues badge", "removed added-10-observations-at-same-location badge"
    user.subtract_points(100)
  when "removed added-25-observations badge", "removed added-25-issues badge", "removed added-25-observations-at-same-location badge"
    user.subtract_points(250)
  when "removed added-50-observations badge", "removed added-50-issues badge", "removed added-50-observations-at-same-location badge", "removed resolved-an-issue badge"
    user.subtract_points(500)
  when "removed added-100-observations badge", "removed added-100-issues badge", "removed added-100-observations-at-same-location badge"
    user.subtract_points(1000)
  else
    user.subtract_points(0)
end
4

1 回答 1

0

所以我找到了一种方法,但不确定它是否是最好的方法!我使用 Merit::Action 来获取目标模型和操作。

merit_action = Merit::Action.find changed_data[:merit_action_id]
controller = merit_action.target_model
action = merit_action.action_method

if controller == "locations" && action == "destroy"
    case description
    when "removed added-1-observation badge", "removed added-1-issue badge", "removed added-observation-at-same-location badge"
    user.subtract_points(10)
    when "removed added-10-observations badge", "removed added-10-issues badge", "removed added-10-observations-at-same-location badge"
    user.subtract_points(100)
    when "removed added-25-observations badge", "removed added-25-issues badge", "removed added-25-observations-at-same-location badge"
    user.subtract_points(250)
    when "removed added-50-observations badge", "removed added-50-issues badge", "removed added-50-observations-at-same-location badge", "removed resolved-an-issue badge"
    user.subtract_points(500)
    when "removed added-100-observations badge", "removed added-100-issues badge", "removed added-100-observations-at-same-location badge"
    user.subtract_points(1000)
    end
else
    case badge.name
    when "added-1-observation", "added-1-issue", "added-observation-at-same-location"
    user.add_points(10)
    when "added-10-observations", "added-10-issues", "added-10-observations-at-same-location"
    user.add_points(100)
    when "added-25-observations", "added-25-issues", "added-25-observations-at-same-location"
    user.add_points(250)
    when "added-50-observations", "added-50-issues", "added-50-observations-at-same-location", "resolved-an-issue"
    user.add_points(500)
    when "added-100-observations", "added-100-issues", "added-100-observations-at-same-location"
    user.add_points(1000)
    end
end
于 2016-10-27T20:57:52.033 回答