Refactor `Todo#target`

parent 2a8858ca
...@@ -61,14 +61,10 @@ class Todo < ActiveRecord::Base ...@@ -61,14 +61,10 @@ class Todo < ActiveRecord::Base
# override to return commits, which are not active record # override to return commits, which are not active record
def target def target
if for_commit? if for_commit?
project.commit(commit_id) project.commit(commit_id) rescue nil
else else
super super
end end
# Temp fix to prevent app crash
# if note commit id doesn't exist
rescue
nil
end end
def target_reference def target_reference
......
...@@ -99,7 +99,8 @@ describe Todo, models: true do ...@@ -99,7 +99,8 @@ describe Todo, models: true do
end end
describe '#target' do describe '#target' do
it 'returns an instance of Commit for commits' do context 'for commits' do
it 'returns an instance of Commit when exists' do
subject.project = project subject.project = project
subject.target_type = 'Commit' subject.target_type = 'Commit'
subject.commit_id = commit.id subject.commit_id = commit.id
...@@ -108,6 +109,15 @@ describe Todo, models: true do ...@@ -108,6 +109,15 @@ describe Todo, models: true do
expect(subject.target).to eq commit expect(subject.target).to eq commit
end end
it 'returns nil when does not exists' do
subject.project = project
subject.target_type = 'Commit'
subject.commit_id = 'xxxx'
expect(subject.target).to be_nil
end
end
it 'returns the issuable for issuables' do it 'returns the issuable for issuables' do
subject.target_id = issue.id subject.target_id = issue.id
subject.target_type = issue.class.name subject.target_type = issue.class.name
......
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