Skip to content

Commit ceee24a

Browse files
committed
Merge branch '42-sortable-resource-items' into 'develop'
Resolve "Sortable resource items by manually like custom fields view" Closes #42 See merge request gtt/redmine_supply!16
2 parents f9177ed + 06afc77 commit ceee24a

File tree

6 files changed

+58
-13
lines changed

6 files changed

+58
-13
lines changed

app/controllers/resource_items_controller.rb

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class ResourceItemsController < ApplicationController
88
def index
99
@resource_items = resource_class.where(project_id: @project.id)
1010
.includes(:category).references(:category)
11-
.order("#{ResourceCategory.table_name}.name ASC, #{ResourceItem.table_name}.name ASC")
11+
.sorted
1212
end
1313

1414
def edit
@@ -44,9 +44,20 @@ def update
4444
resource_item_params, item: @resource_item, project: @project
4545
)
4646
if r.item_saved?
47-
redirect_to_index
47+
respond_to do |format|
48+
format.html {
49+
flash[:notice] = l(:notice_successful_update)
50+
redirect_to_index
51+
}
52+
format.js { head 200 }
53+
end
4854
else
49-
render 'edit'
55+
respond_to do |format|
56+
format.html {
57+
render 'edit'
58+
}
59+
format.js { head 422 }
60+
end
5061
end
5162
end
5263

@@ -78,7 +89,7 @@ def resource_class
7889

7990
def resource_item_params
8091
if parameters = params[:human] || params[:asset]
81-
parameters.permit :name, :category_id, :start_date, :end_date
92+
parameters.permit :name, :category_id, :start_date, :end_date, :position
8293
end
8394
end
8495

app/models/resource_item.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@ class ResourceItem < ActiveRecord::Base
1212
validates :start_date, :date => true
1313
validates :end_date, :date => true
1414

15-
scope :sorted, ->{ order name: :asc}
15+
acts_as_positioned :scope => [:project_id, :type]
16+
scope :sorted, ->{ order :position }
1617
scope :humans, ->{ where type: 'Human' }
1718
scope :assets, ->{ where type: 'Asset' }
1819
scope :filter_by_date, ->(date = Date.today){

app/views/asset_resource_items/index.html.erb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<td class="name"><%= link_to i.name, edit_project_asset_resource_item_path(@project, i) %></td>
3030
<td class="date"><%= format_date(i.start_date) %></td>
3131
<td class="date"><%= format_date(i.end_date) %></td>
32-
<td><%= delete_link project_asset_resource_item_path(@project, i) %></td>
32+
<td class="buttons">
33+
<%= reorder_handle(i, url: project_asset_resource_item_path(@project, i)) %>
34+
<%= delete_link project_asset_resource_item_path(@project, i) %>
35+
</td>
3336
</tr>
3437
<% end %>
3538
</tbody>
@@ -38,3 +41,13 @@
3841
<p class="nodata"><%= l :label_no_data %></p>
3942
<% end %>
4043

44+
<%= javascript_tag do %>
45+
$(function() {
46+
$("table.resource_items tbody").positionedItems({
47+
scroll: false,
48+
sort: function (event, ui) {
49+
ui.helper.css({'top': ui.position.top + $(window).scrollTop() + 'px'});
50+
},
51+
});
52+
});
53+
<% end %>

app/views/human_resource_items/index.html.erb

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,10 @@
2929
<td class="name"><%= link_to i.name, edit_project_human_resource_item_path(@project, i) %></td>
3030
<td class="date"><%= format_date(i.start_date) %></td>
3131
<td class="date"><%= format_date(i.end_date) %></td>
32-
<td><%= delete_link project_human_resource_item_path(@project, i) %></td>
32+
<td class="buttons">
33+
<%= reorder_handle(i, url: project_human_resource_item_path(@project, i)) %>
34+
<%= delete_link project_human_resource_item_path(@project, i) %>
35+
</td>
3336
</tr>
3437
<% end %>
3538
</tbody>
@@ -38,3 +41,13 @@
3841
<p class="nodata"><%= l :label_no_data %></p>
3942
<% end %>
4043

44+
<%= javascript_tag do %>
45+
$(function() {
46+
$("table.resource_items tbody").positionedItems({
47+
scroll: false,
48+
sort: function (event, ui) {
49+
ui.helper.css({'top': ui.position.top + $(window).scrollTop() + 'px'});
50+
},
51+
});
52+
});
53+
<% end %>
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class AddPositionToResourceItems < ActiveRecord::Migration[5.2]
2+
def change
3+
add_column :resource_items, :position, :integer, default: 0
4+
end
5+
end

lib/redmine_resource_manager/save_resource_item.rb

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,15 @@ def initialize(params, resource_class: nil,
1717

1818

1919
def call
20-
if category_id = @params[:category_id].presence
21-
@item.category = @project.resource_categories.find category_id
22-
end
20+
# if category_id = @params[:category_id].presence
21+
# @item.category = @project.resource_categories.find category_id
22+
# end
2323
@item.project = @project
24-
@item.name = @params[:name]
25-
@item.start_date = @params[:start_date]
26-
@item.end_date = @params[:end_date]
24+
@item.attributes = @params
25+
# @item.name = @params[:name]
26+
# @item.start_date = @params[:start_date]
27+
# @item.end_date = @params[:end_date]
28+
# @item.position = @params[:position]
2729

2830
return Result.new item_saved: @item.save,
2931
item: @item

0 commit comments

Comments
 (0)