Commit 2a2fab68 authored by Sean McGivern's avatar Sean McGivern

Merge branch 'jk-backport-issuables-refactor' into 'master'

Refactor issuables index actions

See merge request gitlab-org/gitlab-ce!15242
parents 3f35ba76 ad6e6502
...@@ -9,9 +9,7 @@ module IssuableActions ...@@ -9,9 +9,7 @@ module IssuableActions
def show def show
respond_to do |format| respond_to do |format|
format.html do format.html
render show_view
end
format.json do format.json do
render json: serializer.represent(issuable, serializer: params[:serializer]) render json: serializer.represent(issuable, serializer: params[:serializer])
end end
...@@ -152,10 +150,6 @@ module IssuableActions ...@@ -152,10 +150,6 @@ module IssuableActions
end end
end end
def show_view
'show'
end
def serializer def serializer
raise NotImplementedError raise NotImplementedError
end end
......
...@@ -4,58 +4,44 @@ module IssuableCollections ...@@ -4,58 +4,44 @@ module IssuableCollections
include Gitlab::IssuableMetadata include Gitlab::IssuableMetadata
included do included do
helper_method :issues_finder helper_method :finder
helper_method :merge_requests_finder
end end
private private
def set_issues_index def set_issuables_index
@collection_type = "Issue" @issuables = issuables_collection
@issues = issues_collection @issuables = @issuables.page(params[:page])
@issues = @issues.page(params[:page]) @issuable_meta_data = issuable_meta_data(@issuables, collection_type)
@issuable_meta_data = issuable_meta_data(@issues, @collection_type) @total_pages = issuable_page_count
@total_pages = issues_page_count(@issues)
return if redirect_out_of_range(@issues, @total_pages) return if redirect_out_of_range(@total_pages)
if params[:label_name].present? if params[:label_name].present?
@labels = LabelsFinder.new(current_user, project_id: @project.id, title: params[:label_name]).execute labels_params = { project_id: @project.id, title: params[:label_name] }
@labels = LabelsFinder.new(current_user, labels_params).execute
end end
@users = [] @users = []
if params[:assignee_id].present?
assignee = User.find_by_id(params[:assignee_id])
@users.push(assignee) if assignee
end end
def issues_collection if params[:author_id].present?
issues_finder.execute.preload(:project, :author, :assignees, :labels, :milestone, project: :namespace) author = User.find_by_id(params[:author_id])
@users.push(author) if author
end end
def merge_requests_collection
merge_requests_finder.execute.preload(
:source_project,
:target_project,
:author,
:assignee,
:labels,
:milestone,
head_pipeline: :project,
target_project: :namespace,
merge_request_diff: :merge_request_diff_commits
)
end
def issues_finder
@issues_finder ||= issuable_finder_for(IssuesFinder)
end end
def merge_requests_finder def issuables_collection
@merge_requests_finder ||= issuable_finder_for(MergeRequestsFinder) finder.execute.preload(preload_for_collection)
end end
def redirect_out_of_range(relation, total_pages) def redirect_out_of_range(total_pages)
return false if total_pages.zero? return false if total_pages.zero?
out_of_range = relation.current_page > total_pages out_of_range = @issuables.current_page > total_pages
if out_of_range if out_of_range
redirect_to(url_for(params.merge(page: total_pages, only_path: true))) redirect_to(url_for(params.merge(page: total_pages, only_path: true)))
...@@ -64,12 +50,8 @@ module IssuableCollections ...@@ -64,12 +50,8 @@ module IssuableCollections
out_of_range out_of_range
end end
def issues_page_count(relation) def issuable_page_count
page_count_for_relation(relation, issues_finder.row_count) page_count_for_relation(@issuables, finder.row_count)
end
def merge_requests_page_count(relation)
page_count_for_relation(relation, merge_requests_finder.row_count)
end end
def page_count_for_relation(relation, row_count) def page_count_for_relation(relation, row_count)
...@@ -145,4 +127,31 @@ module IssuableCollections ...@@ -145,4 +127,31 @@ module IssuableCollections
else value else value
end end
end end
def finder
return @finder if defined?(@finder)
@finder = issuable_finder_for(@finder_type)
end
def collection_type
@collection_type ||= case finder
when IssuesFinder
'Issue'
when MergeRequestsFinder
'MergeRequest'
end
end
def preload_for_collection
@preload_for_collection ||= case collection_type
when 'Issue'
[:project, :author, :assignees, :labels, :milestone, project: :namespace]
when 'MergeRequest'
[
:source_project, :target_project, :author, :assignee, :labels, :milestone,
head_pipeline: :project, target_project: :namespace, merge_request_diff: :merge_request_diff_commits
]
end
end
end end
...@@ -3,14 +3,14 @@ module IssuesAction ...@@ -3,14 +3,14 @@ module IssuesAction
include IssuableCollections include IssuableCollections
def issues def issues
@label = issues_finder.labels.first @finder_type = IssuesFinder
@label = finder.labels.first
@issues = issues_collection @issues = issuables_collection
.non_archived .non_archived
.page(params[:page]) .page(params[:page])
@collection_type = "Issue" @issuable_meta_data = issuable_meta_data(@issues, collection_type)
@issuable_meta_data = issuable_meta_data(@issues, @collection_type)
respond_to do |format| respond_to do |format|
format.html format.html
......
...@@ -3,13 +3,12 @@ module MergeRequestsAction ...@@ -3,13 +3,12 @@ module MergeRequestsAction
include IssuableCollections include IssuableCollections
def merge_requests def merge_requests
@label = merge_requests_finder.labels.first @finder_type = MergeRequestsFinder
@label = finder.labels.first
@merge_requests = merge_requests_collection @merge_requests = issuables_collection.page(params[:page])
.page(params[:page])
@collection_type = "MergeRequest" @issuable_meta_data = issuable_meta_data(@merge_requests, collection_type)
@issuable_meta_data = issuable_meta_data(@merge_requests, @collection_type)
end end
private private
......
...@@ -10,7 +10,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -10,7 +10,7 @@ class Projects::IssuesController < Projects::ApplicationController
before_action :check_issues_available! before_action :check_issues_available!
before_action :issue, except: [:index, :new, :create, :bulk_update] before_action :issue, except: [:index, :new, :create, :bulk_update]
before_action :set_issues_index, only: [:index] before_action :set_issuables_index, only: [:index]
# Allow write(create) issue # Allow write(create) issue
before_action :authorize_create_issue!, only: [:new, :create] before_action :authorize_create_issue!, only: [:new, :create]
...@@ -24,15 +24,7 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -24,15 +24,7 @@ class Projects::IssuesController < Projects::ApplicationController
respond_to :html respond_to :html
def index def index
if params[:assignee_id].present? @issues = @issuables
assignee = User.find_by_id(params[:assignee_id])
@users.push(assignee) if assignee
end
if params[:author_id].present?
author = User.find_by_id(params[:author_id])
@users.push(author) if author
end
respond_to do |format| respond_to do |format|
format.html format.html
...@@ -252,4 +244,9 @@ class Projects::IssuesController < Projects::ApplicationController ...@@ -252,4 +244,9 @@ class Projects::IssuesController < Projects::ApplicationController
update_params = issue_params.merge(spammable_params) update_params = issue_params.merge(spammable_params)
Issues::UpdateService.new(project, current_user, update_params) Issues::UpdateService.new(project, current_user, update_params)
end end
def set_issuables_index
@finder_type = IssuesFinder
super
end
end end
...@@ -10,33 +10,12 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -10,33 +10,12 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
before_action :authorize_update_issuable!, only: [:close, :edit, :update, :remove_wip, :sort] before_action :authorize_update_issuable!, only: [:close, :edit, :update, :remove_wip, :sort]
before_action :set_issuables_index, only: [:index]
before_action :authenticate_user!, only: [:assign_related_issues] before_action :authenticate_user!, only: [:assign_related_issues]
def index def index
@collection_type = "MergeRequest" @merge_requests = @issuables
@merge_requests = merge_requests_collection
@merge_requests = @merge_requests.page(params[:page])
@merge_requests = @merge_requests.preload(merge_request_diff: :merge_request)
@issuable_meta_data = issuable_meta_data(@merge_requests, @collection_type)
@total_pages = merge_requests_page_count(@merge_requests)
return if redirect_out_of_range(@merge_requests, @total_pages)
if params[:label_name].present?
labels_params = { project_id: @project.id, title: params[:label_name] }
@labels = LabelsFinder.new(current_user, labels_params).execute
end
@users = []
if params[:assignee_id].present?
assignee = User.find_by_id(params[:assignee_id])
@users.push(assignee) if assignee
end
if params[:author_id].present?
author = User.find_by_id(params[:author_id])
@users.push(author) if author
end
respond_to do |format| respond_to do |format|
format.html format.html
...@@ -338,4 +317,9 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo ...@@ -338,4 +317,9 @@ class Projects::MergeRequestsController < Projects::MergeRequests::ApplicationCo
@target_project = @merge_request.target_project @target_project = @merge_request.target_project
@target_branches = @merge_request.target_project.repository.branch_names @target_branches = @merge_request.target_project.repository.branch_names
end end
def set_issuables_index
@finder_type = MergeRequestsFinder
super
end
end end
...@@ -275,7 +275,8 @@ class ProjectsController < Projects::ApplicationController ...@@ -275,7 +275,8 @@ class ProjectsController < Projects::ApplicationController
@project_wiki = @project.wiki @project_wiki = @project.wiki
@wiki_home = @project_wiki.find_page('home', params[:version_id]) @wiki_home = @project_wiki.find_page('home', params[:version_id])
elsif @project.feature_available?(:issues, current_user) elsif @project.feature_available?(:issues, current_user)
@issues = issues_collection.page(params[:page]) @finder_type = IssuesFinder
@issues = issuables_collection.page(params[:page])
@collection_type = 'Issue' @collection_type = 'Issue'
@issuable_meta_data = issuable_meta_data(@issues, @collection_type) @issuable_meta_data = issuable_meta_data(@issues, @collection_type)
end end
......
...@@ -249,8 +249,6 @@ module IssuablesHelper ...@@ -249,8 +249,6 @@ module IssuablesHelper
end end
def issuables_count_for_state(issuable_type, state) def issuables_count_for_state(issuable_type, state)
finder = public_send("#{issuable_type}_finder") # rubocop:disable GitlabSecurity/PublicSend
Gitlab::IssuablesCountForState.new(finder)[state] Gitlab::IssuablesCountForState.new(finder)[state]
end end
......
...@@ -17,6 +17,8 @@ module Issuable ...@@ -17,6 +17,8 @@ module Issuable
include Importable include Importable
include Editable include Editable
include AfterCommitQueue include AfterCommitQueue
include Sortable
include CreatedAtFilterable
# This object is used to gather issuable meta data for displaying # This object is used to gather issuable meta data for displaying
# upvotes, downvotes, notes and closing merge requests count for issues and merge requests # upvotes, downvotes, notes and closing merge requests count for issues and merge requests
......
...@@ -5,11 +5,9 @@ class Issue < ActiveRecord::Base ...@@ -5,11 +5,9 @@ class Issue < ActiveRecord::Base
include Issuable include Issuable
include Noteable include Noteable
include Referable include Referable
include Sortable
include Spammable include Spammable
include FasterCacheKeys include FasterCacheKeys
include RelativePositioning include RelativePositioning
include CreatedAtFilterable
include TimeTrackable include TimeTrackable
DueDateStruct = Struct.new(:title, :name).freeze DueDateStruct = Struct.new(:title, :name).freeze
......
...@@ -3,9 +3,7 @@ class MergeRequest < ActiveRecord::Base ...@@ -3,9 +3,7 @@ class MergeRequest < ActiveRecord::Base
include Issuable include Issuable
include Noteable include Noteable
include Referable include Referable
include Sortable
include IgnorableColumn include IgnorableColumn
include CreatedAtFilterable
include TimeTrackable include TimeTrackable
ignore_column :locked_at, ignore_column :locked_at,
......
...@@ -3,8 +3,8 @@ ...@@ -3,8 +3,8 @@
- if @can_bulk_update - if @can_bulk_update
= button_tag "Edit issues", class: "btn btn-default append-right-10 js-bulk-update-toggle" = button_tag "Edit issues", class: "btn btn-default append-right-10 js-bulk-update-toggle"
= link_to "New issue", new_project_issue_path(@project, = link_to "New issue", new_project_issue_path(@project,
issue: { assignee_id: issues_finder.assignee.try(:id), issue: { assignee_id: finder.assignee.try(:id),
milestone_id: issues_finder.milestones.first.try(:id) }), milestone_id: finder.milestones.first.try(:id) }),
class: "btn btn-new", class: "btn btn-new",
title: "New issue", title: "New issue",
id: "new_issue_link" id: "new_issue_link"
- finder = controller.controller_name == 'issues' ? issues_finder : merge_requests_finder
- boards_page = controller.controller_name == 'boards' - boards_page = controller.controller_name == 'boards'
.issues-filters .issues-filters
......
...@@ -17,60 +17,6 @@ describe IssuableCollections do ...@@ -17,60 +17,6 @@ describe IssuableCollections do
controller controller
end end
describe '#redirect_out_of_range' do
before do
allow(controller).to receive(:url_for)
end
it 'returns true and redirects if the offset is out of range' do
relation = double(:relation, current_page: 10)
expect(controller).to receive(:redirect_to)
expect(controller.send(:redirect_out_of_range, relation, 2)).to eq(true)
end
it 'returns false if the offset is not out of range' do
relation = double(:relation, current_page: 1)
expect(controller).not_to receive(:redirect_to)
expect(controller.send(:redirect_out_of_range, relation, 2)).to eq(false)
end
end
describe '#issues_page_count' do
it 'returns the number of issue pages' do
project = create(:project, :public)
create(:issue, project: project)
finder = IssuesFinder.new(user)
issues = finder.execute
allow(controller).to receive(:issues_finder)
.and_return(finder)
expect(controller.send(:issues_page_count, issues)).to eq(1)
end
end
describe '#merge_requests_page_count' do
it 'returns the number of merge request pages' do
project = create(:project, :public)
create(:merge_request, source_project: project, target_project: project)
finder = MergeRequestsFinder.new(user)
merge_requests = finder.execute
allow(controller).to receive(:merge_requests_finder)
.and_return(finder)
pages = controller.send(:merge_requests_page_count, merge_requests)
expect(pages).to eq(1)
end
end
describe '#page_count_for_relation' do describe '#page_count_for_relation' do
it 'returns the number of pages' do it 'returns the number of pages' do
relation = double(:relation, limit_value: 20) relation = double(:relation, limit_value: 20)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment