目前在我的观察者中,我根据分配给他们的徽章向用户添加点数。
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
如何根据被删除的徽章删除观察者中的这些点?我可以使用描述或类似的东西来匹配吗?从代码中可以清楚地看到在Observer上删除徽章时会发生什么。我想我可以在destroy方法的控制器级别上执行这些操作,但是删除正确数量的点的规则变得非常复杂。
编辑:我上面并不完全清楚,它是从控制器上的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)
else
user.subtract_points(0)
end
答案 0 :(得分: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