Sha256: b71d6ffd139e455bd59f7846cba1bc2409b8acf2cc831b5317658d715d202d63

Contents?: true

Size: 1 KB

Versions: 5

Compression:

Stored size: 1 KB

Contents

module Foreman
  module STI
    def self.included(base)
      base.class_eval do
        class << self
          # ensures that the correct STI object is created when :type is passed.
          def new_with_cast(*attributes, &block)
            if (h = attributes.first).is_a?(Hash) && (type = h.delete(:type)) && type.length > 0
              if (klass = type.constantize) != self
                raise "Invalid type #{type}" unless klass <= self
                return klass.new(*attributes, &block)
              end
            end

            new_without_cast(*attributes, &block)
          end

          alias_method_chain :new, :cast
        end
        base.alias_method_chain :save, :type
      end
    end
    def save_with_type(*args)
      self.class.instance_variable_set("@finder_needs_type_condition", :false) if self.type_changed?
      value = save_without_type(*args)
      self.class.instance_variable_set("@finder_needs_type_condition", :true)  if self.type_changed?
      return value
    end
  end
end

Version data entries

5 entries across 5 versions & 1 rubygems

Version Path
foreman_discovery-1.0.0 test/foreman_app/lib/foreman/sti.rb
foreman_discovery-1.0.0.rc4 test/foreman_app/lib/foreman/sti.rb
foreman_discovery-1.0.0.rc3 test/foreman_app/lib/foreman/sti.rb
foreman_discovery-1.0.0.rc2 test/foreman_app/lib/foreman/sti.rb
foreman_discovery-1.0.0.rc1 test/foreman_app/lib/foreman/sti.rb