Project

General

Profile

Feature #12909 » drag_drop_ordering_resource_trunk_r15195.patch

Takenori TAKAKI, 2016-03-07 07:16

View differences:

app/controllers/custom_fields_controller.rb
class CustomFieldsController < ApplicationController
layout 'admin'
include Redmine::DragDropOrdering::Controller
before_filter :require_admin
before_filter :build_new_custom_field, :only => [:new, :create]
app/controllers/enumerations_controller.rb
class EnumerationsController < ApplicationController
layout 'admin'
include Redmine::DragDropOrdering::Controller
before_filter :require_admin, :except => :index
before_filter :require_admin_or_api_request, :only => :index
app/controllers/issue_statuses_controller.rb
class IssueStatusesController < ApplicationController
layout 'admin'
include Redmine::DragDropOrdering::Controller
before_filter :require_admin, :except => :index
before_filter :require_admin_or_api_request, :only => :index
app/controllers/roles_controller.rb
class RolesController < ApplicationController
layout 'admin'
include Redmine::DragDropOrdering::Controller
before_filter :require_admin, :except => [:index, :show]
before_filter :require_admin_or_api_request, :only => [:index, :show]
app/controllers/trackers_controller.rb
class TrackersController < ApplicationController
layout 'admin'
include Redmine::DragDropOrdering::Controller
before_filter :require_admin, :except => :index
before_filter :require_admin_or_api_request, :only => :index
app/helpers/application_helper.rb
include Redmine::SudoMode::Helper
include Redmine::Themes::Helper
include Redmine::Hook::Helper
include Redmine::DragDropOrdering::Helper
extend Forwardable
def_delegators :wiki_helper, :wikitoolbar_for, :heads_for_wiki_formatter
app/models/custom_field.rb
class CustomField < ActiveRecord::Base
include Redmine::SubclassFactory
include Redmine::DragDropOrdering::Model
has_many :enumerations,
lambda { order(:position) },
app/models/enumeration.rb
class Enumeration < ActiveRecord::Base
include Redmine::SubclassFactory
include Redmine::DragDropOrdering::Model
default_scope lambda {order(:position)}
app/models/issue_status.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class IssueStatus < ActiveRecord::Base
include Redmine::DragDropOrdering::Model
before_destroy :check_integrity
has_many :workflows, :class_name => 'WorkflowTransition', :foreign_key => "old_status_id"
has_many :workflow_transitions_as_new_status, :class_name => 'WorkflowTransition', :foreign_key => "new_status_id"
app/models/role.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class Role < ActiveRecord::Base
include Redmine::DragDropOrdering::Model
# Custom coder for the permissions attribute that should be an
# array of symbols. Rails 3 uses Psych which can be *unbelievably*
# slow on some platforms (eg. mingw32).
app/models/tracker.rb
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
class Tracker < ActiveRecord::Base
include Redmine::DragDropOrdering::Model
CORE_FIELDS_UNDISABLABLE = %w(project_id tracker_id subject description priority_id is_private).freeze
# Fields that can be disabled
app/views/custom_fields/_index.html.erb
<table class="list">
<%= form_tag reorder_all_custom_fields_path, :method => 'post', :id => 'sortable_form' do %>
<table class="list" id="sortable_table">
<thead><tr>
<th><%=l(:field_name)%></th>
<th><%=l(:field_field_format)%></th>
......
<% (@custom_fields_by_type[tab[:name]] || []).sort.each do |custom_field| -%>
<% back_url = custom_fields_path(:tab => tab[:name]) %>
<tr class="<%= cycle("odd", "even") %>">
<td class="name"><%= link_to custom_field.name, edit_custom_field_path(custom_field) %></td>
<td class="name">
<span class="sort-handle ui-icon ui-icon-arrowthick-2-n-s"></span>
<%= hidden_field_tag "id_and_positions[#{custom_field.id}]", custom_field.position, :class => 'position' %>
<%= link_to custom_field.name, edit_custom_field_path(custom_field) %>
</td>
<td><%= l(custom_field.format.label) %></td>
<td><%= checked_image custom_field.is_required? %></td>
<% if tab[:name] == 'IssueCustomField' %>
......
<% end; reset_cycle %>
</tbody>
</table>
<% end %>
<%= sortable_js('sortable_form', 'sortable_table') %>
app/views/enumerations/index.html.erb
<% enumerations = klass.shared %>
<% if enumerations.any? %>
<table class="list"><thead>
<%= form_tag reorder_all_enumerations_path, :method => 'post', :id => "#{klass.name.underscore}_form" do %>
<table class="list" id="<%= "#{klass.name.underscore}_table" %>"><thead>
<tr>
<th><%= l(:field_name) %></th>
<th><%= l(:field_is_default) %></th>
......
</tr></thead>
<% enumerations.each do |enumeration| %>
<tr class="<%= cycle('odd', 'even') %>">
<td class="name"><%= link_to enumeration, edit_enumeration_path(enumeration) %></td>
<td class="name">
<span class="sort-handle ui-icon ui-icon-arrowthick-2-n-s"></span>
<%= hidden_field_tag "id_and_positions[#{enumeration.id}]", enumeration.position, :class => 'position' %>
<%= link_to enumeration, edit_enumeration_path(enumeration) %>
</td>
<td class="tick"><%= checked_image enumeration.is_default? %></td>
<td class="tick"><%= checked_image enumeration.active? %></td>
<td class="reorder"><%= reorder_links('enumeration', {:action => 'update', :id => enumeration}, :put) %></td>
......
</tr>
<% end %>
</table>
<% end %>
<%= sortable_js("#{klass.name.underscore}_form", "#{klass.name.underscore}_table") %>
<% reset_cycle %>
<% end %>
app/views/issue_statuses/index.html.erb
<h2><%=l(:label_issue_status_plural)%></h2>
<table class="list">
<%= form_tag reorder_all_issue_statuses_path, :method => 'post', :id => 'sortable_form' do %>
<table class="list" id="sortable_table">
<thead><tr>
<th><%=l(:field_status)%></th>
<% if Issue.use_status_for_done_ratio? %>
......
<tbody>
<% for status in @issue_statuses %>
<tr class="<%= cycle("odd", "even") %>">
<td class="name"><%= link_to status.name, edit_issue_status_path(status) %></td>
<td class="name">
<span class="sort-handle ui-icon ui-icon-arrowthick-2-n-s"></span>
<%= hidden_field_tag "id_and_positions[#{status.id}]", status.position, :class => 'position' %>
<%= link_to status.name, edit_issue_status_path(status) %>
</td>
<% if Issue.use_status_for_done_ratio? %>
<td><%= status.default_done_ratio %></td>
<% end %>
......
<% end %>
</tbody>
</table>
<% end %>
<span class="pagination"><%= pagination_links_full @issue_status_pages %></span>
<% html_title(l(:label_issue_status_plural)) -%>
<%= sortable_js('sortable_form', 'sortable_table') %>
app/views/roles/index.html.erb
<h2><%=l(:label_role_plural)%></h2>
<table class="list">
<%= form_tag reorder_all_roles_path, :method => 'post', :id => 'sortable_form' do %>
<table class="list" id="sortable_table">
<thead><tr>
<th><%=l(:label_role)%></th>
<th><%=l(:button_sort)%></th>
......
<tbody>
<% for role in @roles %>
<tr class="<%= cycle("odd", "even") %>">
<td class="name"><%= content_tag(role.builtin? ? 'em' : 'span', link_to(role.name, edit_role_path(role))) %></td>
<td class="name">
<% unless role.builtin? %>
<span class="sort-handle ui-icon ui-icon-arrowthick-2-n-s"></span>
<%= hidden_field_tag "id_and_positions[#{role.id}]", role.position, :class => 'position' %>
<% end %>
<%= content_tag(role.builtin? ? 'em' : 'span', link_to(role.name, edit_role_path(role))) %>
</td>
<td class="reorder">
<% unless role.builtin? %>
<%= reorder_links('role', {:action => 'update', :id => role, :page => params[:page]}, :put) %>
......
<% end %>
</tbody>
</table>
<% end %>
<span class="pagination"><%= pagination_links_full @role_pages %></span>
<% html_title(l(:label_role_plural)) -%>
<%= sortable_js('sortable_form', 'sortable_table') %>
app/views/trackers/index.html.erb
<h2><%=l(:label_tracker_plural)%></h2>
<table class="list">
<%= form_tag reorder_all_trackers_path, :method => 'post', :id => 'sortable_form' do %>
<table class="list" id="sortable_table">
<thead><tr>
<th><%=l(:label_tracker)%></th>
<th></th>
......
<tbody>
<% for tracker in @trackers %>
<tr class="<%= cycle("odd", "even") %>">
<td class="name"><%= link_to tracker.name, edit_tracker_path(tracker) %></td>
<td class="name">
<span class="sort-handle ui-icon ui-icon-arrowthick-2-n-s"></span>
<%= hidden_field_tag "id_and_positions[#{tracker.id}]", tracker.position, :class => 'position' %>
<%= link_to tracker.name, edit_tracker_path(tracker) %>
</td>
<td>
<% unless tracker.workflow_rules.count > 0 %>
<span class="icon icon-warning">
......
<% end %>
</tbody>
</table>
<% end %>
<span class="pagination"><%= pagination_links_full @tracker_pages %></span>
<% html_title(l(:label_tracker_plural)) -%>
<%= sortable_js('sortable_form', 'sortable_table') %>
config/routes.rb
end
end
end
extend Redmine::DragDropOrdering::Routes
end
lib/redmine.rb
require 'redmine/hook/listener'
require 'redmine/hook/view_listener'
require 'redmine/plugin'
require 'redmine/drag_drop_ordering'
Redmine::Scm::Base.add "Subversion"
Redmine::Scm::Base.add "Darcs"
lib/redmine/drag_drop_ordering.rb
require 'active_support/concern'
module Redmine
module DragDropOrdering
module Model
extend ActiveSupport::Concern
module ClassMethods
def reorder_all(id_and_positions)
return unless id_and_positions.is_a?(Hash)
transaction do
id_and_positions.each do |obj_id, position|
obj = self.find(obj_id)
obj.position = position.to_i
unless obj.save
raise ActiveRecord::Rollback
end
end
end
end
end
end
module Controller
def reorder_all
model_klass = self.controller_name.classify.constantize
model_klass.reorder_all(params[:id_and_positions])
redirect_to :action => :index
end
end
module Routes
def self.extended(routes)
routes.instance_exec do
concern :sortable do
post :reorder_all, :on => :collection
end
resources :issue_statuses, :concerns => :sortable
resources :custom_fields, :concerns => :sortable
resources :enumerations, :concerns => :sortable
resources :trackers, :concerns => :sortable
resources :roles, :concerns => :sortable
end
end
end
module Helper
def sortable_js(form_id, table_id)
js = <<-"EOS"
$(function() {
$('##{table_id} tbody').sortable({
handle: '.sort-handle',
update: function(event, ui) {
$('##{table_id} tr').each(function(){
$(this).find('input.position').val($(this).index()+1);
});
$('form##{form_id}').submit();
}
});
});
EOS
javascript_tag(js.html_safe)
end
end
end
end
test/integration/lib/redmine/drag_drop_ordering_test.rb
# Redmine - project management software
# Copyright (C) 2006-2015 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../../../test_helper', __FILE__)
class DragDropOrderingTest < Redmine::IntegrationTest
fixtures :users, :custom_fields, :enumerations, :issue_statuses, :roles, :trackers
def setup
log_user("admin", "admin")
end
def test_reorder_all_uri
resource_names = ['custom_fields', 'enumerations', 'issue_statuses', 'roles', 'trackers']
resource_names.each do |resource_name|
# custom_fields --> CustomField
model_klass = resource_name.classify.constantize
desc_resource_ids = model_klass.all.order('position DESC').pluck(:id)
new_positions = (1..desc_resource_ids.count).to_a
# ex) {"3"=>"1", "2"=>"2", "1"=>"3"}
id_and_positions = Hash[*desc_resource_ids.zip(new_positions).flatten]
# ex) post "/custom_fields/reorder_all", {'id_and_positions' => {"3"=>"1","2"=>"2","3"=>"1"}}
post "/#{resource_name}/reorder_all", {'id_and_positions' => id_and_positions}
assert_redirected_to "/#{resource_name}"
id_and_positions.each do |id, position|
assert_equal position, model_klass.find(id).position
end
asc_resource_ids = model_klass.all.order('position ASC').pluck(:id)
assert_equal asc_resource_ids, desc_resource_ids
end
end
end
test/integration/routing/reorder_all_test.rb
# Redmine - project management software
# Copyright (C) 2006-2015 Jean-Philippe Lang
#
# This program is free software; you can redistribute it and/or
# modify it under the terms of the GNU General Public License
# as published by the Free Software Foundation; either version 2
# of the License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
require File.expand_path('../../../test_helper', __FILE__)
class ReorderAllTest < Redmine::RoutingTest
def test_reorder_all
resource_names = ['custom_fields', 'enumerations', 'issue_statuses', 'roles', 'trackers']
resource_names.each do |resource_name|
should_route "POST /#{resource_name}/reorder_all" => "#{resource_name}#reorder_all"
end
end
end
(1-1/2)