forked from scambra/railscollab
-
Notifications
You must be signed in to change notification settings - Fork 0
/
project_task_list.rb
217 lines (171 loc) · 5.98 KB
/
project_task_list.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
#==
# RailsCollab
# Copyright (C) 2007 - 2008 James S Urquhart
# Portions Copyright (C) René Scheibe
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#++
class ProjectTaskList < ActiveRecord::Base
include ActionController::UrlWriter
belongs_to :project_milestone, :foreign_key => 'milestone_id'
belongs_to :project
belongs_to :completed_by, :class_name => 'User', :foreign_key => 'completed_by_id'
belongs_to :created_by, :class_name => 'User', :foreign_key => 'created_by_id'
belongs_to :updated_by, :class_name => 'User', :foreign_key => 'updated_by_id'
has_many :project_tasks, :foreign_key => 'task_list_id', :order => "#{self.connection.quote_column_name 'order'} ASC", :dependent => :destroy
#has_many :tags, :as => 'rel_object', :dependent => :destroy
before_validation_on_create :process_params
after_create :process_create
before_update :process_update_params
after_update :update_tags
before_destroy :process_destroy
def update_tags
return true if @update_tags.nil?
Tag.clear_by_object(self)
Tag.set_to_object(self, @update_tags)
true
end
def process_params
write_attribute("completed_on", nil)
end
def process_create
ApplicationLog.new_log(self, self.created_by, :add, self.is_private)
update_tags
end
def process_update_params
return unless @ensured_complete.nil?
ApplicationLog.new_log(self, self.updated_by, :edit, self.is_private)
end
def process_destroy
Tag.clear_by_object(self)
ApplicationLog.new_log(self, self.updated_by, :delete, self.is_private)
end
def ensure_completed(completed_by)
# If we don't think we are complete either, exit (vice versa)
@ensured_complete = true
self.project_tasks(true)
# Ok now lets check if we are *really* complete
if self.finished_all_tasks?
if self.completed_on.nil?
write_attribute('completed_on', Time.now.utc)
self.completed_by = completed_by
ApplicationLog::new_log(self, completed_by, :close, self.is_private)
end
else
unless self.completed_on.nil?
write_attribute('completed_on', nil)
ApplicationLog::new_log(self, completed_by, :open, self.is_private)
end
end
end
def object_name
self.name
end
def object_url(host = nil)
url_for :only_path => host.nil?, :host => host, :controller => 'task_lists', :action => 'show', :id => self.id, :active_project => self.project_id
end
def tags
return tags_list.join(',')
end
def tags_list
@update_tags.nil? ? Tag.list_by_object(self) : @update_tags
end
def tags_with_spaces
Tag.list_by_object(self).join(' ')
end
def tags=(val)
@update_tags = val.split(',')
end
def is_completed?
self.completed_on != nil
end
def last_edited_by_owner?
self.created_by.member_of_owner? or (!self.updated_by.nil? and self.updated_by.member_of_owner?)
end
def send_comment_notifications(comment)
end
def self.can_be_created_by(user, project)
project.is_active? and user.has_permission(project, :can_manage_tasks)
end
def can_be_managed_by(user)
project.is_active? and user.has_permission(project, :can_manage_tasks)
end
def can_be_edited_by(user)
return false if !project.is_active? or !user.member_of(project) or user.is_anonymous?
return true if user.is_admin
!(self.is_private and !user.member_of_owner?) and user.id == created_by_id
end
alias :can_be_changed_by :can_be_edited_by
def can_be_deleted_by(user)
project.is_active? and user.member_of(project) and user.is_admin
end
def can_be_seen_by(user)
user.member_of(self.project) and !(self.is_private and !user.member_of_owner?)
end
def comment_can_be_added_by(user)
project.is_active? and project.has_member(user) and !user.is_anonymous?
end
def open_tasks
self.project_tasks.select{ |task| task.completed_on.nil? }
end
def completed_tasks
self.project_tasks.reject{ |task| task.completed_on.nil? }
end
def finished_all_tasks?
completed_count = 0
self.project_tasks.each do |task|
completed_count += 1 unless task.completed_on.nil?
end
completed_count > 0 and completed_count == self.project_tasks.length
end
def self.priv_scope(include_private)
if include_private
yield
else
with_scope :find => { :conditions => ['is_private = ?', false] } do
yield
end
end
end
def self.select_list(project)
ProjectTaskList.all(:conditions => ['project_id = ?', project.id], :select => 'id, name').collect do |tasklist|
[tasklist.name, tasklist.id]
end
end
# Serialization
alias_method :ar_to_xml, :to_xml
def to_xml(options = {}, &block)
default_options = {
:methods => [ :tags ],
:only => [
:id,
:milestone_id,
:priority,
:name,
:description,
:is_private
]}
self.ar_to_xml(options.merge(default_options), &block)
end
# Accesibility
attr_accessible :name, :priority, :description, :milestone_id, :is_private, :tags
# Validation
validates_presence_of :name
validates_each :project_milestone, :allow_nil => true do |record, attr, value|
record.errors.add(attr, :not_part_of_project.l) if value.project_id != record.project_id
end
validates_each :is_private, :if => Proc.new { |obj| !obj.last_edited_by_owner? } do |record, attr, value|
record.errors.add(attr, :not_allowed.l) if value == true
end
end