@@ -1,164 +1,164 | |||||
1 | # redMine - project management software |
|
1 | # redMine - project management software | |
2 | # Copyright (C) 2006 Jean-Philippe Lang |
|
2 | # Copyright (C) 2006 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 Project < ActiveRecord::Base |
|
18 | class Project < ActiveRecord::Base | |
19 | # Project statuses |
|
19 | # Project statuses | |
20 | STATUS_ACTIVE = 1 |
|
20 | STATUS_ACTIVE = 1 | |
21 | STATUS_ARCHIVED = 9 |
|
21 | STATUS_ARCHIVED = 9 | |
22 |
|
22 | |||
23 | has_many :members, :dependent => :delete_all, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" |
|
23 | has_many :members, :dependent => :delete_all, :include => :user, :conditions => "#{User.table_name}.status=#{User::STATUS_ACTIVE}" | |
24 | has_many :users, :through => :members |
|
24 | has_many :users, :through => :members | |
25 | has_many :custom_values, :dependent => :delete_all, :as => :customized |
|
25 | has_many :custom_values, :dependent => :delete_all, :as => :customized | |
26 | has_many :enabled_modules, :dependent => :delete_all |
|
26 | has_many :enabled_modules, :dependent => :delete_all | |
27 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] |
|
27 | has_many :issues, :dependent => :destroy, :order => "#{Issue.table_name}.created_on DESC", :include => [:status, :tracker] | |
28 | has_many :issue_changes, :through => :issues, :source => :journals |
|
28 | has_many :issue_changes, :through => :issues, :source => :journals | |
29 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" |
|
29 | has_many :versions, :dependent => :destroy, :order => "#{Version.table_name}.effective_date DESC, #{Version.table_name}.name DESC" | |
30 | has_many :time_entries, :dependent => :delete_all |
|
30 | has_many :time_entries, :dependent => :delete_all | |
31 | has_many :queries, :dependent => :delete_all |
|
31 | has_many :queries, :dependent => :delete_all | |
32 | has_many :documents, :dependent => :destroy |
|
32 | has_many :documents, :dependent => :destroy | |
33 | has_many :news, :dependent => :delete_all, :include => :author |
|
33 | has_many :news, :dependent => :delete_all, :include => :author | |
34 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" |
|
34 | has_many :issue_categories, :dependent => :delete_all, :order => "#{IssueCategory.table_name}.name" | |
35 | has_many :boards, :order => "position ASC" |
|
35 | has_many :boards, :order => "position ASC" | |
36 | has_one :repository, :dependent => :destroy |
|
36 | has_one :repository, :dependent => :destroy | |
37 | has_many :changesets, :through => :repository |
|
37 | has_many :changesets, :through => :repository | |
38 | has_one :wiki, :dependent => :destroy |
|
38 | has_one :wiki, :dependent => :destroy | |
39 | 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' |
|
39 | 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' | |
40 | acts_as_tree :order => "name", :counter_cache => true |
|
40 | acts_as_tree :order => "name", :counter_cache => true | |
41 |
|
41 | |||
42 | attr_protected :status, :enabled_module_names |
|
42 | attr_protected :status, :enabled_module_names | |
43 |
|
43 | |||
44 | validates_presence_of :name, :description, :identifier |
|
44 | validates_presence_of :name, :description, :identifier | |
45 | validates_uniqueness_of :name, :identifier |
|
45 | validates_uniqueness_of :name, :identifier | |
46 | validates_associated :custom_values, :on => :update |
|
46 | validates_associated :custom_values, :on => :update | |
47 | validates_associated :repository, :wiki |
|
47 | validates_associated :repository, :wiki | |
48 | validates_length_of :name, :maximum => 30 |
|
48 | validates_length_of :name, :maximum => 30 | |
49 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i |
|
49 | validates_format_of :name, :with => /^[\w\s\'\-]*$/i | |
50 | validates_length_of :description, :maximum => 255 |
|
50 | validates_length_of :description, :maximum => 255 | |
51 |
validates_length_of :homepage, :maximum => |
|
51 | validates_length_of :homepage, :maximum => 60 | |
52 | validates_length_of :identifier, :in => 3..12 |
|
52 | validates_length_of :identifier, :in => 3..12 | |
53 | validates_format_of :identifier, :with => /^[a-z0-9\-]*$/ |
|
53 | validates_format_of :identifier, :with => /^[a-z0-9\-]*$/ | |
54 |
|
54 | |||
55 | def identifier=(identifier) |
|
55 | def identifier=(identifier) | |
56 | super unless identifier_frozen? |
|
56 | super unless identifier_frozen? | |
57 | end |
|
57 | end | |
58 |
|
58 | |||
59 | def identifier_frozen? |
|
59 | def identifier_frozen? | |
60 | errors[:identifier].nil? && !(new_record? || identifier.blank?) |
|
60 | errors[:identifier].nil? && !(new_record? || identifier.blank?) | |
61 | end |
|
61 | end | |
62 |
|
62 | |||
63 | def issues_with_subprojects(include_subprojects=false) |
|
63 | def issues_with_subprojects(include_subprojects=false) | |
64 | conditions = nil |
|
64 | conditions = nil | |
65 | if include_subprojects && !active_children.empty? |
|
65 | if include_subprojects && !active_children.empty? | |
66 | ids = [id] + active_children.collect {|c| c.id} |
|
66 | ids = [id] + active_children.collect {|c| c.id} | |
67 | conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"] |
|
67 | conditions = ["#{Issue.table_name}.project_id IN (#{ids.join(',')})"] | |
68 | end |
|
68 | end | |
69 | conditions ||= ["#{Issue.table_name}.project_id = ?", id] |
|
69 | conditions ||= ["#{Issue.table_name}.project_id = ?", id] | |
70 | Issue.with_scope :find => { :conditions => conditions } do |
|
70 | Issue.with_scope :find => { :conditions => conditions } do | |
71 | yield |
|
71 | yield | |
72 | end |
|
72 | end | |
73 | end |
|
73 | end | |
74 |
|
74 | |||
75 | # returns latest created projects |
|
75 | # returns latest created projects | |
76 | # non public projects will be returned only if user is a member of those |
|
76 | # non public projects will be returned only if user is a member of those | |
77 | def self.latest(user=nil, count=5) |
|
77 | def self.latest(user=nil, count=5) | |
78 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") |
|
78 | find(:all, :limit => count, :conditions => visible_by(user), :order => "created_on DESC") | |
79 | end |
|
79 | end | |
80 |
|
80 | |||
81 | def self.visible_by(user=nil) |
|
81 | def self.visible_by(user=nil) | |
82 | if user && user.admin? |
|
82 | if user && user.admin? | |
83 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" |
|
83 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE}" | |
84 | elsif user && user.memberships.any? |
|
84 | elsif user && user.memberships.any? | |
85 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))" |
|
85 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND (#{Project.table_name}.is_public = #{connection.quoted_true} or #{Project.table_name}.id IN (#{user.memberships.collect{|m| m.project_id}.join(',')}))" | |
86 | else |
|
86 | else | |
87 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" |
|
87 | return "#{Project.table_name}.status=#{Project::STATUS_ACTIVE} AND #{Project.table_name}.is_public = #{connection.quoted_true}" | |
88 | end |
|
88 | end | |
89 | end |
|
89 | end | |
90 |
|
90 | |||
91 | def active? |
|
91 | def active? | |
92 | self.status == STATUS_ACTIVE |
|
92 | self.status == STATUS_ACTIVE | |
93 | end |
|
93 | end | |
94 |
|
94 | |||
95 | def archive |
|
95 | def archive | |
96 | # Archive subprojects if any |
|
96 | # Archive subprojects if any | |
97 | children.each do |subproject| |
|
97 | children.each do |subproject| | |
98 | subproject.archive |
|
98 | subproject.archive | |
99 | end |
|
99 | end | |
100 | update_attribute :status, STATUS_ARCHIVED |
|
100 | update_attribute :status, STATUS_ARCHIVED | |
101 | end |
|
101 | end | |
102 |
|
102 | |||
103 | def unarchive |
|
103 | def unarchive | |
104 | return false if parent && !parent.active? |
|
104 | return false if parent && !parent.active? | |
105 | update_attribute :status, STATUS_ACTIVE |
|
105 | update_attribute :status, STATUS_ACTIVE | |
106 | end |
|
106 | end | |
107 |
|
107 | |||
108 | def active_children |
|
108 | def active_children | |
109 | children.select {|child| child.active?} |
|
109 | children.select {|child| child.active?} | |
110 | end |
|
110 | end | |
111 |
|
111 | |||
112 | # Returns an array of all custom fields enabled for project issues |
|
112 | # Returns an array of all custom fields enabled for project issues | |
113 | # (explictly associated custom fields and custom fields enabled for all projects) |
|
113 | # (explictly associated custom fields and custom fields enabled for all projects) | |
114 | def custom_fields_for_issues(tracker) |
|
114 | def custom_fields_for_issues(tracker) | |
115 | all_custom_fields.select {|c| tracker.custom_fields.include? c } |
|
115 | all_custom_fields.select {|c| tracker.custom_fields.include? c } | |
116 | end |
|
116 | end | |
117 |
|
117 | |||
118 | def all_custom_fields |
|
118 | def all_custom_fields | |
119 | @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq |
|
119 | @all_custom_fields ||= (IssueCustomField.for_all + custom_fields).uniq | |
120 | end |
|
120 | end | |
121 |
|
121 | |||
122 | def <=>(project) |
|
122 | def <=>(project) | |
123 | name <=> project.name |
|
123 | name <=> project.name | |
124 | end |
|
124 | end | |
125 |
|
125 | |||
126 | def allows_to?(action) |
|
126 | def allows_to?(action) | |
127 | if action.is_a? Hash |
|
127 | if action.is_a? Hash | |
128 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" |
|
128 | allowed_actions.include? "#{action[:controller]}/#{action[:action]}" | |
129 | else |
|
129 | else | |
130 | allowed_permissions.include? action |
|
130 | allowed_permissions.include? action | |
131 | end |
|
131 | end | |
132 | end |
|
132 | end | |
133 |
|
133 | |||
134 | def module_enabled?(module_name) |
|
134 | def module_enabled?(module_name) | |
135 | module_name = module_name.to_s |
|
135 | module_name = module_name.to_s | |
136 | enabled_modules.detect {|m| m.name == module_name} |
|
136 | enabled_modules.detect {|m| m.name == module_name} | |
137 | end |
|
137 | end | |
138 |
|
138 | |||
139 | def enabled_module_names=(module_names) |
|
139 | def enabled_module_names=(module_names) | |
140 | enabled_modules.clear |
|
140 | enabled_modules.clear | |
141 | module_names = [] unless module_names && module_names.is_a?(Array) |
|
141 | module_names = [] unless module_names && module_names.is_a?(Array) | |
142 | module_names.each do |name| |
|
142 | module_names.each do |name| | |
143 | enabled_modules << EnabledModule.new(:name => name.to_s) |
|
143 | enabled_modules << EnabledModule.new(:name => name.to_s) | |
144 | end |
|
144 | end | |
145 | end |
|
145 | end | |
146 |
|
146 | |||
147 | protected |
|
147 | protected | |
148 | def validate |
|
148 | def validate | |
149 | errors.add(parent_id, " must be a root project") if parent and parent.parent |
|
149 | errors.add(parent_id, " must be a root project") if parent and parent.parent | |
150 | errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0 |
|
150 | errors.add_to_base("A project with subprojects can't be a subproject") if parent and children.size > 0 | |
151 | end |
|
151 | end | |
152 |
|
152 | |||
153 | private |
|
153 | private | |
154 | def allowed_permissions |
|
154 | def allowed_permissions | |
155 | @allowed_permissions ||= begin |
|
155 | @allowed_permissions ||= begin | |
156 | module_names = enabled_modules.collect {|m| m.name} |
|
156 | module_names = enabled_modules.collect {|m| m.name} | |
157 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} |
|
157 | Redmine::AccessControl.modules_permissions(module_names).collect {|p| p.name} | |
158 | end |
|
158 | end | |
159 | end |
|
159 | end | |
160 |
|
160 | |||
161 | def allowed_actions |
|
161 | def allowed_actions | |
162 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten |
|
162 | @actions_allowed ||= allowed_permissions.inject([]) { |actions, permission| actions += Redmine::AccessControl.allowed_actions(permission) }.flatten | |
163 | end |
|
163 | end | |
164 | end |
|
164 | end |
General Comments 0
You need to be logged in to leave comments.
Login now