From 5944696b6d4307de0166ba74c514aae25779abb9 Mon Sep 17 00:00:00 2001 From: Jean-Philippe Lang Date: Mon, 12 Nov 2007 17:23:14 +0000 Subject: [PATCH] Custom fields can now be reordered. git-svn-id: http://redmine.rubyforge.org/svn/trunk@901 e93f8b46-1217-0410-a6f0-8f06a7374b81 --- app/controllers/custom_fields_controller.rb | 21 ++++++++++++++++++--- app/controllers/issues_controller.rb | 2 +- app/controllers/projects_controller.rb | 12 ++++++------ app/controllers/users_controller.rb | 8 ++++---- app/models/custom_field.rb | 5 +++++ app/models/project.rb | 8 +++++++- app/views/custom_fields/list.rhtml | 9 ++++++++- db/migrate/078_add_custom_fields_position.rb | 15 +++++++++++++++ 8 files changed, 64 insertions(+), 16 deletions(-) create mode 100644 db/migrate/078_add_custom_fields_position.rb diff --git a/app/controllers/custom_fields_controller.rb b/app/controllers/custom_fields_controller.rb index 782de66a..1e1c988d 100644 --- a/app/controllers/custom_fields_controller.rb +++ b/app/controllers/custom_fields_controller.rb @@ -45,7 +45,7 @@ class CustomFieldsController < ApplicationController end if request.post? and @custom_field.save flash[:notice] = l(:notice_successful_create) - redirect_to :action => 'list', :tab => @custom_field.type + redirect_to :action => 'list', :tab => @custom_field.class.name end @trackers = Tracker.find(:all, :order => 'position') end @@ -57,14 +57,29 @@ class CustomFieldsController < ApplicationController @custom_field.trackers = params[:tracker_ids] ? Tracker.find(params[:tracker_ids]) : [] end flash[:notice] = l(:notice_successful_update) - redirect_to :action => 'list', :tab => @custom_field.type + redirect_to :action => 'list', :tab => @custom_field.class.name end @trackers = Tracker.find(:all, :order => 'position') end + def move + @custom_field = CustomField.find(params[:id]) + case params[:position] + when 'highest' + @custom_field.move_to_top + when 'higher' + @custom_field.move_higher + when 'lower' + @custom_field.move_lower + when 'lowest' + @custom_field.move_to_bottom + end if params[:position] + redirect_to :action => 'list', :tab => @custom_field.class.name + end + def destroy @custom_field = CustomField.find(params[:id]).destroy - redirect_to :action => 'list', :tab => @custom_field.type + redirect_to :action => 'list', :tab => @custom_field.class.name rescue flash[:error] = "Unable to delete custom field" redirect_to :action => 'list' diff --git a/app/controllers/issues_controller.rb b/app/controllers/issues_controller.rb index 602d551f..a1f91024 100644 --- a/app/controllers/issues_controller.rb +++ b/app/controllers/issues_controller.rb @@ -80,7 +80,7 @@ class IssuesController < ApplicationController end def show - @custom_values = @issue.custom_values.find(:all, :include => :custom_field) + @custom_values = @issue.custom_values.find(:all, :include => :custom_field, :order => "#{CustomField.table_name}.position") @journals = @issue.journals.find(:all, :include => [:user, :details], :order => "#{Journal.table_name}.created_on ASC") @status_options = @issue.status.find_new_statuses_allowed_to(logged_in_user.role_for_project(@project), @issue.tracker) if logged_in_user respond_to do |format| diff --git a/app/controllers/projects_controller.rb b/app/controllers/projects_controller.rb index f67a1caa..e708dd6f 100644 --- a/app/controllers/projects_controller.rb +++ b/app/controllers/projects_controller.rb @@ -56,15 +56,15 @@ class ProjectsController < ApplicationController # Add a new project def add - @custom_fields = IssueCustomField.find(:all) + @custom_fields = IssueCustomField.find(:all, :order => "#{CustomField.table_name}.position") @root_projects = Project.find(:all, :conditions => "parent_id IS NULL AND status = #{Project::STATUS_ACTIVE}") @project = Project.new(params[:project]) @project.enabled_module_names = Redmine::AccessControl.available_project_modules if request.get? - @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project) } + @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project) } else @project.custom_fields = CustomField.find(params[:custom_field_ids]) if params[:custom_field_ids] - @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) } + @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) } @project.custom_values = @custom_values if @project.save @project.enabled_module_names = params[:enabled_modules] @@ -76,7 +76,7 @@ class ProjectsController < ApplicationController # Show @project def show - @custom_values = @project.custom_values.find(:all, :include => :custom_field) + @custom_values = @project.custom_values.find(:all, :include => :custom_field, :order => "#{CustomField.table_name}.position") @members_by_role = @project.members.find(:all, :include => [:user, :role], :order => 'position').group_by {|m| m.role} @subprojects = @project.active_children @news = @project.news.find(:all, :limit => 5, :include => [ :author, :project ], :order => "#{News.table_name}.created_on DESC") @@ -92,7 +92,7 @@ class ProjectsController < ApplicationController @custom_fields = IssueCustomField.find(:all) @issue_category ||= IssueCategory.new @member ||= @project.members.new - @custom_values ||= ProjectCustomField.find(:all).collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) } + @custom_values ||= ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| @project.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) } @repository ||= @project.repository @wiki ||= @project.wiki end @@ -102,7 +102,7 @@ class ProjectsController < ApplicationController if request.post? @project.custom_fields = IssueCustomField.find(params[:custom_field_ids]) if params[:custom_field_ids] if params[:custom_fields] - @custom_values = ProjectCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) } + @custom_values = ProjectCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @project, :value => params["custom_fields"][x.id.to_s]) } @project.custom_values = @custom_values end @project.attributes = params[:project] diff --git a/app/controllers/users_controller.rb b/app/controllers/users_controller.rb index 85579820..cf0128d7 100644 --- a/app/controllers/users_controller.rb +++ b/app/controllers/users_controller.rb @@ -52,13 +52,13 @@ class UsersController < ApplicationController def add if request.get? @user = User.new(:language => Setting.default_language) - @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user) } + @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @user) } else @user = User.new(params[:user]) @user.admin = params[:user][:admin] || false @user.login = params[:user][:login] @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless @user.auth_source_id - @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) } + @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => (params[:custom_fields] ? params["custom_fields"][x.id.to_s] : nil)) } @user.custom_values = @custom_values if @user.save Mailer.deliver_account_information(@user, params[:password]) if params[:send_information] @@ -72,13 +72,13 @@ class UsersController < ApplicationController def edit @user = User.find(params[:id]) if request.get? - @custom_values = UserCustomField.find(:all).collect { |x| @user.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) } + @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| @user.custom_values.find_by_custom_field_id(x.id) || CustomValue.new(:custom_field => x) } else @user.admin = params[:user][:admin] if params[:user][:admin] @user.login = params[:user][:login] if params[:user][:login] @user.password, @user.password_confirmation = params[:password], params[:password_confirmation] unless params[:password].nil? or params[:password].empty? or @user.auth_source_id if params[:custom_fields] - @custom_values = UserCustomField.find(:all).collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => params["custom_fields"][x.id.to_s]) } + @custom_values = UserCustomField.find(:all, :order => "#{CustomField.table_name}.position").collect { |x| CustomValue.new(:custom_field => x, :customized => @user, :value => params["custom_fields"][x.id.to_s]) } @user.custom_values = @custom_values end if @user.update_attributes(params[:user]) diff --git a/app/models/custom_field.rb b/app/models/custom_field.rb index 81c8aae0..c3b5f2a9 100644 --- a/app/models/custom_field.rb +++ b/app/models/custom_field.rb @@ -17,6 +17,7 @@ class CustomField < ActiveRecord::Base has_many :custom_values, :dependent => :delete_all + acts_as_list :scope => 'type = \'#{self.class}\'' serialize :possible_values FIELD_FORMATS = { "string" => { :name => :label_string, :order => 1 }, @@ -51,6 +52,10 @@ class CustomField < ActiveRecord::Base end end + def <=>(field) + position <=> field.position + end + # to move in project_custom_field def self.for_all find(:all, :conditions => ["is_for_all=?", true]) diff --git a/app/models/project.rb b/app/models/project.rb index 152808c1..1fbab2e4 100644 --- a/app/models/project.rb +++ b/app/models/project.rb @@ -36,7 +36,13 @@ class Project < ActiveRecord::Base has_one :repository, :dependent => :destroy has_many :changesets, :through => :repository has_one :wiki, :dependent => :destroy - has_and_belongs_to_many :custom_fields, :class_name => 'IssueCustomField', :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", :association_foreign_key => 'custom_field_id' + # Custom field for the project issues + has_and_belongs_to_many :custom_fields, + :class_name => 'IssueCustomField', + :order => "#{CustomField.table_name}.position", + :join_table => "#{table_name_prefix}custom_fields_projects#{table_name_suffix}", + :association_foreign_key => 'custom_field_id' + acts_as_tree :order => "name", :counter_cache => true acts_as_searchable :columns => ['name', 'description'], :project_key => 'id' diff --git a/app/views/custom_fields/list.rhtml b/app/views/custom_fields/list.rhtml index b6f1f278..8862b3de 100644 --- a/app/views/custom_fields/list.rhtml +++ b/app/views/custom_fields/list.rhtml @@ -19,10 +19,11 @@ <%=l(:field_is_for_all)%> <%=l(:label_used_by)%> <% end %> + <%=l(:button_sort)%> -<% for custom_field in (@custom_fields_by_type[type] || []) %> +<% for custom_field in (@custom_fields_by_type[type] || []).sort %> "> <%= link_to custom_field.name, :action => 'edit', :id => custom_field %> <%= l(CustomField::FIELD_FORMATS[custom_field.field_format][:name]) %> @@ -31,6 +32,12 @@ <%= image_tag 'true.png' if custom_field.is_for_all? %> <%= custom_field.projects.count.to_s + ' ' + lwr(:label_project, custom_field.projects.count) if custom_field.is_a? IssueCustomField and !custom_field.is_for_all? %> <% end %> + + <%= link_to image_tag('2uparrow.png', :alt => l(:label_sort_highest)), {:action => 'move', :id => custom_field, :position => 'highest'}, :method => :post, :title => l(:label_sort_highest) %> + <%= link_to image_tag('1uparrow.png', :alt => l(:label_sort_higher)), {:action => 'move', :id => custom_field, :position => 'higher'}, :method => :post, :title => l(:label_sort_higher) %> - + <%= link_to image_tag('1downarrow.png', :alt => l(:label_sort_lower)), {:action => 'move', :id => custom_field, :position => 'lower'}, :method => :post, :title => l(:label_sort_lower) %> + <%= link_to image_tag('2downarrow.png', :alt => l(:label_sort_lowest)), {:action => 'move', :id => custom_field, :position => 'lowest'}, :method => :post, :title => l(:label_sort_lowest) %> + <%= button_to l(:button_delete), { :action => 'destroy', :id => custom_field }, :confirm => l(:text_are_you_sure), :class => "button-small" %> diff --git a/db/migrate/078_add_custom_fields_position.rb b/db/migrate/078_add_custom_fields_position.rb new file mode 100644 index 00000000..7ee8abb5 --- /dev/null +++ b/db/migrate/078_add_custom_fields_position.rb @@ -0,0 +1,15 @@ +class AddCustomFieldsPosition < ActiveRecord::Migration + def self.up + add_column(:custom_fields, :position, :integer, :default => 1) + CustomField.find(:all).group_by(&:type).each_value do |fields| + fields.each_with_index do |field, i| + # do not call model callbacks + CustomField.update_all "position = #{i+1}", {:id => field.id} + end + end + end + + def self.down + remove_column :custom_fields, :position + end +end -- 2.11.0