2018-12-13 14:17:19 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
class SuggestionEntity < API::Entities::Suggestion
|
|
|
|
include RequestAwareEntity
|
2020-07-03 17:09:31 -04:00
|
|
|
include Gitlab::Utils::StrongMemoize
|
2018-12-13 14:17:19 -05:00
|
|
|
|
2019-04-04 09:08:34 -04:00
|
|
|
unexpose :from_line, :to_line, :from_content, :to_content
|
2019-12-24 04:07:44 -05:00
|
|
|
expose :diff_lines, using: DiffLineEntity do |suggestion|
|
|
|
|
Gitlab::Diff::Highlight.new(suggestion.diff_lines).highlight
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
expose :current_user do
|
|
|
|
expose :can_apply do |suggestion|
|
2020-07-03 17:09:31 -04:00
|
|
|
can_apply?(suggestion)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
expose :inapplicable_reason do |suggestion|
|
|
|
|
next _("You don't have write access to the source branch.") unless can_apply?(suggestion)
|
|
|
|
|
2020-07-20 14:09:27 -04:00
|
|
|
suggestion.inapplicable_reason
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|
|
|
|
|
|
|
|
private
|
|
|
|
|
|
|
|
def current_user
|
|
|
|
request.current_user
|
|
|
|
end
|
2020-07-03 17:09:31 -04:00
|
|
|
|
|
|
|
def can_apply?(suggestion)
|
|
|
|
strong_memoize(:can_apply) do
|
|
|
|
Ability.allowed?(current_user, :apply_suggestion, suggestion)
|
|
|
|
end
|
|
|
|
end
|
2018-12-13 14:17:19 -05:00
|
|
|
end
|