diff options
author | Yorick Peterse <yorickpeterse@gmail.com> | 2018-09-20 16:18:04 +0300 |
---|---|---|
committer | Yorick Peterse <yorickpeterse@gmail.com> | 2018-10-08 16:19:12 +0300 |
commit | 4c1dc31051fb741bbd6daff4b5c1dcb166d85eeb (patch) | |
tree | f47f5d5bec9e2f13d8904d60341e32d7c57ebedd /app/models/todo.rb | |
parent | c616327c1221dc91318a35769e01ab6932d497ea (diff) |
Clean up ActiveRecord code in TodosFinder
This refactors the TodosFinder finder according to the new code reuse
rules, as enforced by the CodeReuse cops. I also changed some of the
methods to use regular if statements, instead of assignments and/or
early returns. This results in a more natural flow when reading the
code, and it makes it harder to accidentally return the wrong result.
Diffstat (limited to 'app/models/todo.rb')
-rw-r--r-- | app/models/todo.rb | 19 |
1 files changed, 19 insertions, 0 deletions
diff --git a/app/models/todo.rb b/app/models/todo.rb index 265fb932f7c..d2890f9ca8e 100644 --- a/app/models/todo.rb +++ b/app/models/todo.rb @@ -40,6 +40,11 @@ class Todo < ActiveRecord::Base scope :pending, -> { with_state(:pending) } scope :done, -> { with_state(:done) } + scope :for_action, -> (action) { where(action: action) } + scope :for_author, -> (author) { where(author: author) } + scope :for_project, -> (project) { where(project: project) } + scope :for_group, -> (group) { where(group: group) } + scope :for_type, -> (type) { where(target_type: type) } state_machine :state, initial: :pending do event :done do @@ -53,6 +58,20 @@ class Todo < ActiveRecord::Base after_save :keep_around_commit, if: :commit_id class << self + # Returns all todos for the given group and its descendants. + # + # group - A `Group` to retrieve todos for. + # + # Returns an `ActiveRecord::Relation`. + def for_group_and_descendants(group) + groups = group.self_and_descendants + + from_union([ + for_project(Project.for_group(groups)), + for_group(groups) + ]) + 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. |