Skip to content

Commit

Permalink
Merge branch '42-sortable-resource-items' into 'develop'
Browse files Browse the repository at this point in the history
Resolve "Sortable resource items by manually like custom fields view"

Closes #42

See merge request gtt/redmine_supply!16
  • Loading branch information
sanak committed Mar 30, 2021
2 parents f9177ed + 06afc77 commit ceee24a
Show file tree
Hide file tree
Showing 6 changed files with 58 additions and 13 deletions.
19 changes: 15 additions & 4 deletions app/controllers/resource_items_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class ResourceItemsController < ApplicationController
def index
@resource_items = resource_class.where(project_id: @project.id)
.includes(:category).references(:category)
.order("#{ResourceCategory.table_name}.name ASC, #{ResourceItem.table_name}.name ASC")
.sorted
end

def edit
Expand Down Expand Up @@ -44,9 +44,20 @@ def update
resource_item_params, item: @resource_item, project: @project
)
if r.item_saved?
redirect_to_index
respond_to do |format|
format.html {
flash[:notice] = l(:notice_successful_update)
redirect_to_index
}
format.js { head 200 }
end
else
render 'edit'
respond_to do |format|
format.html {
render 'edit'
}
format.js { head 422 }
end
end
end

Expand Down Expand Up @@ -78,7 +89,7 @@ def resource_class

def resource_item_params
if parameters = params[:human] || params[:asset]
parameters.permit :name, :category_id, :start_date, :end_date
parameters.permit :name, :category_id, :start_date, :end_date, :position
end
end

Expand Down
3 changes: 2 additions & 1 deletion app/models/resource_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ class ResourceItem < ActiveRecord::Base
validates :start_date, :date => true
validates :end_date, :date => true

scope :sorted, ->{ order name: :asc}
acts_as_positioned :scope => [:project_id, :type]
scope :sorted, ->{ order :position }
scope :humans, ->{ where type: 'Human' }
scope :assets, ->{ where type: 'Asset' }
scope :filter_by_date, ->(date = Date.today){
Expand Down
15 changes: 14 additions & 1 deletion app/views/asset_resource_items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<td class="name"><%= link_to i.name, edit_project_asset_resource_item_path(@project, i) %></td>
<td class="date"><%= format_date(i.start_date) %></td>
<td class="date"><%= format_date(i.end_date) %></td>
<td><%= delete_link project_asset_resource_item_path(@project, i) %></td>
<td class="buttons">
<%= reorder_handle(i, url: project_asset_resource_item_path(@project, i)) %>
<%= delete_link project_asset_resource_item_path(@project, i) %>
</td>
</tr>
<% end %>
</tbody>
Expand All @@ -38,3 +41,13 @@
<p class="nodata"><%= l :label_no_data %></p>
<% end %>

<%= javascript_tag do %>
$(function() {
$("table.resource_items tbody").positionedItems({
scroll: false,
sort: function (event, ui) {
ui.helper.css({'top': ui.position.top + $(window).scrollTop() + 'px'});
},
});
});
<% end %>
15 changes: 14 additions & 1 deletion app/views/human_resource_items/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@
<td class="name"><%= link_to i.name, edit_project_human_resource_item_path(@project, i) %></td>
<td class="date"><%= format_date(i.start_date) %></td>
<td class="date"><%= format_date(i.end_date) %></td>
<td><%= delete_link project_human_resource_item_path(@project, i) %></td>
<td class="buttons">
<%= reorder_handle(i, url: project_human_resource_item_path(@project, i)) %>
<%= delete_link project_human_resource_item_path(@project, i) %>
</td>
</tr>
<% end %>
</tbody>
Expand All @@ -38,3 +41,13 @@
<p class="nodata"><%= l :label_no_data %></p>
<% end %>

<%= javascript_tag do %>
$(function() {
$("table.resource_items tbody").positionedItems({
scroll: false,
sort: function (event, ui) {
ui.helper.css({'top': ui.position.top + $(window).scrollTop() + 'px'});
},
});
});
<% end %>
5 changes: 5 additions & 0 deletions db/migrate/20210325123542_add_position_to_resource_items.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddPositionToResourceItems < ActiveRecord::Migration[5.2]
def change
add_column :resource_items, :position, :integer, default: 0
end
end
14 changes: 8 additions & 6 deletions lib/redmine_resource_manager/save_resource_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ def initialize(params, resource_class: nil,


def call
if category_id = @params[:category_id].presence
@item.category = @project.resource_categories.find category_id
end
# if category_id = @params[:category_id].presence
# @item.category = @project.resource_categories.find category_id
# end
@item.project = @project
@item.name = @params[:name]
@item.start_date = @params[:start_date]
@item.end_date = @params[:end_date]
@item.attributes = @params
# @item.name = @params[:name]
# @item.start_date = @params[:start_date]
# @item.end_date = @params[:end_date]
# @item.position = @params[:position]

return Result.new item_saved: @item.save,
item: @item
Expand Down

0 comments on commit ceee24a

Please sign in to comment.