Commit 0c82bf04 authored by Gary Holtz's avatar Gary Holtz

Adds a great refactoring idea and clarified specs

parent 6bfbc3c6
...@@ -80,20 +80,19 @@ class DiffsEntity < Grape::Entity ...@@ -80,20 +80,19 @@ class DiffsEntity < Grape::Entity
private private
def commit_ids def commit_ids
strong_memoize(:commit_ids) do @commit_ids ||= merge_request.commits.collect(&:id)
[nil] + merge_request.commits.collect(&:id) + [nil]
end
end end
def commit_with_neighbors(id) def commit_neighbors(commit_id)
commits = commit_ids.each_cons(3).find { |prev_commit, commit, next_commit| commit == id } index = commit_ids.index(commit_id)
{ prev_commit_id: commits.first, next_commit_id: commits.last } if commits
return [] unless index
[(index > 0 ? commit_ids[index - 1] : nil), commit_ids[index + 1]]
end end
def commit_options(options) def commit_options(options)
neighbors = commit_with_neighbors(options[:commit]&.id) prev_commit_id, next_commit_id = *commit_neighbors(options[:commit]&.id)
prev_commit_id = neighbors&.dig(:prev_commit_id)
next_commit_id = neighbors&.dig(:next_commit_id)
options.merge( options.merge(
type: :full, type: :full,
......
...@@ -29,21 +29,50 @@ describe DiffsEntity do ...@@ -29,21 +29,50 @@ describe DiffsEntity do
context "when a commit_id is passed" do context "when a commit_id is passed" do
let(:commits) { [nil] + merge_request.commits + [nil] } let(:commits) { [nil] + merge_request.commits + [nil] }
let(:commit) { commits.compact.sample }
let(:entity) do let(:entity) do
described_class.new(merge_request_diffs.first.diffs, request: request, merge_request: merge_request, merge_request_diffs: merge_request_diffs, commit: commit) described_class.new(
merge_request_diffs.first.diffs,
request: request,
merge_request: merge_request,
merge_request_diffs: merge_request_diffs,
commit: @commit
)
end end
subject { entity.as_json }
context "when the passed commit is not the first or last in the group" do
it 'includes commit references for previous and next' do it 'includes commit references for previous and next' do
expect(subject[:commit]).to include(:prev_commit_id, :next_commit_id) @commit = commits.third
check_neighbor_commits(commits, @commit)
end
end
context "when the passed commit is the first in the group" do
it 'includes commit references for nil and next' do
@commit = commits.compact.last
check_neighbor_commits(commits, @commit)
end
end
context "when the passed commit is the last in the group" do
it 'includes commit references for previous and nil' do
@commit = commits.compact.first
check_neighbor_commits(commits, @commit)
end
end
end
end
private
def check_neighbor_commits(commits, commit)
index = commits.index(commit) index = commits.index(commit)
prev_commit = commits[index - 1]&.id prev_commit = commits[index - 1]&.id
next_commit = commits[index + 1]&.id next_commit = commits[index + 1]&.id
expect(subject[:commit]).to include(:prev_commit_id, :next_commit_id)
expect(subject[:commit][:prev_commit_id]).to eq(prev_commit) expect(subject[:commit][:prev_commit_id]).to eq(prev_commit)
expect(subject[:commit][:next_commit_id]).to eq(next_commit) expect(subject[:commit][:next_commit_id]).to eq(next_commit)
end end
end
end
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