Sha256: 4ee6d39dc25160bdc3de87f6bc81443ad77987422a7b4c2fd7728a55ce71d513
Contents?: true
Size: 1.57 KB
Versions: 4
Compression:
Stored size: 1.57 KB
Contents
module AdaptiveAlias module Hooks module ActiveRecordPersistence def _update_record AdaptiveAlias.rescue_statement_invalid(model: self) do attribute_set_fix! super end end def _create_record(*) AdaptiveAlias.rescue_statement_invalid(model: self) do attribute_set_fix! super end end private def attribute_set_fix! if attribute_names.any?{|name| self.class.attribute_aliases[name] } attributes = @attributes.instance_variable_get(:@attributes) attributes = attributes.instance_variable_get(:@delegate_hash) if attributes.is_a?(ActiveModel::LazyAttributeHash) delete_duplicate_keys!(attributes) attributes.transform_keys! do |key| self.class.attribute_aliases[key] || key end end end # delete duplicate keys caused by instantiate after migration but before patch removal. def delete_duplicate_keys!(attributes) duplicate_keys = {} delete_keys = [] attributes.each do |key, _| aliased_key = self.class.attribute_aliases[key] || key delete_keys << key if duplicate_keys[aliased_key] # delete old key and keep the new one duplicate_keys[aliased_key] = true end delete_keys.each{|s| attributes.delete(s) } end end end end # Nested module include is not supported until ruby 3.0 class ActiveRecord::Base prepend AdaptiveAlias::Hooks::ActiveRecordPersistence end
Version data entries
4 entries across 4 versions & 1 rubygems