2015-08-25 21:42:46 -04:00
|
|
|
module Ci
|
|
|
|
class Trigger < ActiveRecord::Base
|
|
|
|
extend Ci::Model
|
2015-09-10 09:52:52 -04:00
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
acts_as_paranoid
|
|
|
|
|
2017-03-17 19:06:11 -04:00
|
|
|
belongs_to :project
|
2017-02-15 13:36:31 -05:00
|
|
|
belongs_to :owner, class_name: "User"
|
|
|
|
|
2016-10-18 17:20:36 -04:00
|
|
|
has_many :trigger_requests, dependent: :destroy
|
2015-08-25 21:42:46 -04:00
|
|
|
|
2017-02-15 13:36:31 -05:00
|
|
|
validates :token, presence: true, uniqueness: true
|
2015-08-25 21:42:46 -04:00
|
|
|
|
|
|
|
before_validation :set_default_values
|
|
|
|
|
|
|
|
def set_default_values
|
|
|
|
self.token = SecureRandom.hex(15) if self.token.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def last_trigger_request
|
|
|
|
trigger_requests.last
|
|
|
|
end
|
|
|
|
|
2016-01-13 06:16:27 -05:00
|
|
|
def last_used
|
|
|
|
last_trigger_request.try(:created_at)
|
|
|
|
end
|
|
|
|
|
2015-08-25 21:42:46 -04:00
|
|
|
def short_token
|
2017-02-15 13:36:31 -05:00
|
|
|
token[0...4]
|
|
|
|
end
|
|
|
|
|
2017-03-07 08:02:56 -05:00
|
|
|
def legacy?
|
|
|
|
self.owner_id.blank?
|
|
|
|
end
|
|
|
|
|
|
|
|
def can_access_project?
|
|
|
|
self.owner_id.blank? || Ability.allowed?(self.owner, :create_build, project)
|
2015-08-25 21:42:46 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|