Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorYorick Peterse <yorickpeterse@gmail.com>2018-09-20 18:05:26 +0300
committerYorick Peterse <yorickpeterse@gmail.com>2018-10-08 16:19:12 +0300
commit38b8ae641fcfd7bbc5958556c5976d9ed872784c (patch)
treecccb3268ed808ed826d71edb0e6186e0199d6b46 /app/models/todo.rb
parent4c1dc31051fb741bbd6daff4b5c1dcb166d85eeb (diff)
Clean up ActiveRecord code in TodoService
This refactors the TodoService class according to our code reuse guidelines. The resulting code is a wee bit more verbose, but it allows us to decouple the column names from the input, resulting in fewer changes being necessary when we change the schema. One particular noteworthy line in TodoService is the following: todos_ids = todos.update_state(state) Technically this is a violation of the guidelines, because `update_state` is a class method, which services are not supposed to use (safe for a few allowed ones). I decided to keep this, since there is no alternative. `update_state` doesn't produce a relation so it doesn't belong in a Finder, and we can't move it to another Service either. As such I opted to just use the method directly. Cases like this may happen more frequently, at which point we should update our documentation with some sort of recommendation. For now, I want to refrain from doing so until we have a few more examples.
Diffstat (limited to 'app/models/todo.rb')
-rw-r--r--app/models/todo.rb24
1 files changed, 24 insertions, 0 deletions
diff --git a/app/models/todo.rb b/app/models/todo.rb
index d2890f9ca8e..7b64615f699 100644
--- a/app/models/todo.rb
+++ b/app/models/todo.rb
@@ -45,6 +45,8 @@ class Todo < ActiveRecord::Base
scope :for_project, -> (project) { where(project: project) }
scope :for_group, -> (group) { where(group: group) }
scope :for_type, -> (type) { where(target_type: type) }
+ scope :for_target, -> (id) { where(target_id: id) }
+ scope :for_commit, -> (id) { where(commit_id: id) }
state_machine :state, initial: :pending do
event :done do
@@ -72,6 +74,28 @@ class Todo < ActiveRecord::Base
])
end
+ # Returns `true` if the current user has any todos for the given target.
+ #
+ # target - The value of the `target_type` column, such as `Issue`.
+ def any_for_target?(target)
+ exists?(target: target)
+ end
+
+ # Updates the state of a relation of todos to the new state.
+ #
+ # new_state - The new state of the todos.
+ #
+ # Returns an `Array` containing the IDs of the updated todos.
+ def update_state(new_state)
+ # Only update those that are not really on that state
+ base = where.not(state: new_state).except(:order)
+ ids = base.pluck(:id)
+
+ base.update_all(state: new_state)
+
+ ids
+ end
+
# Priority sorting isn't displayed in the dropdown, because we don't show
# milestones, but still show something if the user has a URL with that
# selected.