BigW Consortium Gitlab

toggle_award_emoji.rb 693 Bytes
Newer Older
1 2 3 4
module ToggleAwardEmoji
  extend ActiveSupport::Concern

  def toggle_award_emoji
5
    authenticate_user!
6
    name = params.require(:name)
ZJ van de Weg committed
7

8 9
    if awardable.user_can_award?(current_user, name)
      awardable.toggle_award_emoji(name, current_user)
10

11 12
      todoable = to_todoable(awardable)
      TodoService.new.new_award_emoji(todoable, current_user) if todoable
13

14 15 16 17
      render json: { ok: true }
    else
      render json: { ok: false }
    end
18 19 20 21
  end

  private

22 23 24 25
  def to_todoable(awardable)
    case awardable
    when Note
      awardable.noteable
26
    when MergeRequest, Issue
27
      awardable
28 29
    when Snippet
      nil
30 31 32
    end
  end

33 34 35 36
  def awardable
    raise NotImplementedError
  end
end