Hello! We are running our annual fundraising. Please consider making a donation if you value this freely available service or want to support people around the world working towards liberatory social change. https://riseup.net/donate.

task.rb 1.14 KB
Newer Older
1
2
class Task < ActiveRecord::Base

3
  belongs_to :page
4
#  has_and_belongs_to_many :users, :foreign_key => 'task_id'
5
6
7
  has_many :participations,
    dependent: :destroy
  has_many :users, through: :participations
8
  acts_as_list scope: :page
9
10
11
  format_attribute :description
  validates_presence_of :name

12
13
14
15
16
17
18
19
20
21
  belongs_to :created_by, class_name: 'User'
  belongs_to :updated_by, class_name: 'User'

  def self.completed
    where "completed_at IS NOT NULL"
  end

  def self.pending
    where completed_at: nil
  end
22
23
24
25
26
27
28
29
30
31
32

  before_create :set_user
  def set_user
    if User.current or self.created_by
      self.created_by ||= User.current
      self.updated_by = self.created_by
    end
    true
  end

  def owner_name
33
    page.try.owner_name
34
35
  end

azul's avatar
azul committed
36
  def state=(state)
azul's avatar
azul committed
37
38
    self.complete if state == 'complete'
    self.pending if state == 'pending'
azul's avatar
azul committed
39
40
  end

azul's avatar
azul committed
41
42
43
44
45
46
  def complete
    self.completed_at = Time.now
  end

  def pending
    self.completed_at = nil
47
48
49
50
51
52
53
54
55
56
57
58
59
  end

  def completed
    completed_at != nil && completed_at < Time.now
  end
  alias :completed? :completed

  def past_due?
    !completed? && due_at && due_at.to_date < Date.today
  end
  alias :overdue? :past_due?

end