Class: IssueCollection

Inherits:
Object
  • Object
show all
Defined in:
app/models/issue_collection.rb

Overview

IssueCollection can be used to reduce a list of issues down to a subset.

IssueCollection is not meant to be some sort of Enumerable, instead it's meant to take a list of issues and return a new list of issues based on some criteria. For example, given a list of issues you may want to return a list of issues that can be read or updated by a given user.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(collection) ⇒ IssueCollection

Returns a new instance of IssueCollection.


12
13
14
# File 'app/models/issue_collection.rb', line 12

def initialize(collection)
  @collection = collection
end

Instance Attribute Details

#collectionObject (readonly)

Returns the value of attribute collection


10
11
12
# File 'app/models/issue_collection.rb', line 10

def collection
  @collection
end

Instance Method Details

#updatable_by_user(user) ⇒ Object Also known as: visible_to

Returns all the issues that can be updated by the user.


17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'app/models/issue_collection.rb', line 17

def updatable_by_user(user)
  return collection if user.admin?

  # Given all the issue projects we get a list of projects that the current
  # user has at least reporter access to.
  projects_with_reporter_access = user
    .projects_with_reporter_access_limited_to(project_ids)
    .pluck(:id)

  collection.select do |issue|
    if projects_with_reporter_access.include?(issue.project_id)
      true
    elsif issue.is_a?(Issue)
      issue.assignee_or_author?(user)
    else
      false
    end
  end
end