Sha256: 1714b886b4d0a7d8e914c86e47a15e96f437b37865ef1b9d9e9df84761d4f458

Contents?: true

Size: 1.12 KB

Versions: 4

Compression:

Stored size: 1.12 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 gPXE - host', 'Kickstart boot disk gPXE']

  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_readonly
    errors.empty?
  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 }
    if BOOTDISK_TMPLS.include?(name_was) && other_attrs
      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
end

Version data entries

4 entries across 4 versions & 1 rubygems

Version Path
foreman_bootdisk-1.2.3 app/models/concerns/bootdisk/config_template_ext.rb
foreman_bootdisk-1.2.2 app/models/concerns/bootdisk/config_template_ext.rb
foreman_bootdisk-1.2.1 app/models/concerns/bootdisk/config_template_ext.rb
foreman_bootdisk-1.2.0 app/models/concerns/bootdisk/config_template_ext.rb