2016-10-27 4 views
0

現在、オブザーバーに割り当てられているバッジに基づいて、ユーザーにポイントを追加します。メリット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メソッドのコントローラーレベルでこれらを行うことができたと思うが、ルールは本当に複雑になり、正しい数のポイントを削除する。

EDIT:コントローラーの破棄メソッドからバッジが削除されたときに、一時的なバッジが削除されたときではなく、完全にクリアされていませんでした。

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

私はこの方法を見つけましたが、それが最良の方法かどうかはわかりません。 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 
関連する問題