Sha256: 8efad18f09c4d57b2ec5289852f38dce2ad5a6117964fd465aa7f3686a3322e6

Contents?: true

Size: 1.19 KB

Versions: 4

Compression:

Stored size: 1.19 KB

Contents

# Make templates supplied by the plugin read-only so they can be safely
# replaced during DB migrations.
module Bootdisk::ConfigTemplateExt
  extend ActiveSupport::Concern

  BOOTDISK_TMPLS = ['Boot disk iPXE - host', 'Boot disk iPXE - generic host']

  included do
    before_destroy :bootdisk_destroy?
    validate :bootdisk_readonly
  end

  def bootdisk_destroy?
    return true if ARGV.find { |a| a.start_with? "db:migrate" }
    bootdisk_add_error if BOOTDISK_TMPLS.include?(name)
    false
  end

  def bootdisk_readonly
    c = changes

    # ignore CRLF changes
    if c['template'] && (c['template'][0].nil? || c['template'][0].gsub("\n", "\r\n") == c['template'][1])
      c.delete 'template'
    end

    # ignore nil to false changes
    c.delete 'snippet' if c['snippet'] == [nil, false]

    # allow the user to associate it, just not change the content
    other_attrs = c.keys.find { |f| !['template_combinations', 'template_associations'].include? f }
    bootdisk_add_error if BOOTDISK_TMPLS.include?(name_was) && other_attrs
  end

  def bootdisk_add_error
    errors.add(:base, _("Template is read-only as it's supplied in foreman_bootdisk.  Please copy it to a new template to customize."))
  end
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreman_bootdisk-2.0.5 app/models/concerns/bootdisk/config_template_ext.rb
foreman_bootdisk-2.0.4 app/models/concerns/bootdisk/config_template_ext.rb
foreman_bootdisk-2.0.3 app/models/concerns/bootdisk/config_template_ext.rb
foreman_bootdisk-2.0.2 app/models/concerns/bootdisk/config_template_ext.rb