Commit 8e532be8 authored by Patrick Bajao's avatar Patrick Bajao

Make inapplicable_reason text to be reusable

We also need to display the inapplicable_reason text in another
place (`Gitlab::Suggestions::SuggestionSet`).

There's no need to return `inapplicable_reason` as symbol for now
so it was converted to return text instead.
parent cbf1b264
......@@ -43,12 +43,12 @@ class Suggestion < ApplicationRecord
def inapplicable_reason(cached: true)
strong_memoize("inapplicable_reason_#{cached}") do
next :applied if applied?
next :merge_request_merged if noteable.merged?
next :merge_request_closed if noteable.closed?
next :source_branch_deleted unless noteable.source_branch_exists?
next :outdated if outdated?(cached: cached) || !note.active?
next :same_content unless different_content?
next _("Can't apply this suggestion.") if applied?
next _("This merge request was merged. To apply this suggestion, edit this file directly.") if noteable.merged?
next _("This merge request is closed. To apply this suggestion, edit this file directly.") if noteable.closed?
next _("Can't apply as the source branch was deleted.") unless noteable.source_branch_exists?
next outdated_reason if outdated?(cached: cached) || !note.active?
next _("This suggestion already matches its content.") unless different_content?
end
end
......@@ -73,4 +73,12 @@ class Suggestion < ApplicationRecord
def different_content?
from_content != to_content
end
def outdated_reason
if single_line?
_("Can't apply as this line was changed in a more recent version.")
else
_("Can't apply as these lines were changed in a more recent version.")
end
end
end
......@@ -16,24 +16,8 @@ class SuggestionEntity < API::Entities::Suggestion
expose :inapplicable_reason do |suggestion|
next _("You don't have write access to the source branch.") unless can_apply?(suggestion)
next if suggestion.appliable?
case suggestion.inapplicable_reason
when :merge_request_merged
_("This merge request was merged. To apply this suggestion, edit this file directly.")
when :merge_request_closed
_("This merge request is closed. To apply this suggestion, edit this file directly.")
when :source_branch_deleted
_("Can't apply as the source branch was deleted.")
when :outdated
phrase = suggestion.single_line? ? 'this line was' : 'these lines were'
_("Can't apply as %{phrase} changed in a more recent version.") % { phrase: phrase }
when :same_content
_("This suggestion already matches its content.")
else
_("Can't apply this suggestion.")
end
suggestion.inapplicable_reason
end
private
......
......@@ -4134,10 +4134,13 @@ msgstr ""
msgid "Can override approvers and approvals required per merge request"
msgstr ""
msgid "Can't apply as %{phrase} changed in a more recent version."
msgid "Can't apply as the source branch was deleted."
msgstr ""
msgid "Can't apply as the source branch was deleted."
msgid "Can't apply as these lines were changed in a more recent version."
msgstr ""
msgid "Can't apply as this line was changed in a more recent version."
msgstr ""
msgid "Can't apply this suggestion."
......
......@@ -53,7 +53,7 @@ RSpec.describe Suggestion do
end
context 'when inapplicable_reason is not nil' do
let(:inapplicable_reason) { :applied }
let(:inapplicable_reason) { "Can't apply this suggestion." }
it { is_expected.to be_falsey }
end
......@@ -77,7 +77,7 @@ RSpec.describe Suggestion do
context 'when suggestion is already applied' do
let(:suggestion) { build(:suggestion, :applied, note: note) }
it { is_expected.to eq(:applied) }
it { is_expected.to eq("Can't apply this suggestion.") }
end
context 'when merge request was merged' do
......@@ -85,7 +85,7 @@ RSpec.describe Suggestion do
merge_request.mark_as_merged!
end
it { is_expected.to eq(:merge_request_merged) }
it { is_expected.to eq("This merge request was merged. To apply this suggestion, edit this file directly.") }
end
context 'when merge request is closed' do
......@@ -93,7 +93,7 @@ RSpec.describe Suggestion do
merge_request.close!
end
it { is_expected.to eq(:merge_request_closed) }
it { is_expected.to eq("This merge request is closed. To apply this suggestion, edit this file directly.") }
end
context 'when source branch is deleted' do
......@@ -101,23 +101,51 @@ RSpec.describe Suggestion do
merge_request.project.repository.rm_branch(merge_request.author, merge_request.source_branch)
end
it { is_expected.to eq(:source_branch_deleted) }
it { is_expected.to eq("Can't apply as the source branch was deleted.") }
end
context 'when content is outdated' do
before do
allow(suggestion).to receive(:outdated?).and_return(true)
context 'when outdated' do
shared_examples_for 'outdated suggestion' do
before do
allow(suggestion).to receive(:single_line?).and_return(single_line)
end
context 'and suggestion is for a single line' do
let(:single_line) { true }
it { is_expected.to eq("Can't apply as this line was changed in a more recent version.") }
end
context 'and suggestion is for multiple lines' do
let(:single_line) { false }
it { is_expected.to eq("Can't apply as these lines were changed in a more recent version.") }
end
end
it { is_expected.to eq(:outdated) }
context 'and content is outdated' do
before do
allow(suggestion).to receive(:outdated?).and_return(true)
end
it_behaves_like 'outdated suggestion'
end
context 'and note is outdated' do
before do
allow(note).to receive(:active?).and_return(false)
end
it_behaves_like 'outdated suggestion'
end
end
context 'when note is outdated' do
context 'when suggestion has the same content' do
before do
allow(note).to receive(:active?).and_return(false)
allow(suggestion).to receive(:different_content?).and_return(false)
end
it { is_expected.to eq(:outdated) }
it { is_expected.to eq("This suggestion already matches its content.") }
end
context 'when applicable' do
......
......@@ -36,87 +36,11 @@ RSpec.describe SuggestionEntity do
let(:can_apply_suggestion) { true }
before do
allow(suggestion).to receive(:appliable?).and_return(appliable)
allow(suggestion).to receive(:inapplicable_reason).and_return("Can't apply this suggestion.")
end
context 'and suggestion is appliable' do
let(:appliable) { true }
it 'returns nil' do
expect(inapplicable_reason).to be_nil
end
end
context 'but suggestion is not applicable' do
let(:appliable) { false }
before do
allow(suggestion).to receive(:inapplicable_reason).and_return(reason)
end
context 'and merge request was merged' do
let(:reason) { :merge_request_merged }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("This merge request was merged. To apply this suggestion, edit this file directly.")
end
end
context 'and source branch was deleted' do
let(:reason) { :source_branch_deleted }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("Can't apply as the source branch was deleted.")
end
end
context 'and merge request is closed' do
let(:reason) { :merge_request_closed }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("This merge request is closed. To apply this suggestion, edit this file directly.")
end
end
context 'and suggestion is outdated' do
let(:reason) { :outdated }
before do
allow(suggestion).to receive(:single_line?).and_return(single_line)
end
context 'and suggestion is for a single line' do
let(:single_line) { true }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("Can't apply as this line was changed in a more recent version.")
end
end
context 'and suggestion is for multiple lines' do
let(:single_line) { false }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("Can't apply as these lines were changed in a more recent version.")
end
end
end
context 'and suggestion has the same content' do
let(:reason) { :same_content }
it 'returns appropriate message' do
expect(inapplicable_reason).to eq("This suggestion already matches its content.")
end
end
context 'and suggestion is inapplicable for other reasons' do
let(:reason) { :some_other_reason }
it 'returns default message' do
expect(inapplicable_reason).to eq("Can't apply this suggestion.")
end
end
it 'returns the inapplicable reason' do
expect(inapplicable_reason).to eq(suggestion.inapplicable_reason)
end
end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment