Module: ForemanExpireHosts::HostControllerExtensions

Defined in:
app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.prepended(base) ⇒ Object



5
6
7
8
9
10
11
# File 'app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb', line 5

def self.prepended(base)
  base.class_eval do
    before_action :validate_multiple_expiration, :only => :update_multiple_expiration
    before_action :find_multiple_with_expire_hosts, :only => [:select_multiple_expiration, :update_multiple_expiration]
    alias_method :find_multiple_with_expire_hosts, :find_multiple
  end
end

Instance Method Details

#select_multiple_expirationObject



13
# File 'app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb', line 13

def select_multiple_expiration; end

#update_multiple_expirationObject



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'app/controllers/concerns/foreman_expire_hosts/host_controller_extensions.rb', line 15

def update_multiple_expiration
  failed_hosts = {}

  @hosts.each do |host|
    begin
      host.expired_on = expiration_date
      host.save!
    rescue StandardError => e
      failed_hosts[host.name] = e
      message = if expiration_date.present?
                  _('Failed to set expiration date for %{host} to %{expiration_date}.') % { :host => host, :expiration_date => l(expiration_date) }
                else
                  _('Failed to clear expiration date for %s.') % host
                end
      Foreman::Logging.exception(message, e)
    end
  end

  if failed_hosts.empty?
    if expiration_date.present?
      success _('The expiration date of the selected hosts was set to %s.') % l(expiration_date)
    else
      success _('The expiration date of the selected hosts was cleared.')
    end
  else
    error n_('The expiration date could not be set for host: %s.',
             'The expiration date could not be set for hosts: %s.',
             failed_hosts.count) % failed_hosts.map { |h, err| "#{h} (#{err})" }.to_sentence
  end
  redirect_back_or_to hosts_path
end