Commit a5cfa8f0 authored by Stan Hu's avatar Stan Hu

Merge branch '66701-done-quick-action-is-available-even-no-To-Do-exists' into 'master'

Refactored todo model to check only for pending todos

See merge request gitlab-org/gitlab!16837
parents ff26d072 19f74903
...@@ -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 = nil)
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) def any_for_target?(target, state = nil)
state.nil? ? 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
......
---
title: Changed todo/done quick actions to work not only for first usage
merge_request: 16837
author: Marc Schwede
type: fixed
...@@ -273,6 +273,22 @@ describe Todo do ...@@ -273,6 +273,22 @@ describe Todo do
expect(described_class.any_for_target?(todo.target)).to eq(true) expect(described_class.any_for_target?(todo.target)).to eq(true)
end end
it 'returns true if there is at least one todo for a given target with state pending' do
issue = create(:issue)
create(:todo, state: :done, target: issue)
create(:todo, state: :pending, target: issue)
expect(described_class.any_for_target?(issue)).to eq(true)
end
it 'returns false if there are only todos for a given target with state done while searching for pending' do
issue = create(:issue)
create(:todo, state: :done, target: issue)
create(:todo, state: :done, target: issue)
expect(described_class.any_for_target?(issue, :pending)).to eq(false)
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
issue = create(:issue) issue = create(:issue)
......
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