Class: DeliveryCenter::Application

Inherits:
ApplicationRecord show all
Defined in:
lib/delivery_center/models/application.rb

Instance Method Summary collapse

Instance Method Details

#can_deploy?Boolean

Returns:

  • (Boolean)


7
8
9
# File 'lib/delivery_center/models/application.rb', line 7

def can_deploy?
  current_revision != recent_revision
end

#can_rollback?Boolean

Returns:

  • (Boolean)


21
22
23
24
25
# File 'lib/delivery_center/models/application.rb', line 21

def can_rollback?
  current = deploys.find_by(current: true)
  return false if current.nil?
  deploys.where('id < :current_id', current_id: current.id).order(id: :desc).exists?
end

#current_revisionObject



39
40
41
# File 'lib/delivery_center/models/application.rb', line 39

def current_revision
  deploys.find_by(current: true)&.revision
end

#deploy!Object



11
12
13
14
15
16
17
18
19
# File 'lib/delivery_center/models/application.rb', line 11

def deploy!
  self.class.transaction do
    raise DeliveryCenter::AlreadyDeployedError unless can_deploy?
    deploy = deploys.find_or_create_by!(revision: recent_revision)
    deploy.mark_current!
    deploys.reload # invalidate older current status has object cache
    deploy
  end
end

#recent_revisionObject



43
44
45
# File 'lib/delivery_center/models/application.rb', line 43

def recent_revision
  revisions.last
end

#rollback!Object



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/delivery_center/models/application.rb', line 27

def rollback!
  self.class.transaction do
    current = deploys.find_by(current: true)
    raise DeliveryCenter::PreviousDeployNotExistError if current.nil?
    previous = deploys.where('id < :current_id', current_id: current.id).order(id: :desc).first
    raise DeliveryCenter::PreviousDeployNotExistError if previous.nil?
    previous.mark_current!
    deploys.reload # invalidate older current status has object cache
    previous
  end
end