##// END OF EJS Templates
Propose system activities only when reassigning time entries (#13783)....
Jean-Philippe Lang -
r11532:6c39a260a3c4
parent child
Show More
@@ -1,98 +1,98
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class EnumerationsController < ApplicationController
18 class EnumerationsController < ApplicationController
19 layout 'admin'
19 layout 'admin'
20
20
21 before_filter :require_admin, :except => :index
21 before_filter :require_admin, :except => :index
22 before_filter :require_admin_or_api_request, :only => :index
22 before_filter :require_admin_or_api_request, :only => :index
23 before_filter :build_new_enumeration, :only => [:new, :create]
23 before_filter :build_new_enumeration, :only => [:new, :create]
24 before_filter :find_enumeration, :only => [:edit, :update, :destroy]
24 before_filter :find_enumeration, :only => [:edit, :update, :destroy]
25 accept_api_auth :index
25 accept_api_auth :index
26
26
27 helper :custom_fields
27 helper :custom_fields
28
28
29 def index
29 def index
30 respond_to do |format|
30 respond_to do |format|
31 format.html
31 format.html
32 format.api {
32 format.api {
33 @klass = Enumeration.get_subclass(params[:type])
33 @klass = Enumeration.get_subclass(params[:type])
34 if @klass
34 if @klass
35 @enumerations = @klass.shared.sorted.all
35 @enumerations = @klass.shared.sorted.all
36 else
36 else
37 render_404
37 render_404
38 end
38 end
39 }
39 }
40 end
40 end
41 end
41 end
42
42
43 def new
43 def new
44 end
44 end
45
45
46 def create
46 def create
47 if request.post? && @enumeration.save
47 if request.post? && @enumeration.save
48 flash[:notice] = l(:notice_successful_create)
48 flash[:notice] = l(:notice_successful_create)
49 redirect_to enumerations_path
49 redirect_to enumerations_path
50 else
50 else
51 render :action => 'new'
51 render :action => 'new'
52 end
52 end
53 end
53 end
54
54
55 def edit
55 def edit
56 end
56 end
57
57
58 def update
58 def update
59 if request.put? && @enumeration.update_attributes(params[:enumeration])
59 if request.put? && @enumeration.update_attributes(params[:enumeration])
60 flash[:notice] = l(:notice_successful_update)
60 flash[:notice] = l(:notice_successful_update)
61 redirect_to enumerations_path
61 redirect_to enumerations_path
62 else
62 else
63 render :action => 'edit'
63 render :action => 'edit'
64 end
64 end
65 end
65 end
66
66
67 def destroy
67 def destroy
68 if !@enumeration.in_use?
68 if !@enumeration.in_use?
69 # No associated objects
69 # No associated objects
70 @enumeration.destroy
70 @enumeration.destroy
71 redirect_to enumerations_path
71 redirect_to enumerations_path
72 return
72 return
73 elsif params[:reassign_to_id]
73 elsif params[:reassign_to_id]
74 if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id])
74 if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id])
75 @enumeration.destroy(reassign_to)
75 @enumeration.destroy(reassign_to)
76 redirect_to enumerations_path
76 redirect_to enumerations_path
77 return
77 return
78 end
78 end
79 end
79 end
80 @enumerations = @enumeration.class.all - [@enumeration]
80 @enumerations = @enumeration.class.system.all - [@enumeration]
81 end
81 end
82
82
83 private
83 private
84
84
85 def build_new_enumeration
85 def build_new_enumeration
86 class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
86 class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
87 @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
87 @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
88 if @enumeration.nil?
88 if @enumeration.nil?
89 render_404
89 render_404
90 end
90 end
91 end
91 end
92
92
93 def find_enumeration
93 def find_enumeration
94 @enumeration = Enumeration.find(params[:id])
94 @enumeration = Enumeration.find(params[:id])
95 rescue ActiveRecord::RecordNotFound
95 rescue ActiveRecord::RecordNotFound
96 render_404
96 render_404
97 end
97 end
98 end
98 end
@@ -1,141 +1,142
1 # Redmine - project management software
1 # Redmine - project management software
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 #
3 #
4 # This program is free software; you can redistribute it and/or
4 # This program is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License
5 # modify it under the terms of the GNU General Public License
6 # as published by the Free Software Foundation; either version 2
6 # as published by the Free Software Foundation; either version 2
7 # of the License, or (at your option) any later version.
7 # of the License, or (at your option) any later version.
8 #
8 #
9 # This program is distributed in the hope that it will be useful,
9 # This program is distributed in the hope that it will be useful,
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 # GNU General Public License for more details.
12 # GNU General Public License for more details.
13 #
13 #
14 # You should have received a copy of the GNU General Public License
14 # You should have received a copy of the GNU General Public License
15 # along with this program; if not, write to the Free Software
15 # along with this program; if not, write to the Free Software
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17
17
18 class Enumeration < ActiveRecord::Base
18 class Enumeration < ActiveRecord::Base
19 include Redmine::SubclassFactory
19 include Redmine::SubclassFactory
20
20
21 default_scope :order => "#{Enumeration.table_name}.position ASC"
21 default_scope :order => "#{Enumeration.table_name}.position ASC"
22
22
23 belongs_to :project
23 belongs_to :project
24
24
25 acts_as_list :scope => 'type = \'#{type}\''
25 acts_as_list :scope => 'type = \'#{type}\''
26 acts_as_customizable
26 acts_as_customizable
27 acts_as_tree :order => "#{Enumeration.table_name}.position ASC"
27 acts_as_tree :order => "#{Enumeration.table_name}.position ASC"
28
28
29 before_destroy :check_integrity
29 before_destroy :check_integrity
30 before_save :check_default
30 before_save :check_default
31
31
32 attr_protected :type
32 attr_protected :type
33
33
34 validates_presence_of :name
34 validates_presence_of :name
35 validates_uniqueness_of :name, :scope => [:type, :project_id]
35 validates_uniqueness_of :name, :scope => [:type, :project_id]
36 validates_length_of :name, :maximum => 30
36 validates_length_of :name, :maximum => 30
37
37
38 scope :shared, lambda { where(:project_id => nil) }
38 scope :shared, lambda { where(:project_id => nil) }
39 scope :sorted, lambda { order("#{table_name}.position ASC") }
39 scope :sorted, lambda { order("#{table_name}.position ASC") }
40 scope :active, lambda { where(:active => true) }
40 scope :active, lambda { where(:active => true) }
41 scope :system, lambda { where(:project_id => nil) }
41 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
42 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
42
43
43 def self.default
44 def self.default
44 # Creates a fake default scope so Enumeration.default will check
45 # Creates a fake default scope so Enumeration.default will check
45 # it's type. STI subclasses will automatically add their own
46 # it's type. STI subclasses will automatically add their own
46 # types to the finder.
47 # types to the finder.
47 if self.descends_from_active_record?
48 if self.descends_from_active_record?
48 where(:is_default => true, :type => 'Enumeration').first
49 where(:is_default => true, :type => 'Enumeration').first
49 else
50 else
50 # STI classes are
51 # STI classes are
51 where(:is_default => true).first
52 where(:is_default => true).first
52 end
53 end
53 end
54 end
54
55
55 # Overloaded on concrete classes
56 # Overloaded on concrete classes
56 def option_name
57 def option_name
57 nil
58 nil
58 end
59 end
59
60
60 def check_default
61 def check_default
61 if is_default? && is_default_changed?
62 if is_default? && is_default_changed?
62 Enumeration.update_all({:is_default => false}, {:type => type})
63 Enumeration.update_all({:is_default => false}, {:type => type})
63 end
64 end
64 end
65 end
65
66
66 # Overloaded on concrete classes
67 # Overloaded on concrete classes
67 def objects_count
68 def objects_count
68 0
69 0
69 end
70 end
70
71
71 def in_use?
72 def in_use?
72 self.objects_count != 0
73 self.objects_count != 0
73 end
74 end
74
75
75 # Is this enumeration overiding a system level enumeration?
76 # Is this enumeration overiding a system level enumeration?
76 def is_override?
77 def is_override?
77 !self.parent.nil?
78 !self.parent.nil?
78 end
79 end
79
80
80 alias :destroy_without_reassign :destroy
81 alias :destroy_without_reassign :destroy
81
82
82 # Destroy the enumeration
83 # Destroy the enumeration
83 # If a enumeration is specified, objects are reassigned
84 # If a enumeration is specified, objects are reassigned
84 def destroy(reassign_to = nil)
85 def destroy(reassign_to = nil)
85 if reassign_to && reassign_to.is_a?(Enumeration)
86 if reassign_to && reassign_to.is_a?(Enumeration)
86 self.transfer_relations(reassign_to)
87 self.transfer_relations(reassign_to)
87 end
88 end
88 destroy_without_reassign
89 destroy_without_reassign
89 end
90 end
90
91
91 def <=>(enumeration)
92 def <=>(enumeration)
92 position <=> enumeration.position
93 position <=> enumeration.position
93 end
94 end
94
95
95 def to_s; name end
96 def to_s; name end
96
97
97 # Returns the Subclasses of Enumeration. Each Subclass needs to be
98 # Returns the Subclasses of Enumeration. Each Subclass needs to be
98 # required in development mode.
99 # required in development mode.
99 #
100 #
100 # Note: subclasses is protected in ActiveRecord
101 # Note: subclasses is protected in ActiveRecord
101 def self.get_subclasses
102 def self.get_subclasses
102 subclasses
103 subclasses
103 end
104 end
104
105
105 # Does the +new+ Hash override the previous Enumeration?
106 # Does the +new+ Hash override the previous Enumeration?
106 def self.overridding_change?(new, previous)
107 def self.overridding_change?(new, previous)
107 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
108 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
108 return false
109 return false
109 else
110 else
110 return true
111 return true
111 end
112 end
112 end
113 end
113
114
114 # Does the +new+ Hash have the same custom values as the previous Enumeration?
115 # Does the +new+ Hash have the same custom values as the previous Enumeration?
115 def self.same_custom_values?(new, previous)
116 def self.same_custom_values?(new, previous)
116 previous.custom_field_values.each do |custom_value|
117 previous.custom_field_values.each do |custom_value|
117 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
118 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
118 return false
119 return false
119 end
120 end
120 end
121 end
121
122
122 return true
123 return true
123 end
124 end
124
125
125 # Are the new and previous fields equal?
126 # Are the new and previous fields equal?
126 def self.same_active_state?(new, previous)
127 def self.same_active_state?(new, previous)
127 new = (new == "1" ? true : false)
128 new = (new == "1" ? true : false)
128 return new == previous
129 return new == previous
129 end
130 end
130
131
131 private
132 private
132 def check_integrity
133 def check_integrity
133 raise "Can't delete enumeration" if self.in_use?
134 raise "Can't delete enumeration" if self.in_use?
134 end
135 end
135
136
136 end
137 end
137
138
138 # Force load the subclasses in development mode
139 # Force load the subclasses in development mode
139 require_dependency 'time_entry_activity'
140 require_dependency 'time_entry_activity'
140 require_dependency 'document_category'
141 require_dependency 'document_category'
141 require_dependency 'issue_priority'
142 require_dependency 'issue_priority'
General Comments 0
You need to be logged in to leave comments. Login now