Class: IssuableFinder::Params

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

Direct Known Subclasses

IssuesFinder::Params

Constant Summary collapse

FILTER_NONE =

This is used as a common filter for None / Any

'none'
FILTER_ANY =
'any'
NONE =

This is used in unassigning users

'0'

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from Gitlab::Utils::StrongMemoize

#clear_memoization, #strong_memoize, #strong_memoized?

Constructor Details

#initialize(params, current_user, klass) ⇒ Params

Returns a new instance of Params.


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

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)


283
284
285
286
287
288
289
# File 'app/finders/issuable_finder/params.rb', line 283

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


16
17
18
# File 'app/finders/issuable_finder/params.rb', line 16

def current_user
  @current_user
end

#klassObject

Returns the value of attribute klass


16
17
18
# File 'app/finders/issuable_finder/params.rb', line 16

def klass
  @klass
end

Instance Method Details

#assigneeObject

rubocop: enable CodeReuse/ActiveRecord


193
194
195
# File 'app/finders/issuable_finder/params.rb', line 193

def assignee
  assignees.first
end

#assigneesObject

rubocop: disable CodeReuse/ActiveRecord


180
181
182
183
184
185
186
187
188
189
190
# File 'app/finders/issuable_finder/params.rb', line 180

def assignees
  strong_memoize(:assignees) do
    if assignee_id?
      User.where(id: params[:assignee_id])
    elsif assignee_username?
      User.where(username: params[:assignee_username])
    else
      User.none
    end
  end
end

#authorObject

rubocop: disable CodeReuse/ActiveRecord


166
167
168
169
170
171
172
173
174
175
176
# File 'app/finders/issuable_finder/params.rb', line 166

def author
  strong_memoize(:author) do
    if author_id?
      User.find_by(id: params[:author_id])
    elsif author_username?
      User.find_by_username(params[:author_username])
    else
      nil
    end
  end
end

#author_id?Boolean

Returns:

  • (Boolean)

30
31
32
# File 'app/finders/issuable_finder/params.rb', line 30

def author_id?
  params[:author_id].present? && params[:author_id] != NONE
end

#author_username?Boolean

Returns:

  • (Boolean)

34
35
36
# File 'app/finders/issuable_finder/params.rb', line 34

def author_username?
  params[:author_username].present? && params[:author_username] != NONE
end

#current_user_related?Boolean

Returns:

  • (Boolean)

235
236
237
238
# File 'app/finders/issuable_finder/params.rb', line 235

def current_user_related?
  scope = params[:scope]
  scope == 'created_by_me' || scope == 'authored' || scope == 'assigned_to_me'
end

#filter_by_any_assignee?Boolean

Returns:

  • (Boolean)

47
48
49
# File 'app/finders/issuable_finder/params.rb', line 47

def filter_by_any_assignee?
  params[:assignee_id].to_s.downcase == FILTER_ANY
end

#filter_by_any_label?Boolean

Returns:

  • (Boolean)

57
58
59
# File 'app/finders/issuable_finder/params.rb', line 57

def filter_by_any_label?
  label_names.map(&:downcase).include?(FILTER_ANY)
end

#filter_by_any_milestone?Boolean

Returns:

  • (Boolean)

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

def filter_by_any_milestone?
  # Accepts `Any Milestone` for compatibility
  params[:milestone_title].to_s.downcase == FILTER_ANY || params[:milestone_title] == Milestone::Any.title
end

#filter_by_any_reaction?Boolean

Returns:

  • (Boolean)

99
100
101
# File 'app/finders/issuable_finder/params.rb', line 99

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

#filter_by_any_release?Boolean

Returns:

  • (Boolean)

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

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

#filter_by_no_assignee?Boolean

Returns:

  • (Boolean)

43
44
45
# File 'app/finders/issuable_finder/params.rb', line 43

def filter_by_no_assignee?
  params[:assignee_id].to_s.downcase == FILTER_NONE
end

#filter_by_no_label?Boolean

Returns:

  • (Boolean)

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

def filter_by_no_label?
  downcased = label_names.map(&:downcase)

  downcased.include?(FILTER_NONE)
end

#filter_by_no_milestone?Boolean

Returns:

  • (Boolean)

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

def filter_by_no_milestone?
  # Accepts `No Milestone` for compatibility
  params[:milestone_title].to_s.downcase == FILTER_NONE || params[:milestone_title] == Milestone::None.title
end

#filter_by_no_reaction?Boolean

Returns:

  • (Boolean)

95
96
97
# File 'app/finders/issuable_finder/params.rb', line 95

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

#filter_by_no_release?Boolean

Returns:

  • (Boolean)

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

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

#filter_by_started_milestone?Boolean

Returns:

  • (Boolean)

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

def filter_by_started_milestone?
  params[:milestone_title] == Milestone::Started.name
end

#filter_by_upcoming_milestone?Boolean

Returns:

  • (Boolean)

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

def filter_by_upcoming_milestone?
  params[:milestone_title] == Milestone::Upcoming.name
end

#find_group_projectsObject


240
241
242
243
244
245
246
247
248
# File 'app/finders/issuable_finder/params.rb', line 240

def find_group_projects
  return Project.none unless group

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

#groupObject


111
112
113
114
115
116
117
118
119
120
121
# File 'app/finders/issuable_finder/params.rb', line 111

def group
  strong_memoize(:group) do
    if params[:group_id].is_a?(Group)
      params[:group_id]
    elsif params[:group_id].present?
      Group.find(params[:group_id])
    else
      nil
    end
  end
end

#label_namesObject


197
198
199
200
201
202
203
# File 'app/finders/issuable_finder/params.rb', line 197

def label_names
  if labels?
    params[:label_name].is_a?(String) ? params[:label_name].split(',') : params[:label_name]
  else
    []
  end
end

#labelsObject


205
206
207
208
209
210
211
212
213
# File 'app/finders/issuable_finder/params.rb', line 205

def labels
  strong_memoize(:labels) do
    if labels? && !filter_by_no_label?
      LabelsFinder.new(current_user, project_ids: projects, title: label_names).execute(skip_authorization: true) # rubocop: disable CodeReuse/Finder
    else
      Label.none
    end
  end
end

#labels?Boolean

Returns:

  • (Boolean)

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

def labels?
  params[:label_name].present?
end

#merge(other) ⇒ Object

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


251
252
253
# File 'app/finders/issuable_finder/params.rb', line 251

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


256
257
258
# File 'app/finders/issuable_finder/params.rb', line 256

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

#milestonesObject


215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
# File 'app/finders/issuable_finder/params.rb', line 215

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

      group_id = group.id if group

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

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

#milestones?Boolean

Returns:

  • (Boolean)

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

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

#no_author?Boolean

Returns:

  • (Boolean)

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

def no_author?
  # author_id takes precedence over author_username
  params[:author_id] == NONE || params[:author_username] == NONE
end

#present?Boolean

Returns:

  • (Boolean)

26
27
28
# File 'app/finders/issuable_finder/params.rb', line 26

def present?
  params.present?
end

#projectObject


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

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)

107
108
109
# File 'app/finders/issuable_finder/params.rb', line 107

def project?
  project_id.present?
end

#project_idObject


146
147
148
# File 'app/finders/issuable_finder/params.rb', line 146

def project_id
  params[:project_id]
end

#projectsObject


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

def projects
  strong_memoize(:projects) do
    next [project] if project?

    projects =
      if current_user && params[:authorized_only].presence && !current_user_related?
        current_user.authorized_projects(min_access_level)
      else
        projects_public_or_visible_to_user
      end

    projects.with_feature_available_for_user(klass, current_user).reorder(nil) # rubocop: disable CodeReuse/ActiveRecord
  end
end

123
124
125
126
127
128
129
130
131
132
133
# File 'app/finders/issuable_finder/params.rb', line 123

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)

103
104
105
# File 'app/finders/issuable_finder/params.rb', line 103

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