Commit 39fc0db3 authored by Marc Schwede's avatar Marc Schwede
parent 7a31cfe9
...@@ -47,11 +47,12 @@ class TodosFinder ...@@ -47,11 +47,12 @@ class TodosFinder
sort(items) sort(items)
end end
# Returns `true` if the current user has any todos for the given target. # Returns `true` if the current user has any todos for the given target with the optional given state.
# #
# target - The value of the `target_type` column, such as `Issue`. # target - The value of the `target_type` column, such as `Issue`.
def any_for_target?(target) # state - The value of the `state` column, such as `pending` or `done`.
current_user.todos.any_for_target?(target) def any_for_target?(target, *state)
current_user.todos.any_for_target?(target, state)
end end
private private
......
...@@ -89,11 +89,12 @@ class Todo < ApplicationRecord ...@@ -89,11 +89,12 @@ class Todo < ApplicationRecord
]) ])
end end
# Returns `true` if the current user has any todos for the given target. # Returns `true` if the current user has any todos for the given target with the optional given state.
# #
# target - The value of the `target_type` column, such as `Issue`. # target - The value of the `target_type` column, such as `Issue`.
def any_for_target?(target) # state - The value of the `state` column, such as `pending` or `done`.
exists?(target: target, state: :pending) def any_for_target?(target, *state)
state.empty? ? exists?(target: target) : exists?(target: target, state: state)
end end
# Updates the state of a relation of todos to the new state. # Updates the state of a relation of todos to the new state.
......
...@@ -191,7 +191,7 @@ class TodoService ...@@ -191,7 +191,7 @@ class TodoService
end end
def todo_exist?(issuable, current_user) def todo_exist?(issuable, current_user)
TodosFinder.new(current_user).any_for_target?(issuable) TodosFinder.new(current_user).any_for_target?(issuable, :pending)
end end
private private
......
...@@ -281,12 +281,12 @@ describe Todo do ...@@ -281,12 +281,12 @@ describe Todo do
expect(described_class.any_for_target?(issue)).to eq(true) expect(described_class.any_for_target?(issue)).to eq(true)
end end
it 'returns false if there are only todos for a given target with state done' do it 'returns false if there are only todos for a given target with state done while searching for pending' do
issue = create(:issue) issue = create(:issue)
create(:todo, state: :done, target: issue) create(:todo, state: :done, target: issue)
create(:todo, state: :done, target: issue) create(:todo, state: :done, target: issue)
expect(described_class.any_for_target?(issue)).to eq(false) expect(described_class.any_for_target?(issue, :pending)).to eq(false)
end end
it 'returns false if there are no todos for a given target' do it 'returns false if there are no todos for a given target' do
......
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