Class: Environments::EnvironmentNamesFinder

Inherits:
Object
  • Object
show all
Defined in:
app/finders/environments/environment_names_finder.rb

Overview

Finder for obtaining the unique environment names of a project or group.

This finder exists so that the merge requests “environments” filter can be populated with a unique list of environment names. If we retrieve just the environments, duplicates may be present (e.g. multiple projects in a group having a “staging” environment).

In addition, this finder only produces unfoldered environments. We do this because when searching for environments we want to exclude review app environments.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(project_or_group, current_user = nil) ⇒ EnvironmentNamesFinder

Returns a new instance of EnvironmentNamesFinder.



17
18
19
20
# File 'app/finders/environments/environment_names_finder.rb', line 17

def initialize(project_or_group, current_user = nil)
  @project_or_group = project_or_group
  @current_user = current_user
end

Instance Attribute Details

#current_userObject (readonly)

Returns the value of attribute current_user.



15
16
17
# File 'app/finders/environments/environment_names_finder.rb', line 15

def current_user
  @current_user
end

#project_or_groupObject (readonly)

Returns the value of attribute project_or_group.



15
16
17
# File 'app/finders/environments/environment_names_finder.rb', line 15

def project_or_group
  @project_or_group
end

Instance Method Details

#all_environmentsObject



26
27
28
29
30
31
32
# File 'app/finders/environments/environment_names_finder.rb', line 26

def all_environments
  if project_or_group.is_a?(Namespace)
    namespace_environments
  else
    project_environments
  end
end

#executeObject



22
23
24
# File 'app/finders/environments/environment_names_finder.rb', line 22

def execute
  all_environments.unfoldered.order_by_name.pluck_unique_names
end

#namespace_environmentsObject



34
35
36
37
38
39
40
# File 'app/finders/environments/environment_names_finder.rb', line 34

def namespace_environments
  projects = project_or_group
    .all_projects
    .filter_by_feature_visibility(:environments, current_user)

  Environment.for_project(projects)
end

#project_environmentsObject



42
43
44
45
46
47
48
# File 'app/finders/environments/environment_names_finder.rb', line 42

def project_environments
  if Ability.allowed?(current_user, :read_environment, project_or_group)
    project_or_group.environments
  else
    Environment.none
  end
end