##// END OF EJS Templates
Merged r11762 and r11763 from trunk (#13783)....
Jean-Philippe Lang -
r11605:2a6dadf78799
parent child
Show More
@@ -1,98 +1,98
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class EnumerationsController < ApplicationController
19 19 layout 'admin'
20 20
21 21 before_filter :require_admin, :except => :index
22 22 before_filter :require_admin_or_api_request, :only => :index
23 23 before_filter :build_new_enumeration, :only => [:new, :create]
24 24 before_filter :find_enumeration, :only => [:edit, :update, :destroy]
25 25 accept_api_auth :index
26 26
27 27 helper :custom_fields
28 28
29 29 def index
30 30 respond_to do |format|
31 31 format.html
32 32 format.api {
33 33 @klass = Enumeration.get_subclass(params[:type])
34 34 if @klass
35 35 @enumerations = @klass.shared.sorted.all
36 36 else
37 37 render_404
38 38 end
39 39 }
40 40 end
41 41 end
42 42
43 43 def new
44 44 end
45 45
46 46 def create
47 47 if request.post? && @enumeration.save
48 48 flash[:notice] = l(:notice_successful_create)
49 49 redirect_to enumerations_path
50 50 else
51 51 render :action => 'new'
52 52 end
53 53 end
54 54
55 55 def edit
56 56 end
57 57
58 58 def update
59 59 if request.put? && @enumeration.update_attributes(params[:enumeration])
60 60 flash[:notice] = l(:notice_successful_update)
61 61 redirect_to enumerations_path
62 62 else
63 63 render :action => 'edit'
64 64 end
65 65 end
66 66
67 67 def destroy
68 68 if !@enumeration.in_use?
69 69 # No associated objects
70 70 @enumeration.destroy
71 71 redirect_to enumerations_path
72 72 return
73 73 elsif params[:reassign_to_id]
74 74 if reassign_to = @enumeration.class.find_by_id(params[:reassign_to_id])
75 75 @enumeration.destroy(reassign_to)
76 76 redirect_to enumerations_path
77 77 return
78 78 end
79 79 end
80 @enumerations = @enumeration.class.all - [@enumeration]
80 @enumerations = @enumeration.class.system.all - [@enumeration]
81 81 end
82 82
83 83 private
84 84
85 85 def build_new_enumeration
86 86 class_name = params[:enumeration] && params[:enumeration][:type] || params[:type]
87 87 @enumeration = Enumeration.new_subclass_instance(class_name, params[:enumeration])
88 88 if @enumeration.nil?
89 89 render_404
90 90 end
91 91 end
92 92
93 93 def find_enumeration
94 94 @enumeration = Enumeration.find(params[:id])
95 95 rescue ActiveRecord::RecordNotFound
96 96 render_404
97 97 end
98 98 end
@@ -1,141 +1,142
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class Enumeration < ActiveRecord::Base
19 19 include Redmine::SubclassFactory
20 20
21 21 default_scope :order => "#{Enumeration.table_name}.position ASC"
22 22
23 23 belongs_to :project
24 24
25 25 acts_as_list :scope => 'type = \'#{type}\''
26 26 acts_as_customizable
27 27 acts_as_tree :order => "#{Enumeration.table_name}.position ASC"
28 28
29 29 before_destroy :check_integrity
30 30 before_save :check_default
31 31
32 32 attr_protected :type
33 33
34 34 validates_presence_of :name
35 35 validates_uniqueness_of :name, :scope => [:type, :project_id]
36 36 validates_length_of :name, :maximum => 30
37 37
38 38 scope :shared, lambda { where(:project_id => nil) }
39 39 scope :sorted, lambda { order("#{table_name}.position ASC") }
40 40 scope :active, lambda { where(:active => true) }
41 scope :system, lambda { where(:project_id => nil) }
41 42 scope :named, lambda {|arg| where("LOWER(#{table_name}.name) = LOWER(?)", arg.to_s.strip)}
42 43
43 44 def self.default
44 45 # Creates a fake default scope so Enumeration.default will check
45 46 # it's type. STI subclasses will automatically add their own
46 47 # types to the finder.
47 48 if self.descends_from_active_record?
48 49 where(:is_default => true, :type => 'Enumeration').first
49 50 else
50 51 # STI classes are
51 52 where(:is_default => true).first
52 53 end
53 54 end
54 55
55 56 # Overloaded on concrete classes
56 57 def option_name
57 58 nil
58 59 end
59 60
60 61 def check_default
61 62 if is_default? && is_default_changed?
62 63 Enumeration.update_all({:is_default => false}, {:type => type})
63 64 end
64 65 end
65 66
66 67 # Overloaded on concrete classes
67 68 def objects_count
68 69 0
69 70 end
70 71
71 72 def in_use?
72 73 self.objects_count != 0
73 74 end
74 75
75 76 # Is this enumeration overiding a system level enumeration?
76 77 def is_override?
77 78 !self.parent.nil?
78 79 end
79 80
80 81 alias :destroy_without_reassign :destroy
81 82
82 83 # Destroy the enumeration
83 84 # If a enumeration is specified, objects are reassigned
84 85 def destroy(reassign_to = nil)
85 86 if reassign_to && reassign_to.is_a?(Enumeration)
86 87 self.transfer_relations(reassign_to)
87 88 end
88 89 destroy_without_reassign
89 90 end
90 91
91 92 def <=>(enumeration)
92 93 position <=> enumeration.position
93 94 end
94 95
95 96 def to_s; name end
96 97
97 98 # Returns the Subclasses of Enumeration. Each Subclass needs to be
98 99 # required in development mode.
99 100 #
100 101 # Note: subclasses is protected in ActiveRecord
101 102 def self.get_subclasses
102 103 subclasses
103 104 end
104 105
105 106 # Does the +new+ Hash override the previous Enumeration?
106 107 def self.overridding_change?(new, previous)
107 108 if (same_active_state?(new['active'], previous.active)) && same_custom_values?(new,previous)
108 109 return false
109 110 else
110 111 return true
111 112 end
112 113 end
113 114
114 115 # Does the +new+ Hash have the same custom values as the previous Enumeration?
115 116 def self.same_custom_values?(new, previous)
116 117 previous.custom_field_values.each do |custom_value|
117 118 if custom_value.value != new["custom_field_values"][custom_value.custom_field_id.to_s]
118 119 return false
119 120 end
120 121 end
121 122
122 123 return true
123 124 end
124 125
125 126 # Are the new and previous fields equal?
126 127 def self.same_active_state?(new, previous)
127 128 new = (new == "1" ? true : false)
128 129 return new == previous
129 130 end
130 131
131 132 private
132 133 def check_integrity
133 134 raise "Can't delete enumeration" if self.in_use?
134 135 end
135 136
136 137 end
137 138
138 139 # Force load the subclasses in development mode
139 140 require_dependency 'time_entry_activity'
140 141 require_dependency 'document_category'
141 142 require_dependency 'issue_priority'
@@ -1,34 +1,38
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 class TimeEntryActivity < Enumeration
19 19 has_many :time_entries, :foreign_key => 'activity_id'
20 20
21 21 OptionName = :enumeration_activities
22 22
23 23 def option_name
24 24 OptionName
25 25 end
26 26
27 def objects
28 TimeEntry.where(:activity_id => self_and_descendants(1).map(&:id))
29 end
30
27 31 def objects_count
28 time_entries.count
32 objects.count
29 33 end
30 34
31 35 def transfer_relations(to)
32 time_entries.update_all("activity_id = #{to.id}")
36 objects.update_all(:activity_id => to.id)
33 37 end
34 38 end
@@ -1,150 +1,151
1 1 module ObjectHelpers
2 2 def User.generate!(attributes={})
3 3 @generated_user_login ||= 'user0'
4 4 @generated_user_login.succ!
5 5 user = User.new(attributes)
6 6 user.login = @generated_user_login.dup if user.login.blank?
7 7 user.mail = "#{@generated_user_login}@example.com" if user.mail.blank?
8 8 user.firstname = "Bob" if user.firstname.blank?
9 9 user.lastname = "Doe" if user.lastname.blank?
10 10 yield user if block_given?
11 11 user.save!
12 12 user
13 13 end
14 14
15 15 def User.add_to_project(user, project, roles=nil)
16 16 roles = Role.find(1) if roles.nil?
17 17 roles = [roles] unless roles.is_a?(Array)
18 18 Member.create!(:principal => user, :project => project, :roles => roles)
19 19 end
20 20
21 21 def Group.generate!(attributes={})
22 22 @generated_group_name ||= 'Group 0'
23 23 @generated_group_name.succ!
24 24 group = Group.new(attributes)
25 25 group.name = @generated_group_name.dup if group.name.blank?
26 26 yield group if block_given?
27 27 group.save!
28 28 group
29 29 end
30 30
31 31 def Project.generate!(attributes={})
32 32 @generated_project_identifier ||= 'project-0000'
33 33 @generated_project_identifier.succ!
34 34 project = Project.new(attributes)
35 35 project.name = @generated_project_identifier.dup if project.name.blank?
36 36 project.identifier = @generated_project_identifier.dup if project.identifier.blank?
37 37 yield project if block_given?
38 38 project.save!
39 39 project
40 40 end
41 41
42 42 def Project.generate_with_parent!(parent, attributes={})
43 43 project = Project.generate!(attributes)
44 44 project.set_parent!(parent)
45 45 project
46 46 end
47 47
48 48 def Tracker.generate!(attributes={})
49 49 @generated_tracker_name ||= 'Tracker 0'
50 50 @generated_tracker_name.succ!
51 51 tracker = Tracker.new(attributes)
52 52 tracker.name = @generated_tracker_name.dup if tracker.name.blank?
53 53 yield tracker if block_given?
54 54 tracker.save!
55 55 tracker
56 56 end
57 57
58 58 def Role.generate!(attributes={})
59 59 @generated_role_name ||= 'Role 0'
60 60 @generated_role_name.succ!
61 61 role = Role.new(attributes)
62 62 role.name = @generated_role_name.dup if role.name.blank?
63 63 yield role if block_given?
64 64 role.save!
65 65 role
66 66 end
67 67
68 68 def Issue.generate!(attributes={})
69 69 issue = Issue.new(attributes)
70 70 issue.project ||= Project.find(1)
71 71 issue.tracker ||= issue.project.trackers.first
72 72 issue.subject = 'Generated' if issue.subject.blank?
73 73 issue.author ||= User.find(2)
74 74 yield issue if block_given?
75 75 issue.save!
76 76 issue
77 77 end
78 78
79 79 # Generates an issue with 2 children and a grandchild
80 80 def Issue.generate_with_descendants!(attributes={})
81 81 issue = Issue.generate!(attributes)
82 82 child = Issue.generate!(:project => issue.project, :subject => 'Child1', :parent_issue_id => issue.id)
83 83 Issue.generate!(:project => issue.project, :subject => 'Child2', :parent_issue_id => issue.id)
84 84 Issue.generate!(:project => issue.project, :subject => 'Child11', :parent_issue_id => child.id)
85 85 issue.reload
86 86 end
87 87
88 88 def Journal.generate!(attributes={})
89 89 journal = Journal.new(attributes)
90 90 journal.user ||= User.first
91 91 journal.journalized ||= Issue.first
92 92 yield journal if block_given?
93 93 journal.save!
94 94 journal
95 95 end
96 96
97 97 def Version.generate!(attributes={})
98 98 @generated_version_name ||= 'Version 0'
99 99 @generated_version_name.succ!
100 100 version = Version.new(attributes)
101 101 version.name = @generated_version_name.dup if version.name.blank?
102 102 yield version if block_given?
103 103 version.save!
104 104 version
105 105 end
106 106
107 107 def TimeEntry.generate!(attributes={})
108 108 entry = TimeEntry.new(attributes)
109 109 entry.user ||= User.find(2)
110 entry.issue ||= Issue.find(1)
110 entry.issue ||= Issue.find(1) unless entry.project
111 111 entry.project ||= entry.issue.project
112 112 entry.activity ||= TimeEntryActivity.first
113 113 entry.spent_on ||= Date.today
114 entry.hours ||= 1.0
114 115 entry.save!
115 116 entry
116 117 end
117 118
118 119 def AuthSource.generate!(attributes={})
119 120 @generated_auth_source_name ||= 'Auth 0'
120 121 @generated_auth_source_name.succ!
121 122 source = AuthSource.new(attributes)
122 123 source.name = @generated_auth_source_name.dup if source.name.blank?
123 124 yield source if block_given?
124 125 source.save!
125 126 source
126 127 end
127 128
128 129 def Board.generate!(attributes={})
129 130 @generated_board_name ||= 'Forum 0'
130 131 @generated_board_name.succ!
131 132 board = Board.new(attributes)
132 133 board.name = @generated_board_name.dup if board.name.blank?
133 134 board.description = @generated_board_name.dup if board.description.blank?
134 135 yield board if block_given?
135 136 board.save!
136 137 board
137 138 end
138 139
139 140 def Attachment.generate!(attributes={})
140 141 @generated_filename ||= 'testfile0'
141 142 @generated_filename.succ!
142 143 attributes = attributes.dup
143 144 attachment = Attachment.new(attributes)
144 145 attachment.container ||= Issue.find(1)
145 146 attachment.author ||= User.find(2)
146 147 attachment.filename = @generated_filename.dup if attachment.filename.blank?
147 148 attachment.save!
148 149 attachment
149 150 end
150 151 end
@@ -1,88 +1,116
1 1 # Redmine - project management software
2 2 # Copyright (C) 2006-2013 Jean-Philippe Lang
3 3 #
4 4 # This program is free software; you can redistribute it and/or
5 5 # modify it under the terms of the GNU General Public License
6 6 # as published by the Free Software Foundation; either version 2
7 7 # of the License, or (at your option) any later version.
8 8 #
9 9 # This program is distributed in the hope that it will be useful,
10 10 # but WITHOUT ANY WARRANTY; without even the implied warranty of
11 11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 12 # GNU General Public License for more details.
13 13 #
14 14 # You should have received a copy of the GNU General Public License
15 15 # along with this program; if not, write to the Free Software
16 16 # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
17 17
18 18 require File.expand_path('../../test_helper', __FILE__)
19 19
20 20 class TimeEntryActivityTest < ActiveSupport::TestCase
21 21 fixtures :enumerations, :time_entries, :custom_fields
22 22
23 23 include Redmine::I18n
24 24
25 25 def test_should_be_an_enumeration
26 26 assert TimeEntryActivity.ancestors.include?(Enumeration)
27 27 end
28 28
29 29 def test_objects_count
30 30 assert_equal 3, TimeEntryActivity.find_by_name("Design").objects_count
31 31 assert_equal 2, TimeEntryActivity.find_by_name("Development").objects_count
32 32 end
33 33
34 34 def test_option_name
35 35 assert_equal :enumeration_activities, TimeEntryActivity.new.option_name
36 36 end
37 37
38 38 def test_create_with_custom_field
39 39 field = TimeEntryActivityCustomField.find_by_name('Billable')
40 40 e = TimeEntryActivity.new(:name => 'Custom Data')
41 41 e.custom_field_values = {field.id => "1"}
42 42 assert e.save
43 43
44 44 e.reload
45 45 assert_equal "1", e.custom_value_for(field).value
46 46 end
47 47
48 48 def test_create_without_required_custom_field_should_fail
49 49 set_language_if_valid 'en'
50 50 field = TimeEntryActivityCustomField.find_by_name('Billable')
51 51 field.update_attribute(:is_required, true)
52 52
53 53 e = TimeEntryActivity.new(:name => 'Custom Data')
54 54 assert !e.save
55 55 assert_equal ["Billable can't be blank"], e.errors.full_messages
56 56 end
57 57
58 58 def test_create_with_required_custom_field_should_succeed
59 59 field = TimeEntryActivityCustomField.find_by_name('Billable')
60 60 field.update_attribute(:is_required, true)
61 61
62 62 e = TimeEntryActivity.new(:name => 'Custom Data')
63 63 e.custom_field_values = {field.id => "1"}
64 64 assert e.save
65 65 end
66 66
67 67 def test_update_with_required_custom_field_change
68 68 set_language_if_valid 'en'
69 69 field = TimeEntryActivityCustomField.find_by_name('Billable')
70 70 field.update_attribute(:is_required, true)
71 71
72 72 e = TimeEntryActivity.find(10)
73 73 assert e.available_custom_fields.include?(field)
74 74 # No change to custom field, record can be saved
75 75 assert e.save
76 76 # Blanking custom field, save should fail
77 77 e.custom_field_values = {field.id => ""}
78 78 assert !e.save
79 79 assert_equal ["Billable can't be blank"], e.errors.full_messages
80 80
81 81 # Update custom field to valid value, save should succeed
82 82 e.custom_field_values = {field.id => "0"}
83 83 assert e.save
84 84 e.reload
85 85 assert_equal "0", e.custom_value_for(field).value
86 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