2016-02-12 08:17:42 -05:00
|
|
|
# == Schema Information
|
|
|
|
#
|
|
|
|
# Table name: tasks
|
|
|
|
#
|
|
|
|
# id :integer not null, primary key
|
|
|
|
# user_id :integer not null
|
|
|
|
# project_id :integer not null
|
|
|
|
# target_id :integer not null
|
|
|
|
# target_type :string not null
|
|
|
|
# author_id :integer
|
2016-02-17 14:45:32 -05:00
|
|
|
# note_id :integer
|
2016-02-12 08:17:42 -05:00
|
|
|
# action :integer
|
|
|
|
# state :string not null
|
|
|
|
# created_at :datetime
|
|
|
|
# updated_at :datetime
|
|
|
|
#
|
|
|
|
|
|
|
|
class Task < ActiveRecord::Base
|
2016-02-12 13:45:44 -05:00
|
|
|
ASSIGNED = 1
|
2016-02-17 12:46:26 -05:00
|
|
|
MENTIONED = 2
|
2016-02-12 13:45:44 -05:00
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
belongs_to :author, class_name: "User"
|
2016-02-17 14:45:32 -05:00
|
|
|
belongs_to :note
|
2016-02-12 08:17:42 -05:00
|
|
|
belongs_to :project
|
|
|
|
belongs_to :target, polymorphic: true, touch: true
|
|
|
|
belongs_to :user
|
|
|
|
|
2016-02-12 13:45:44 -05:00
|
|
|
delegate :name, :email, to: :author, prefix: true, allow_nil: true
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
validates :action, :project, :target, :user, presence: true
|
|
|
|
|
2016-02-12 13:45:44 -05:00
|
|
|
default_scope { reorder(id: :desc) }
|
|
|
|
|
|
|
|
scope :pending, -> { with_state(:pending) }
|
|
|
|
scope :done, -> { with_state(:done) }
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
state_machine :state, initial: :pending do
|
2016-02-15 14:49:28 -05:00
|
|
|
event :done do
|
|
|
|
transition pending: :done
|
|
|
|
end
|
|
|
|
|
2016-02-12 08:17:42 -05:00
|
|
|
state :pending
|
|
|
|
state :done
|
|
|
|
end
|
2016-02-12 13:45:44 -05:00
|
|
|
|
|
|
|
def action_name
|
|
|
|
case action
|
|
|
|
when ASSIGNED then 'assigned'
|
2016-02-17 12:46:26 -05:00
|
|
|
when MENTIONED then 'mentioned on'
|
2016-02-12 13:45:44 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def body?
|
|
|
|
target.respond_to? :title
|
|
|
|
end
|
|
|
|
|
2016-02-17 14:45:32 -05:00
|
|
|
def note_text
|
|
|
|
note.try(:note)
|
|
|
|
end
|
|
|
|
|
2016-02-12 13:45:44 -05:00
|
|
|
def target_iid
|
|
|
|
target.respond_to?(:iid) ? target.iid : target_id
|
|
|
|
end
|
2016-02-12 08:17:42 -05:00
|
|
|
end
|