gitlab-org--gitlab-foss/app/models/todo.rb

78 lines
1.6 KiB
Ruby
Raw Normal View History

2016-02-12 08:17:42 -05:00
# == Schema Information
#
2016-02-20 08:59:59 -05:00
# Table name: todos
2016-02-12 08:17:42 -05:00
#
# id :integer not null, primary key
# user_id :integer not null
# project_id :integer not null
# target_id :integer
2016-02-12 08:17:42 -05:00
# target_type :string not null
# author_id :integer
2016-02-18 08:51:53 -05:00
# action :integer not null
2016-02-12 08:17:42 -05:00
# state :string not null
# created_at :datetime
# updated_at :datetime
# note_id :integer
# commit_id :string
2016-02-12 08:17:42 -05:00
#
2016-02-20 08:59:59 -05:00
class Todo < ActiveRecord::Base
2016-02-12 13:45:44 -05:00
ASSIGNED = 1
MENTIONED = 2
2016-02-12 13:45:44 -05:00
2016-02-12 08:17:42 -05:00
belongs_to :author, class_name: "User"
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
validates :action, :project, :target_type, :user, presence: true
validates :target_id, presence: true, unless: :for_commit?
validates :commit_id, presence: true, if: :for_commit?
2016-02-12 08:17:42 -05:00
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
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
2016-02-18 14:16:39 -05:00
def body
if note.present?
note.note
else
target.title
end
end
def for_commit?
target_type == "Commit"
end
# override to return commits, which are not active record
def target
if for_commit?
2016-03-18 12:27:27 -04:00
project.commit(commit_id) rescue nil
else
super
end
end
def target_reference
if for_commit?
target.short_id
else
target.to_reference
end
end
2016-02-12 08:17:42 -05:00
end