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