Skip to content

Commit 0b00b3d

Browse files
committed
Add sorting to merge requests on milestone page
Signed-off-by: Dmitriy Zaporozhets <[email protected]>
1 parent 1d77357 commit 0b00b3d

File tree

7 files changed

+41
-8
lines changed

7 files changed

+41
-8
lines changed

app/assets/javascripts/milestone.js.coffee

+16
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,18 @@ class Milestone
2323
new Flash("Issues update failed", 'alert')
2424
dataType: "json"
2525

26+
@sortMergeRequests: (data) ->
27+
sort_mr_url = location.href + "/sort_merge_requests"
28+
29+
$.ajax
30+
type: "PUT"
31+
url: sort_mr_url
32+
data: data
33+
success: (data) ->
34+
if data.saved != true
35+
new Flash("MR update failed", 'alert')
36+
dataType: "json"
37+
2638
@updateMergeRequest: (li, merge_request_url, data) ->
2739
$.ajax
2840
type: "PUT"
@@ -71,6 +83,10 @@ class Milestone
7183
$("#merge_requests-list-unassigned, #merge_requests-list-ongoing, #merge_requests-list-closed").sortable(
7284
connectWith: ".merge_requests-sortable-list",
7385
dropOnEmpty: true,
86+
update: (event, ui) ->
87+
data = $(this).sortable("serialize")
88+
Milestone.sortMergeRequests(data)
89+
7490
receive: (event, ui) ->
7591
new_state = $(this).data('state')
7692
merge_request_id = ui.item.data('iid')

app/controllers/projects/milestones_controller.rb

+11-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
class Projects::MilestonesController < Projects::ApplicationController
22
before_filter :module_enabled
3-
before_filter :milestone, only: [:edit, :update, :destroy, :show, :sort_issues]
3+
before_filter :milestone, only: [:edit, :update, :destroy, :show, :sort_issues, :sort_merge_requests]
44

55
# Allow read any milestone
66
before_filter :authorize_read_milestone!
@@ -82,6 +82,16 @@ def sort_issues
8282
render json: { saved: true }
8383
end
8484

85+
def sort_merge_requests
86+
@merge_requests = @milestone.merge_requests.where(id: params['sortable_merge_request'])
87+
@merge_requests.each do |merge_request|
88+
merge_request.position = params['sortable_merge_request'].index(merge_request.id.to_s) + 1
89+
merge_request.save
90+
end
91+
92+
render json: { saved: true }
93+
end
94+
8595
protected
8696

8797
def milestone

app/views/projects/milestones/_merge_request.html.haml

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
%li{ class: 'mr-row', 'data-iid' => merge_request.iid, 'data-url' => project_merge_request_path(@project, merge_request) }
1+
%li{ id: dom_id(merge_request, 'sortable'), class: 'mr-row', 'data-iid' => merge_request.iid, 'data-url' => project_merge_request_path(@project, merge_request) }
22
%span.str-truncated
33
= link_to [@project, merge_request] do
44
%span.cgray ##{merge_request.iid}
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
.panel.panel-default
22
.panel-heading= title
33
%ul{ class: "well-list merge_requests-sortable-list", id: "merge_requests-list-#{id}", "data-state" => id }
4-
- merge_requests.each do |merge_request|
4+
- merge_requests.sort_by(&:position).each do |merge_request|
55
= render 'merge_request', merge_request: merge_request
66
%li.light Drag and drop available

config/routes.rb

+1
Original file line numberDiff line numberDiff line change
@@ -281,6 +281,7 @@
281281
resources :milestones, except: [:destroy], constraints: {id: /\d+/} do
282282
member do
283283
put :sort_issues
284+
put :sort_merge_requests
284285
end
285286
end
286287

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPositionToMergeRequest < ActiveRecord::Migration
2+
def change
3+
add_column :merge_requests, :position, :integer, default: 0
4+
end
5+
end

db/schema.rb

+6-5
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
#
1212
# It's strongly recommended that you check this file into your version control system.
1313

14-
ActiveRecord::Schema.define(version: 20140502125220) do
14+
ActiveRecord::Schema.define(version: 20140611135229) do
1515

1616
# These are extensions that must be enabled in order to support this database
1717
enable_extension "plpgsql"
@@ -121,9 +121,9 @@
121121
add_index "merge_request_diffs", ["merge_request_id"], name: "index_merge_request_diffs_on_merge_request_id", unique: true, using: :btree
122122

123123
create_table "merge_requests", force: true do |t|
124-
t.string "target_branch", null: false
125-
t.string "source_branch", null: false
126-
t.integer "source_project_id", null: false
124+
t.string "target_branch", null: false
125+
t.string "source_branch", null: false
126+
t.integer "source_project_id", null: false
127127
t.integer "author_id"
128128
t.integer "assignee_id"
129129
t.string "title"
@@ -132,9 +132,10 @@
132132
t.integer "milestone_id"
133133
t.string "state"
134134
t.string "merge_status"
135-
t.integer "target_project_id", null: false
135+
t.integer "target_project_id", null: false
136136
t.integer "iid"
137137
t.text "description"
138+
t.integer "position", default: 0
138139
end
139140

140141
add_index "merge_requests", ["assignee_id"], name: "index_merge_requests_on_assignee_id", using: :btree

0 commit comments

Comments
 (0)