0

Currently in my Observer I add points to Users based on the badges that are assigned to them.

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

How would I go about removing these points in the Observer based on the badge being removed? Could I use the description or something similar to match against? It's not really clear from the code what happens when a badge is removed on the Observer. I guess I could do these on a controller level on the destroy method but the rules are getting really complex to remove the correct number of points.

EDIT: I wasn't totally clear above, its when a badge is removed from a destroy method on the controller, not when temporary badges are removed.

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
olliekav
  • 165
  • 3
  • 8

1 Answers1

0

So I found one way of doing this, not sure if it's the best way though! I use the Merit::Action to get the target model and 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
olliekav
  • 165
  • 3
  • 8