繁体   English   中英

在Merit Rails Gem中使用Observer移除点

[英]Removal of points using Observer in Merit Rails Gem

当前,在我的观察员中,我根据分配给用户的徽章向用户添加点。

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

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

编辑:上面我还不太清楚,从控制器上的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

因此,我找到了一种执行此操作的方法,但是不确定是否是最佳方法! 我使用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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM