Class Task
In: app/models/task.rb
Parent: ActiveRecord::Base

Task is the base class of … tasks! Its instances represents tasks that must be confirmed by someone (like an environment administrator) or by noosfero itself.

The specific types of tasks must override the perform method, so the actual action associated to the type of task can be performed. See the documentation of the perform method for details.

This class has a data field of type text, where you can store any type of data (as serialized Ruby objects) you need for your subclass (which will need to declare <ttserialize</tt> itself).

Methods

Classes and Modules

Module Task::Status

Attributes

code_length  [RW] 

Public Class methods

[Source]

    # File app/models/task.rb, line 38
38:   def initialize(*args)
39:     super
40:     self.status ||= Task::Status::ACTIVE
41:   end

Protected Class methods

finds a task by its (generated) code. Only returns a task with the specified code AND with status = Task::Status::ACTIVE.

Can be used in subclasses to find only their instances.

[Source]

     # File app/models/task.rb, line 186
186:     def find_by_code(code)
187:       self.find(:first, :conditions => { :code => code, :status => Task::Status::ACTIVE })
188:     end

[Source]

     # File app/models/task.rb, line 167
167:     def finished
168:       self.find(:all, :conditions => { :status =>  [Task::Status::CANCELLED, Task::Status::FINISHED]})
169:     end

generates a random code string consisting of length characters (or 36 by default) in the ranges a-z and 0-9

[Source]

     # File app/models/task.rb, line 173
173:     def generate_code(length = nil)
174:       chars = ('a'..'z').to_a + ('0'..'9').to_a
175:       code = ""
176:       (length || chars.size).times do |n|
177:         code << chars[rand(chars.size)]
178:       end
179:       code
180:     end

[Source]

     # File app/models/task.rb, line 163
163:     def pending
164:       self.find(:all, :conditions => { :status =>  Task::Status::ACTIVE })
165:     end

Public Instance methods

this method cancels the task. At the end a message (as returned by cancel_message) is sent to the requestor with notify_requestor.

[Source]

    # File app/models/task.rb, line 77
77:   def cancel
78:     transaction do
79:       self.status = Task::Status::CANCELLED
80:       self.end_date = Time.now
81:       self.save!
82:       send_notification(:cancelled)
83:     end
84:   end

Returns the description of the task.

This method must be overriden in subclasses to return something meaningful for each kind of task

[Source]

    # File app/models/task.rb, line 91
91:   def description
92:     _('Generic task')
93:   end

this method finished the task. It calls perform, which must be overriden by subclasses. At the end a message (as returned by finish_message) is sent to the requestor with notify_requestor.

[Source]

    # File app/models/task.rb, line 65
65:   def finish
66:     transaction do
67:       self.status = Task::Status::FINISHED
68:       self.end_date = Time.now
69:       self.save!
70:       self.perform
71:       send_notification(:finished)
72:     end
73:   end

What permission is required to perform task?

[Source]

     # File app/models/task.rb, line 126
126:   def permission
127:     :perform_task
128:   end

The message that will be sent to the target of the task when it is created. The indent of this message is to notify the target about the request that was just created for him/her.

The implementation in this class returns nil, what makes the notification not to be sent. If you want to send a notification to the target upon task creation, override this method and return a String.

[Source]

     # File app/models/task.rb, line 121
121:   def target_notification_message
122:     nil
123:   end

The message that will be sent to the requestor of the task when its cancelled.

[Source]

     # File app/models/task.rb, line 110
110:   def task_cancelled_message
111:     _("The task was cancelled at %s") % (self.end_date.to_s)
112:   end

The message that will be sent to the requestor of the task when the task is created.

[Source]

     # File app/models/task.rb, line 97
 97:   def task_created_message
 98:     # FIXME: use a date properly recorded.
 99:     _("The task was created at %s") % Time.now
100:   end

The message that will be sent to the requestor of the task when its finished.

[Source]

     # File app/models/task.rb, line 104
104:   def task_finished_message
105:     _("The task was finished at %s") % (self.end_date.to_s)
106:   end

Protected Instance methods

This method must be overrided in subclasses, and its implementation must do the job the task is intended to. This method will be called when the finish method is called.

To cancel the finish of the task, you can throw an exception in perform.

The implementation on Task class just does nothing.

[Source]

     # File app/models/task.rb, line 139
139:   def perform
140:   end

sends notification e-mail about a task, if the task has a requestor.

If

[Source]

     # File app/models/task.rb, line 153
153:   def send_notification(action)
154:     if sends_email?
155:       if self.requestor
156:         TaskMailer.send("deliver_task_#{action}", self)
157:       end
158:     end
159:   end

Tells wheter e-mail notifications must be sent or not. Returns true by default (i.e. notification are sent), but can be overriden in subclasses to disable notifications or even to send notifications based on some conditions.

[Source]

     # File app/models/task.rb, line 146
146:   def sends_email?
147:     true
148:   end

[Validate]