#376: refactoring according to Alexander's comments

This commit is contained in:
Vokhmin Alexey V 2014-04-17 23:15:35 +04:00
parent aea5c94219
commit 16f2bbb246
3 changed files with 9 additions and 5 deletions

View File

@ -70,19 +70,19 @@ ActiveAdmin.register NodeInstruction do
collection_action :lock_all, method: :post do
NodeInstruction.lock_all
flash[:info] = 'Locked successfully'
flash[:notice] = 'Locked successfully'
redirect_to admin_node_instructions_path
end
collection_action :unlock_all, method: :post do
NodeInstruction.unlock_all
flash[:info] = 'Unlocked successfully'
flash[:notice] = 'Unlocked successfully'
redirect_to admin_node_instructions_path
end
member_action :force, method: :patch do
resource.send(params[:state])
flash[:info] = 'Updated successfully'
flash[:notice] = 'Updated successfully'
redirect_to admin_node_instruction_path(resource)
end

View File

@ -5,7 +5,7 @@ class RestartNodesJob
return if NodeInstruction.all_locked?
available_nodes = RpmBuildNode.all.map{ |n| n.user_id }.compact.uniq
NodeInstruction.where(status: NodeInstruction::READY).
where('user_id NOT IN (?)', available_nodes).find_each(&:restart)
where.not(user_id: available_nodes).find_each(&:restart)
end
end

View File

@ -10,13 +10,17 @@ class NodeInstruction < ActiveRecord::Base
belongs_to :user
scope :duplicate, -> id, user_id {
where.not(id: id.to_i).where(user_id: user_id, status: STATUSES - [DISABLED])
}
attr_encrypted :instruction, key: APP_CONFIG['keys']['node_instruction_secret_key']
validates :user, presence: true
validates :instruction, presence: true, length: { maximum: 10000 }
validates :status, presence: true
validate -> {
errors.add(:status, 'Can be only single active instruction for each node') if !disabled? && NodeInstruction.where('id != ?', id.to_i).where(user_id: user_id, status: STATUSES - [DISABLED]).exists?
errors.add(:status, 'Can be only single active instruction for each node') if !disabled? && NodeInstruction.duplicate(id.to_i, user_id).exists?
}
attr_accessible :instruction, :user_id, :output, :status