PageRenderTime 76ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/app/finders/issuable_finder.rb

https://gitlab.com/lee.claude/gitlab-ce
Ruby | 227 lines | 168 code | 41 blank | 18 comment | 34 complexity | 5c0ce4281f29107e99c4116493c903ea MD5 | raw file
Possible License(s): CC-BY-3.0
  1. # IssuableFinder
  2. #
  3. # Used to filter Issues and MergeRequests collections by set of params
  4. #
  5. # Arguments:
  6. # klass - actual class like Issue or MergeRequest
  7. # current_user - which user use
  8. # params:
  9. # scope: 'created-by-me' or 'assigned-to-me' or 'all'
  10. # state: 'open' or 'closed' or 'all'
  11. # group_id: integer
  12. # project_id: integer
  13. # milestone_id: integer
  14. # assignee_id: integer
  15. # search: string
  16. # label_name: string
  17. # sort: string
  18. #
  19. require_relative 'projects_finder'
  20. class IssuableFinder
  21. NONE = '0'
  22. attr_accessor :current_user, :params
  23. def initialize(current_user, params)
  24. @current_user = current_user
  25. @params = params
  26. end
  27. def execute
  28. items = init_collection
  29. items = by_scope(items)
  30. items = by_state(items)
  31. items = by_group(items)
  32. items = by_project(items)
  33. items = by_search(items)
  34. items = by_milestone(items)
  35. items = by_assignee(items)
  36. items = by_author(items)
  37. items = by_label(items)
  38. items = sort(items)
  39. end
  40. def group
  41. return @group if defined?(@group)
  42. @group =
  43. if params[:group_id].present?
  44. Group.find(params[:group_id])
  45. else
  46. nil
  47. end
  48. end
  49. def project
  50. return @project if defined?(@project)
  51. @project =
  52. if params[:project_id].present?
  53. Project.find(params[:project_id])
  54. else
  55. nil
  56. end
  57. end
  58. def search
  59. params[:search].presence
  60. end
  61. def milestones?
  62. params[:milestone_title].present?
  63. end
  64. def milestones
  65. return @milestones if defined?(@milestones)
  66. @milestones =
  67. if milestones? && params[:milestone_title] != NONE
  68. Milestone.where(title: params[:milestone_title])
  69. else
  70. nil
  71. end
  72. end
  73. def assignee?
  74. params[:assignee_id].present?
  75. end
  76. def assignee
  77. return @assignee if defined?(@assignee)
  78. @assignee =
  79. if assignee? && params[:assignee_id] != NONE
  80. User.find(params[:assignee_id])
  81. else
  82. nil
  83. end
  84. end
  85. def author?
  86. params[:author_id].present?
  87. end
  88. def author
  89. return @author if defined?(@author)
  90. @author =
  91. if author? && params[:author_id] != NONE
  92. User.find(params[:author_id])
  93. else
  94. nil
  95. end
  96. end
  97. private
  98. def init_collection
  99. table_name = klass.table_name
  100. if project
  101. if Ability.abilities.allowed?(current_user, :read_project, project)
  102. project.send(table_name)
  103. else
  104. []
  105. end
  106. elsif current_user && params[:authorized_only].presence && !current_user_related?
  107. klass.of_projects(current_user.authorized_projects).references(:project)
  108. else
  109. klass.of_projects(ProjectsFinder.new.execute(current_user)).references(:project)
  110. end
  111. end
  112. def by_scope(items)
  113. case params[:scope]
  114. when 'created-by-me', 'authored' then
  115. items.where(author_id: current_user.id)
  116. when 'all' then
  117. items
  118. when 'assigned-to-me' then
  119. items.where(assignee_id: current_user.id)
  120. else
  121. raise 'You must specify default scope'
  122. end
  123. end
  124. def by_state(items)
  125. case params[:state]
  126. when 'closed'
  127. items.closed
  128. when 'rejected'
  129. items.respond_to?(:rejected) ? items.rejected : items.closed
  130. when 'merged'
  131. items.respond_to?(:merged) ? items.merged : items.closed
  132. when 'all'
  133. items
  134. when 'opened'
  135. items.opened
  136. else
  137. raise 'You must specify default state'
  138. end
  139. end
  140. def by_group(items)
  141. items = items.of_group(group) if group
  142. items
  143. end
  144. def by_project(items)
  145. items = items.of_projects(project.id) if project
  146. items
  147. end
  148. def by_search(items)
  149. items = items.search(search) if search
  150. items
  151. end
  152. def sort(items)
  153. items.sort(params[:sort])
  154. end
  155. def by_milestone(items)
  156. if milestones?
  157. items = items.where(milestone_id: milestones.try(:pluck, :id))
  158. end
  159. items
  160. end
  161. def by_assignee(items)
  162. if assignee?
  163. items = items.where(assignee_id: assignee.try(:id))
  164. end
  165. items
  166. end
  167. def by_author(items)
  168. if author?
  169. items = items.where(author_id: author.try(:id))
  170. end
  171. items
  172. end
  173. def by_label(items)
  174. if params[:label_name].present?
  175. label_names = params[:label_name].split(",")
  176. item_ids = LabelLink.joins(:label).
  177. where('labels.title in (?)', label_names).
  178. where(target_type: klass.name).pluck(:target_id)
  179. items = items.where(id: item_ids)
  180. end
  181. items
  182. end
  183. def current_user_related?
  184. params[:scope] == 'created-by-me' || params[:scope] == 'authored' || params[:scope] == 'assigned-to-me'
  185. end
  186. end