todos_finder.rb 3.22 KB
Newer Older
1
# TodosFinder
2
#
3
# Used to filter Todos by set of params
4 5 6 7 8 9 10
#
# Arguments:
#   current_user - which user use
#   params:
#     action_id: integer
#     author_id: integer
#     project_id; integer
11
#     state: 'pending' (default) or 'done'
12 13 14
#     type: 'Issue' or 'MergeRequest'
#

15
class TodosFinder
16 17 18 19 20
  prepend FinderWithCrossProjectAccess
  include FinderMethods

  requires_cross_project_access unless: -> { project? }

21
  NONE = '0'.freeze
22 23 24

  attr_accessor :current_user, :params

25
  def initialize(current_user, params = {})
26 27 28 29 30
    @current_user = current_user
    @params = params
  end

  def execute
31
    items = current_user.todos
32
    items = by_action_id(items)
33
    items = by_action(items)
34 35 36
    items = by_author(items)
    items = by_state(items)
    items = by_type(items)
37 38 39
    # Filtering by project HAS TO be the last because we use
    # the project IDs yielded by the todos query thus far
    items = by_project(items)
40

Felipe's avatar
Felipe committed
41
    sort(items)
42 43 44 45 46
  end

  private

  def action_id?
47
    action_id.present? && Todo::ACTION_NAMES.key?(action_id.to_i)
48 49 50 51 52 53
  end

  def action_id
    params[:action_id]
  end

54 55 56 57 58 59 60 61 62 63 64 65
  def to_action_id
    Todo::ACTION_NAMES.key(action.to_sym)
  end

  def action?
    action.present? && to_action_id
  end

  def action
    params[:action]
  end

66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90
  def author?
    params[:author_id].present?
  end

  def author
    return @author if defined?(@author)

    @author =
      if author? && params[:author_id] != NONE
        User.find(params[:author_id])
      else
        nil
      end
  end

  def project?
    params[:project_id].present?
  end

  def project
    return @project if defined?(@project)

    if project?
      @project = Project.find(params[:project_id])

91 92
      @project = nil if @project.pending_delete?

jneen's avatar
jneen committed
93
      unless Ability.allowed?(current_user, :read_project, @project)
94 95 96 97 98 99 100 101 102
        @project = nil
      end
    else
      @project = nil
    end

    @project
  end

103 104 105 106 107 108 109 110 111 112
  def project_ids(items)
    ids = items.except(:order).select(:project_id)
    if Gitlab::Database.mysql?
      # To make UPDATE work on MySQL, wrap it in a SELECT with an alias
      ids = Todo.except(:order).select('*').from("(#{ids.to_sql}) AS t")
    end

    ids
  end

113
  def projects(items)
114
    ProjectsFinder.new(current_user: current_user, project_ids_relation: project_ids(items)).execute
115 116
  end

117
  def type?
Douwe Maan's avatar
Douwe Maan committed
118
    type.present? && %w(Issue MergeRequest).include?(type)
119 120 121 122 123 124
  end

  def type
    params[:type]
  end

Felipe's avatar
Felipe committed
125
  def sort(items)
126
    params[:sort] ? items.sort(params[:sort]) : items.order_id_desc
Felipe's avatar
Felipe committed
127 128
  end

129 130 131 132 133 134 135 136
  def by_action(items)
    if action?
      items = items.where(action: to_action_id)
    end

    items
  end

137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155
  def by_action_id(items)
    if action_id?
      items = items.where(action: action_id)
    end

    items
  end

  def by_author(items)
    if author?
      items = items.where(author_id: author.try(:id))
    end

    items
  end

  def by_project(items)
    if project?
      items = items.where(project: project)
156 157 158
    else
      item_projects = projects(items)
      items = items.merge(item_projects).joins(:project)
159 160 161 162 163 164
    end

    items
  end

  def by_state(items)
165
    case params[:state].to_s
166 167 168 169 170 171 172 173 174 175 176 177 178 179 180
    when 'done'
      items.done
    else
      items.pending
    end
  end

  def by_type(items)
    if type?
      items = items.where(target_type: type)
    end

    items
  end
end