Class: Autocomplete::ProjectFinder

Inherits:
Object
  • Object
show all
Defined in:
app/finders/autocomplete/project_finder.rb

Overview

Finder for retrieving a project to use for autocomplete data sources.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(current_user = nil, params = {}) ⇒ ProjectFinder

current_user - The currently logged in user, if any. params - A Hash containing parameters to use for finding the project.

The following parameters are supported:

  • project_id: The ID of the project to find.



14
15
16
17
# File 'app/finders/autocomplete/project_finder.rb', line 14

def initialize(current_user = nil, params = {})
  @current_user = current_user
  @project_id = params[:project_id]
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



6
7
8
# File 'app/finders/autocomplete/project_finder.rb', line 6

def current_user
  @current_user
end

#project_idObject (readonly)

Returns the value of attribute project_id.



6
7
8
# File 'app/finders/autocomplete/project_finder.rb', line 6

def project_id
  @project_id
end

Instance Method Details

#executeObject

Attempts to find a Project based on the current project ID.



20
21
22
23
24
25
26
27
28
29
30
31
32
# File 'app/finders/autocomplete/project_finder.rb', line 20

def execute
  return if project_id.blank?

  project = Project.find(project_id)

  # This removes the need for using `return render_404` and similar patterns
  # in controllers that use this finder.
  unless Ability.allowed?(current_user, :read_project, project)
    raise ActiveRecord::RecordNotFound, "Could not find a Project with ID #{project_id}"
  end

  project
end