Class: IssuableFinder::Params

Inherits:
SimpleDelegator
  • Object
show all
Includes:
Gitlab::Utils::StrongMemoize
Defined in:
app/finders/issuable_finder/params.rb

Constant Summary collapse

FILTER_NONE =

This is used as a common filter for None / Any / Upcoming / Started

'none'
FILTER_ANY =
'any'
FILTER_STARTED =
'started'
FILTER_UPCOMING =
'upcoming'
NONE =

This is used in unassigning users

'0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(params, current_user, klass) ⇒ Params

Returns a new instance of Params.



20
21
22
23
24
25
26
# File 'app/finders/issuable_finder/params.rb', line 20

def initialize(params, current_user, klass)
  @current_user = current_user
  @klass = klass
  # We turn the params into a HashWithIndifferentAccess. We must use #to_h first because sometimes
  # we get ActionController::Params and IssuableFinder::Params objects here.
  super(params.to_h.with_indifferent_access)
end

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(method_name, *args, &block) ⇒ Object (private)



223
224
225
226
227
228
229
# File 'app/finders/issuable_finder/params.rb', line 223

def method_missing(method_name, *args, &block)
  if method_name[-1] == '?'
    params[method_name[0..-2].to_sym].present?
  else
    super
  end
end

Instance Attribute Details

#current_userObject

Returns the value of attribute current_user.



18
19
20
# File 'app/finders/issuable_finder/params.rb', line 18

def current_user
  @current_user
end

#klassObject

Returns the value of attribute klass.



18
19
20
# File 'app/finders/issuable_finder/params.rb', line 18

def klass
  @klass
end

Instance Method Details

#current_user_related?Boolean

Returns:

  • (Boolean)


166
167
168
169
# File 'app/finders/issuable_finder/params.rb', line 166

def current_user_related?
  scope = params[:scope]
  %w[created_by_me authored assigned_to_me reviews_for_me].include?(scope)
end

#filter_by_any_milestone?Boolean

Returns:

  • (Boolean)


44
45
46
47
48
49
50
# File 'app/finders/issuable_finder/params.rb', line 44

def filter_by_any_milestone?
  # Usage of `Any Milestone` and `any`/`Any` in milestone_title to be deprecated
  # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
  params[:milestone_title].to_s.downcase == FILTER_ANY ||
    params[:milestone_title] == Milestone::Any.title ||
    params[:milestone_wildcard_id].to_s.downcase == FILTER_ANY
end

#filter_by_any_reaction?Boolean

Returns:

  • (Boolean)


78
79
80
# File 'app/finders/issuable_finder/params.rb', line 78

def filter_by_any_reaction?
  params[:my_reaction_emoji].to_s.downcase == FILTER_ANY
end

#filter_by_any_release?Boolean

Returns:

  • (Boolean)


70
71
72
# File 'app/finders/issuable_finder/params.rb', line 70

def filter_by_any_release?
  params[:release_tag].to_s.downcase == FILTER_ANY
end

#filter_by_no_milestone?Boolean

Returns:

  • (Boolean)


36
37
38
39
40
41
42
# File 'app/finders/issuable_finder/params.rb', line 36

def filter_by_no_milestone?
  # Usage of `No Milestone` and `none`/`None` in milestone_title to be deprecated
  # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
  params[:milestone_title].to_s.downcase == FILTER_NONE ||
    params[:milestone_title] == Milestone::None.title ||
    params[:milestone_wildcard_id].to_s.downcase == FILTER_NONE
end

#filter_by_no_reaction?Boolean

Returns:

  • (Boolean)


74
75
76
# File 'app/finders/issuable_finder/params.rb', line 74

def filter_by_no_reaction?
  params[:my_reaction_emoji].to_s.downcase == FILTER_NONE
end

#filter_by_no_release?Boolean

Returns:

  • (Boolean)


66
67
68
# File 'app/finders/issuable_finder/params.rb', line 66

def filter_by_no_release?
  params[:release_tag].to_s.downcase == FILTER_NONE
end

#filter_by_started_milestone?Boolean

Returns:

  • (Boolean)


59
60
61
62
63
64
# File 'app/finders/issuable_finder/params.rb', line 59

def filter_by_started_milestone?
  # Usage of `#started` in milestone_title to be deprecated
  # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
  params[:milestone_title] == Milestone::Started.name ||
    params[:milestone_wildcard_id].to_s.downcase == FILTER_STARTED
end

#filter_by_upcoming_milestone?Boolean

Returns:

  • (Boolean)


52
53
54
55
56
57
# File 'app/finders/issuable_finder/params.rb', line 52

def filter_by_upcoming_milestone?
  # Usage of `#upcoming` in milestone_title to be deprecated
  # https://gitlab.com/gitlab-org/gitlab/-/issues/336044
  params[:milestone_title] == Milestone::Upcoming.name ||
    params[:milestone_wildcard_id].to_s.downcase == FILTER_UPCOMING
end

#find_group_projectsObject



171
172
173
174
175
176
177
178
179
# File 'app/finders/issuable_finder/params.rb', line 171

def find_group_projects
  return Project.none unless group

  if params[:include_subgroups]
    Project.where(namespace_id: group.self_and_descendant_ids) # rubocop: disable CodeReuse/ActiveRecord
  else
    group.projects
  end
end

#groupObject



117
118
119
120
121
122
123
124
125
126
# File 'app/finders/issuable_finder/params.rb', line 117

def group
  strong_memoize(:group) do
    next nil unless group?

    group = group_id.is_a?(Group) ? group_id : Group.find(group_id)
    group = nil unless Ability.allowed?(current_user, :read_group, group)

    group
  end
end

#group?Boolean

Returns:

  • (Boolean)


90
91
92
# File 'app/finders/issuable_finder/params.rb', line 90

def group?
  group_id.present?
end

#group_idObject



132
133
134
# File 'app/finders/issuable_finder/params.rb', line 132

def group_id
  params[:group_id]
end

#merge(other) ⇒ Object

We use Hash#merge in a few places, so let’s support it



182
183
184
# File 'app/finders/issuable_finder/params.rb', line 182

def merge(other)
  self.class.new(params.merge(other), current_user, klass)
end

#merge!(other) ⇒ Object

Just for symmetry, and in case someone tries to use it



187
188
189
# File 'app/finders/issuable_finder/params.rb', line 187

def merge!(other)
  params.merge!(other)
end

#milestonesObject



146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
# File 'app/finders/issuable_finder/params.rb', line 146

def milestones
  strong_memoize(:milestones) do
    if milestones?
      if project?
        project_group_id = project.group&.id
        project_id = project.id
      end

      project_group_id = group.id if group

      search_params =
        { title: params[:milestone_title], project_ids: project_id, group_ids: project_group_id }

      MilestonesFinder.new(search_params).execute # rubocop: disable CodeReuse/Finder
    else
      Milestone.none
    end
  end
end

#milestones?Boolean

Returns:

  • (Boolean)


32
33
34
# File 'app/finders/issuable_finder/params.rb', line 32

def milestones?
  params[:milestone_title].present? || params[:milestone_wildcard_id].present?
end

#parentObject



191
192
193
# File 'app/finders/issuable_finder/params.rb', line 191

def parent
  project || group
end

#present?Boolean

Returns:

  • (Boolean)


28
29
30
# File 'app/finders/issuable_finder/params.rb', line 28

def present?
  params.present?
end

#projectObject



106
107
108
109
110
111
112
113
114
115
# File 'app/finders/issuable_finder/params.rb', line 106

def project
  strong_memoize(:project) do
    next nil unless project?

    project = project_id.is_a?(Project) ? project_id : Project.find(project_id)
    project = nil unless Ability.allowed?(current_user, :"read_#{klass.to_ability_name}", project)

    project
  end
end

#project?Boolean

Returns:

  • (Boolean)


86
87
88
# File 'app/finders/issuable_finder/params.rb', line 86

def project?
  project_id.present?
end

#project_idObject



128
129
130
# File 'app/finders/issuable_finder/params.rb', line 128

def project_id
  params[:project_id]
end

#projectsObject



136
137
138
139
140
141
142
143
144
# File 'app/finders/issuable_finder/params.rb', line 136

def projects
  strong_memoize(:projects) do
    next Array.wrap(project) if project?

    projects_public_or_visible_to_user
      .with_feature_available_for_user(klass.base_class, current_user)
      .without_order
  end
end


94
95
96
97
98
99
100
101
102
103
104
# File 'app/finders/issuable_finder/params.rb', line 94

def related_groups
  if project? && project&.group && Ability.allowed?(current_user, :read_group, project.group)
    project.group.self_and_ancestors
  elsif group
    [group]
  elsif current_user
    Gitlab::ObjectHierarchy.new(current_user.authorized_groups, current_user.groups).all_objects
  else
    []
  end
end

#releases?Boolean

Returns:

  • (Boolean)


82
83
84
# File 'app/finders/issuable_finder/params.rb', line 82

def releases?
  params[:release_tag].present?
end

#user_can_see_all_issuables?Boolean

Returns:

  • (Boolean)


195
196
197
# File 'app/finders/issuable_finder/params.rb', line 195

def user_can_see_all_issuables?
  Ability.allowed?(current_user, :read_all_resources)
end