Sha256: 3825e731021c00b31d2e4872b8a2ffdd20a7fdcdc436cbb674ee189ed44a7baf

Contents?: true

Size: 1.42 KB

Versions: 19

Compression:

Stored size: 1.42 KB

Contents

# frozen_string_literal: true

module ActiveRecord
  module LegacyYamlAdapter # :nodoc:
    def self.convert(klass, coder)
      return coder unless coder.is_a?(Psych::Coder)

      case coder["active_record_yaml_version"]
      when 1, 2 then coder
      else
        ActiveSupport::Deprecation.warn(<<-MSG.squish)
          YAML loading from legacy format older than Rails 5.0 is deprecated
          and will be removed in Rails 7.0.
        MSG
        if coder["attributes"].is_a?(ActiveModel::AttributeSet)
          Rails420.convert(klass, coder)
        else
          Rails41.convert(klass, coder)
        end
      end
    end

    module Rails420 # :nodoc:
      def self.convert(klass, coder)
        attribute_set = coder["attributes"]

        klass.attribute_names.each do |attr_name|
          attribute = attribute_set[attr_name]
          if attribute.type.is_a?(Delegator)
            type_from_klass = klass.type_for_attribute(attr_name)
            attribute_set[attr_name] = attribute.with_type(type_from_klass)
          end
        end

        coder
      end
    end

    module Rails41 # :nodoc:
      def self.convert(klass, coder)
        attributes = klass.attributes_builder
          .build_from_database(coder["attributes"])
        new_record = coder["attributes"][klass.primary_key].blank?

        {
          "attributes" => attributes,
          "new_record" => new_record,
        }
      end
    end
  end
end

Version data entries

19 entries across 19 versions & 2 rubygems

Version Path
activerecord-6.1.7.10 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.9 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.8 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.7 lib/active_record/legacy_yaml_adapter.rb
scrapbook-0.3.2 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.6 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.5 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.4 lib/active_record/legacy_yaml_adapter.rb
scrapbook-0.3.1 vendor/ruby/2.7.0/gems/activerecord-6.1.6.1/lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.3 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.2 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7.1 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.7 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.6.1 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.6 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.5.1 lib/active_record/legacy_yaml_adapter.rb
activerecord-6.1.5 lib/active_record/legacy_yaml_adapter.rb
activerecord-7.0.0.alpha2 lib/active_record/legacy_yaml_adapter.rb
activerecord-7.0.0.alpha1 lib/active_record/legacy_yaml_adapter.rb