Sha256: 84f92d2b000239dc8c17c998392fff93f16886760789e4240c7e66eabab8aa70

Contents?: true

Size: 1.57 KB

Versions: 1

Compression:

Stored size: 1.57 KB

Contents

# frozen_string_literal: true

require 'rails_compatibility'
require 'rails_compatibility/active_record'
require 'rails_compatibility/attribute_types'
require 'rails_compatibility/deserialize'

class << RailsCompatibility
  if Gem::Version.new(ActiveRecord::VERSION::STRING) >= Gem::Version.new('7.0.6')
    # Rails 7.0.6 changes parameter handling. Details at: https://github.com/rails/rails/pull/45783
    def cast_values(klass, result)
      attribute_types = self.attribute_types(klass)

      result.map do |attributes| # This map behaves different to array#map
        attributes.each_with_index do |(key, attribute), index|
          attributes[key] = deserialize(result.send(:column_type, key, index, attribute_types), attribute)
        end

        next attributes
      end
    end
  elsif GTE_RAILS_4_0
    def cast_values(klass, result)
      attribute_types = self.attribute_types(klass)

      result.map do |attributes| # This map behaves different to array#map
        attributes.each do |key, attribute|
          attributes[key] = deserialize(result.send(:column_type, key, attribute_types), attribute)
        end

        next attributes
      end
    end
  else
    def cast_values(klass, result)
      result.map! do |attributes| # This map! behaves different to array#map!
        initialized_attributes = klass.initialize_attributes(attributes)
        attributes.each do |key, _attribute|
          attributes[key] = klass.type_cast_attribute(key, initialized_attributes)
        end

        next attributes
      end
    end
  end
end

Version data entries

1 entries across 1 versions & 1 rubygems

Version Path
rails_compatibility-0.0.10 lib/rails_compatibility/cast_values.rb