@@ -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' |
@@ -1,34 +1,38 | |||||
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 TimeEntryActivity < Enumeration |
|
18 | class TimeEntryActivity < Enumeration | |
19 | has_many :time_entries, :foreign_key => 'activity_id' |
|
19 | has_many :time_entries, :foreign_key => 'activity_id' | |
20 |
|
20 | |||
21 | OptionName = :enumeration_activities |
|
21 | OptionName = :enumeration_activities | |
22 |
|
22 | |||
23 | def option_name |
|
23 | def option_name | |
24 | OptionName |
|
24 | OptionName | |
25 | end |
|
25 | end | |
26 |
|
26 | |||
|
27 | def objects | |||
|
28 | TimeEntry.where(:activity_id => self_and_descendants(1).map(&:id)) | |||
|
29 | end | |||
|
30 | ||||
27 | def objects_count |
|
31 | def objects_count | |
28 |
|
|
32 | objects.count | |
29 | end |
|
33 | end | |
30 |
|
34 | |||
31 | def transfer_relations(to) |
|
35 | def transfer_relations(to) | |
32 |
|
|
36 | objects.update_all(:activity_id => to.id) | |
33 | end |
|
37 | end | |
34 | end |
|
38 | end |
@@ -1,150 +1,151 | |||||
1 | module ObjectHelpers |
|
1 | module ObjectHelpers | |
2 | def User.generate!(attributes={}) |
|
2 | def User.generate!(attributes={}) | |
3 | @generated_user_login ||= 'user0' |
|
3 | @generated_user_login ||= 'user0' | |
4 | @generated_user_login.succ! |
|
4 | @generated_user_login.succ! | |
5 | user = User.new(attributes) |
|
5 | user = User.new(attributes) | |
6 | user.login = @generated_user_login.dup if user.login.blank? |
|
6 | user.login = @generated_user_login.dup if user.login.blank? | |
7 | user.mail = "#{@generated_user_login}@example.com" if user.mail.blank? |
|
7 | user.mail = "#{@generated_user_login}@example.com" if user.mail.blank? | |
8 | user.firstname = "Bob" if user.firstname.blank? |
|
8 | user.firstname = "Bob" if user.firstname.blank? | |
9 | user.lastname = "Doe" if user.lastname.blank? |
|
9 | user.lastname = "Doe" if user.lastname.blank? | |
10 | yield user if block_given? |
|
10 | yield user if block_given? | |
11 | user.save! |
|
11 | user.save! | |
12 | user |
|
12 | user | |
13 | end |
|
13 | end | |
14 |
|
14 | |||
15 | def User.add_to_project(user, project, roles=nil) |
|
15 | def User.add_to_project(user, project, roles=nil) | |
16 | roles = Role.find(1) if roles.nil? |
|
16 | roles = Role.find(1) if roles.nil? | |
17 | roles = [roles] unless roles.is_a?(Array) |
|
17 | roles = [roles] unless roles.is_a?(Array) | |
18 | Member.create!(:principal => user, :project => project, :roles => roles) |
|
18 | Member.create!(:principal => user, :project => project, :roles => roles) | |
19 | end |
|
19 | end | |
20 |
|
20 | |||
21 | def Group.generate!(attributes={}) |
|
21 | def Group.generate!(attributes={}) | |
22 | @generated_group_name ||= 'Group 0' |
|
22 | @generated_group_name ||= 'Group 0' | |
23 | @generated_group_name.succ! |
|
23 | @generated_group_name.succ! | |
24 | group = Group.new(attributes) |
|
24 | group = Group.new(attributes) | |
25 | group.name = @generated_group_name.dup if group.name.blank? |
|
25 | group.name = @generated_group_name.dup if group.name.blank? | |
26 | yield group if block_given? |
|
26 | yield group if block_given? | |
27 | group.save! |
|
27 | group.save! | |
28 | group |
|
28 | group | |
29 | end |
|
29 | end | |
30 |
|
30 | |||
31 | def Project.generate!(attributes={}) |
|
31 | def Project.generate!(attributes={}) | |
32 | @generated_project_identifier ||= 'project-0000' |
|
32 | @generated_project_identifier ||= 'project-0000' | |
33 | @generated_project_identifier.succ! |
|
33 | @generated_project_identifier.succ! | |
34 | project = Project.new(attributes) |
|
34 | project = Project.new(attributes) | |
35 | project.name = @generated_project_identifier.dup if project.name.blank? |
|
35 | project.name = @generated_project_identifier.dup if project.name.blank? | |
36 | project.identifier = @generated_project_identifier.dup if project.identifier.blank? |
|
36 | project.identifier = @generated_project_identifier.dup if project.identifier.blank? | |
37 | yield project if block_given? |
|
37 | yield project if block_given? | |
38 | project.save! |
|
38 | project.save! | |
39 | project |
|
39 | project | |
40 | end |
|
40 | end | |
41 |
|
41 | |||
42 | def Project.generate_with_parent!(parent, attributes={}) |
|
42 | def Project.generate_with_parent!(parent, attributes={}) | |
43 | project = Project.generate!(attributes) |
|
43 | project = Project.generate!(attributes) | |
44 | project.set_parent!(parent) |
|
44 | project.set_parent!(parent) | |
45 | project |
|
45 | project | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def Tracker.generate!(attributes={}) |
|
48 | def Tracker.generate!(attributes={}) | |
49 | @generated_tracker_name ||= 'Tracker 0' |
|
49 | @generated_tracker_name ||= 'Tracker 0' | |
50 | @generated_tracker_name.succ! |
|
50 | @generated_tracker_name.succ! | |
51 | tracker = Tracker.new(attributes) |
|
51 | tracker = Tracker.new(attributes) | |
52 | tracker.name = @generated_tracker_name.dup if tracker.name.blank? |
|
52 | tracker.name = @generated_tracker_name.dup if tracker.name.blank? | |
53 | yield tracker if block_given? |
|
53 | yield tracker if block_given? | |
54 | tracker.save! |
|
54 | tracker.save! | |
55 | tracker |
|
55 | tracker | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def Role.generate!(attributes={}) |
|
58 | def Role.generate!(attributes={}) | |
59 | @generated_role_name ||= 'Role 0' |
|
59 | @generated_role_name ||= 'Role 0' | |
60 | @generated_role_name.succ! |
|
60 | @generated_role_name.succ! | |
61 | role = Role.new(attributes) |
|
61 | role = Role.new(attributes) | |
62 | role.name = @generated_role_name.dup if role.name.blank? |
|
62 | role.name = @generated_role_name.dup if role.name.blank? | |
63 | yield role if block_given? |
|
63 | yield role if block_given? | |
64 | role.save! |
|
64 | role.save! | |
65 | role |
|
65 | role | |
66 | end |
|
66 | end | |
67 |
|
67 | |||
68 | def Issue.generate!(attributes={}) |
|
68 | def Issue.generate!(attributes={}) | |
69 | issue = Issue.new(attributes) |
|
69 | issue = Issue.new(attributes) | |
70 | issue.project ||= Project.find(1) |
|
70 | issue.project ||= Project.find(1) | |
71 | issue.tracker ||= issue.project.trackers.first |
|
71 | issue.tracker ||= issue.project.trackers.first | |
72 | issue.subject = 'Generated' if issue.subject.blank? |
|
72 | issue.subject = 'Generated' if issue.subject.blank? | |
73 | issue.author ||= User.find(2) |
|
73 | issue.author ||= User.find(2) | |
74 | yield issue if block_given? |
|
74 | yield issue if block_given? | |
75 | issue.save! |
|
75 | issue.save! | |
76 | issue |
|
76 | issue | |
77 | end |
|
77 | end | |
78 |
|
78 | |||
79 | # Generates an issue with 2 children and a grandchild |
|
79 | # Generates an issue with 2 children and a grandchild | |
80 | def Issue.generate_with_descendants!(attributes={}) |
|
80 | def Issue.generate_with_descendants!(attributes={}) | |
81 | issue = Issue.generate!(attributes) |
|
81 | issue = Issue.generate!(attributes) | |
82 | child = Issue.generate!(:project => issue.project, :subject => 'Child1', :parent_issue_id => issue.id) |
|
82 | child = Issue.generate!(:project => issue.project, :subject => 'Child1', :parent_issue_id => issue.id) | |
83 | Issue.generate!(:project => issue.project, :subject => 'Child2', :parent_issue_id => issue.id) |
|
83 | Issue.generate!(:project => issue.project, :subject => 'Child2', :parent_issue_id => issue.id) | |
84 | Issue.generate!(:project => issue.project, :subject => 'Child11', :parent_issue_id => child.id) |
|
84 | Issue.generate!(:project => issue.project, :subject => 'Child11', :parent_issue_id => child.id) | |
85 | issue.reload |
|
85 | issue.reload | |
86 | end |
|
86 | end | |
87 |
|
87 | |||
88 | def Journal.generate!(attributes={}) |
|
88 | def Journal.generate!(attributes={}) | |
89 | journal = Journal.new(attributes) |
|
89 | journal = Journal.new(attributes) | |
90 | journal.user ||= User.first |
|
90 | journal.user ||= User.first | |
91 | journal.journalized ||= Issue.first |
|
91 | journal.journalized ||= Issue.first | |
92 | yield journal if block_given? |
|
92 | yield journal if block_given? | |
93 | journal.save! |
|
93 | journal.save! | |
94 | journal |
|
94 | journal | |
95 | end |
|
95 | end | |
96 |
|
96 | |||
97 | def Version.generate!(attributes={}) |
|
97 | def Version.generate!(attributes={}) | |
98 | @generated_version_name ||= 'Version 0' |
|
98 | @generated_version_name ||= 'Version 0' | |
99 | @generated_version_name.succ! |
|
99 | @generated_version_name.succ! | |
100 | version = Version.new(attributes) |
|
100 | version = Version.new(attributes) | |
101 | version.name = @generated_version_name.dup if version.name.blank? |
|
101 | version.name = @generated_version_name.dup if version.name.blank? | |
102 | yield version if block_given? |
|
102 | yield version if block_given? | |
103 | version.save! |
|
103 | version.save! | |
104 | version |
|
104 | version | |
105 | end |
|
105 | end | |
106 |
|
106 | |||
107 | def TimeEntry.generate!(attributes={}) |
|
107 | def TimeEntry.generate!(attributes={}) | |
108 | entry = TimeEntry.new(attributes) |
|
108 | entry = TimeEntry.new(attributes) | |
109 | entry.user ||= User.find(2) |
|
109 | entry.user ||= User.find(2) | |
110 | entry.issue ||= Issue.find(1) |
|
110 | entry.issue ||= Issue.find(1) unless entry.project | |
111 | entry.project ||= entry.issue.project |
|
111 | entry.project ||= entry.issue.project | |
112 | entry.activity ||= TimeEntryActivity.first |
|
112 | entry.activity ||= TimeEntryActivity.first | |
113 | entry.spent_on ||= Date.today |
|
113 | entry.spent_on ||= Date.today | |
|
114 | entry.hours ||= 1.0 | |||
114 | entry.save! |
|
115 | entry.save! | |
115 | entry |
|
116 | entry | |
116 | end |
|
117 | end | |
117 |
|
118 | |||
118 | def AuthSource.generate!(attributes={}) |
|
119 | def AuthSource.generate!(attributes={}) | |
119 | @generated_auth_source_name ||= 'Auth 0' |
|
120 | @generated_auth_source_name ||= 'Auth 0' | |
120 | @generated_auth_source_name.succ! |
|
121 | @generated_auth_source_name.succ! | |
121 | source = AuthSource.new(attributes) |
|
122 | source = AuthSource.new(attributes) | |
122 | source.name = @generated_auth_source_name.dup if source.name.blank? |
|
123 | source.name = @generated_auth_source_name.dup if source.name.blank? | |
123 | yield source if block_given? |
|
124 | yield source if block_given? | |
124 | source.save! |
|
125 | source.save! | |
125 | source |
|
126 | source | |
126 | end |
|
127 | end | |
127 |
|
128 | |||
128 | def Board.generate!(attributes={}) |
|
129 | def Board.generate!(attributes={}) | |
129 | @generated_board_name ||= 'Forum 0' |
|
130 | @generated_board_name ||= 'Forum 0' | |
130 | @generated_board_name.succ! |
|
131 | @generated_board_name.succ! | |
131 | board = Board.new(attributes) |
|
132 | board = Board.new(attributes) | |
132 | board.name = @generated_board_name.dup if board.name.blank? |
|
133 | board.name = @generated_board_name.dup if board.name.blank? | |
133 | board.description = @generated_board_name.dup if board.description.blank? |
|
134 | board.description = @generated_board_name.dup if board.description.blank? | |
134 | yield board if block_given? |
|
135 | yield board if block_given? | |
135 | board.save! |
|
136 | board.save! | |
136 | board |
|
137 | board | |
137 | end |
|
138 | end | |
138 |
|
139 | |||
139 | def Attachment.generate!(attributes={}) |
|
140 | def Attachment.generate!(attributes={}) | |
140 | @generated_filename ||= 'testfile0' |
|
141 | @generated_filename ||= 'testfile0' | |
141 | @generated_filename.succ! |
|
142 | @generated_filename.succ! | |
142 | attributes = attributes.dup |
|
143 | attributes = attributes.dup | |
143 | attachment = Attachment.new(attributes) |
|
144 | attachment = Attachment.new(attributes) | |
144 | attachment.container ||= Issue.find(1) |
|
145 | attachment.container ||= Issue.find(1) | |
145 | attachment.author ||= User.find(2) |
|
146 | attachment.author ||= User.find(2) | |
146 | attachment.filename = @generated_filename.dup if attachment.filename.blank? |
|
147 | attachment.filename = @generated_filename.dup if attachment.filename.blank? | |
147 | attachment.save! |
|
148 | attachment.save! | |
148 | attachment |
|
149 | attachment | |
149 | end |
|
150 | end | |
150 | end |
|
151 | end |
@@ -1,88 +1,116 | |||||
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 | require File.expand_path('../../test_helper', __FILE__) |
|
18 | require File.expand_path('../../test_helper', __FILE__) | |
19 |
|
19 | |||
20 | class TimeEntryActivityTest < ActiveSupport::TestCase |
|
20 | class TimeEntryActivityTest < ActiveSupport::TestCase | |
21 | fixtures :enumerations, :time_entries, :custom_fields |
|
21 | fixtures :enumerations, :time_entries, :custom_fields | |
22 |
|
22 | |||
23 | include Redmine::I18n |
|
23 | include Redmine::I18n | |
24 |
|
24 | |||
25 | def test_should_be_an_enumeration |
|
25 | def test_should_be_an_enumeration | |
26 | assert TimeEntryActivity.ancestors.include?(Enumeration) |
|
26 | assert TimeEntryActivity.ancestors.include?(Enumeration) | |
27 | end |
|
27 | end | |
28 |
|
28 | |||
29 | def test_objects_count |
|
29 | def test_objects_count | |
30 | assert_equal 3, TimeEntryActivity.find_by_name("Design").objects_count |
|
30 | assert_equal 3, TimeEntryActivity.find_by_name("Design").objects_count | |
31 | assert_equal 2, TimeEntryActivity.find_by_name("Development").objects_count |
|
31 | assert_equal 2, TimeEntryActivity.find_by_name("Development").objects_count | |
32 | end |
|
32 | end | |
33 |
|
33 | |||
34 | def test_option_name |
|
34 | def test_option_name | |
35 | assert_equal :enumeration_activities, TimeEntryActivity.new.option_name |
|
35 | assert_equal :enumeration_activities, TimeEntryActivity.new.option_name | |
36 | end |
|
36 | end | |
37 |
|
37 | |||
38 | def test_create_with_custom_field |
|
38 | def test_create_with_custom_field | |
39 | field = TimeEntryActivityCustomField.find_by_name('Billable') |
|
39 | field = TimeEntryActivityCustomField.find_by_name('Billable') | |
40 | e = TimeEntryActivity.new(:name => 'Custom Data') |
|
40 | e = TimeEntryActivity.new(:name => 'Custom Data') | |
41 | e.custom_field_values = {field.id => "1"} |
|
41 | e.custom_field_values = {field.id => "1"} | |
42 | assert e.save |
|
42 | assert e.save | |
43 |
|
43 | |||
44 | e.reload |
|
44 | e.reload | |
45 | assert_equal "1", e.custom_value_for(field).value |
|
45 | assert_equal "1", e.custom_value_for(field).value | |
46 | end |
|
46 | end | |
47 |
|
47 | |||
48 | def test_create_without_required_custom_field_should_fail |
|
48 | def test_create_without_required_custom_field_should_fail | |
49 | set_language_if_valid 'en' |
|
49 | set_language_if_valid 'en' | |
50 | field = TimeEntryActivityCustomField.find_by_name('Billable') |
|
50 | field = TimeEntryActivityCustomField.find_by_name('Billable') | |
51 | field.update_attribute(:is_required, true) |
|
51 | field.update_attribute(:is_required, true) | |
52 |
|
52 | |||
53 | e = TimeEntryActivity.new(:name => 'Custom Data') |
|
53 | e = TimeEntryActivity.new(:name => 'Custom Data') | |
54 | assert !e.save |
|
54 | assert !e.save | |
55 | assert_equal ["Billable can't be blank"], e.errors.full_messages |
|
55 | assert_equal ["Billable can't be blank"], e.errors.full_messages | |
56 | end |
|
56 | end | |
57 |
|
57 | |||
58 | def test_create_with_required_custom_field_should_succeed |
|
58 | def test_create_with_required_custom_field_should_succeed | |
59 | field = TimeEntryActivityCustomField.find_by_name('Billable') |
|
59 | field = TimeEntryActivityCustomField.find_by_name('Billable') | |
60 | field.update_attribute(:is_required, true) |
|
60 | field.update_attribute(:is_required, true) | |
61 |
|
61 | |||
62 | e = TimeEntryActivity.new(:name => 'Custom Data') |
|
62 | e = TimeEntryActivity.new(:name => 'Custom Data') | |
63 | e.custom_field_values = {field.id => "1"} |
|
63 | e.custom_field_values = {field.id => "1"} | |
64 | assert e.save |
|
64 | assert e.save | |
65 | end |
|
65 | end | |
66 |
|
66 | |||
67 | def test_update_with_required_custom_field_change |
|
67 | def test_update_with_required_custom_field_change | |
68 | set_language_if_valid 'en' |
|
68 | set_language_if_valid 'en' | |
69 | field = TimeEntryActivityCustomField.find_by_name('Billable') |
|
69 | field = TimeEntryActivityCustomField.find_by_name('Billable') | |
70 | field.update_attribute(:is_required, true) |
|
70 | field.update_attribute(:is_required, true) | |
71 |
|
71 | |||
72 | e = TimeEntryActivity.find(10) |
|
72 | e = TimeEntryActivity.find(10) | |
73 | assert e.available_custom_fields.include?(field) |
|
73 | assert e.available_custom_fields.include?(field) | |
74 | # No change to custom field, record can be saved |
|
74 | # No change to custom field, record can be saved | |
75 | assert e.save |
|
75 | assert e.save | |
76 | # Blanking custom field, save should fail |
|
76 | # Blanking custom field, save should fail | |
77 | e.custom_field_values = {field.id => ""} |
|
77 | e.custom_field_values = {field.id => ""} | |
78 | assert !e.save |
|
78 | assert !e.save | |
79 | assert_equal ["Billable can't be blank"], e.errors.full_messages |
|
79 | assert_equal ["Billable can't be blank"], e.errors.full_messages | |
80 |
|
80 | |||
81 | # Update custom field to valid value, save should succeed |
|
81 | # Update custom field to valid value, save should succeed | |
82 | e.custom_field_values = {field.id => "0"} |
|
82 | e.custom_field_values = {field.id => "0"} | |
83 | assert e.save |
|
83 | assert e.save | |
84 | e.reload |
|
84 | e.reload | |
85 | assert_equal "0", e.custom_value_for(field).value |
|
85 | assert_equal "0", e.custom_value_for(field).value | |
86 | end |
|
86 | end | |
87 | end |
|
|||
88 |
|
87 | |||
|
88 | def test_system_activity_with_child_in_use_should_be_in_use | |||
|
89 | project = Project.generate! | |||
|
90 | system_activity = TimeEntryActivity.create!(:name => 'Activity') | |||
|
91 | project_activity = TimeEntryActivity.create!(:name => 'Activity', :project => project, :parent_id => system_activity.id) | |||
|
92 | ||||
|
93 | TimeEntry.generate!(:project => project, :activity => project_activity) | |||
|
94 | ||||
|
95 | assert project_activity.in_use? | |||
|
96 | assert system_activity.in_use? | |||
|
97 | end | |||
|
98 | ||||
|
99 | def test_destroying_a_system_activity_should_reassign_children_activities | |||
|
100 | project = Project.generate! | |||
|
101 | system_activity = TimeEntryActivity.create!(:name => 'Activity') | |||
|
102 | project_activity = TimeEntryActivity.create!(:name => 'Activity', :project => project, :parent_id => system_activity.id) | |||
|
103 | ||||
|
104 | entries = [ | |||
|
105 | TimeEntry.generate!(:project => project, :activity => system_activity), | |||
|
106 | TimeEntry.generate!(:project => project, :activity => project_activity) | |||
|
107 | ] | |||
|
108 | ||||
|
109 | assert_difference 'TimeEntryActivity.count', -2 do | |||
|
110 | assert_nothing_raised do | |||
|
111 | assert system_activity.destroy(TimeEntryActivity.find_by_name('Development')) | |||
|
112 | end | |||
|
113 | end | |||
|
114 | assert entries.all? {|entry| entry.reload.activity.name == 'Development'} | |||
|
115 | end | |||
|
116 | end |
General Comments 0
You need to be logged in to leave comments.
Login now