lib/superstore/persistence.rb in superstore-2.5.0 vs lib/superstore/persistence.rb in superstore-3.0.0

- old
+ new

@@ -17,38 +17,33 @@ id = constraints.fetch(primary_key) adapter.update table_name, id, serialize_attributes(attributes) end - if Rails.version >= '6.0' - def instantiate_instance_of(klass, attributes, column_types = {}, &block) - if attributes[superstore_column].is_a?(String) - attributes = JSON.parse(attributes[superstore_column]).merge('id' => attributes['id']) - end - - super(klass, attributes, column_types, &block) - end - private :instantiate_instance_of - else - def instantiate(attributes, column_types = {}, &block) - if attributes[superstore_column].is_a?(String) - attributes = JSON.parse(attributes[superstore_column]).merge('id' => attributes['id']) - end - - super(attributes, column_types, &block) - end - end - def serialize_attributes(attributes) serialized = {} attributes.each do |attr_name, value| next if attr_name == primary_key serialized[attr_name] = attribute_types[attr_name].serialize(value) end serialized end private + + def instantiate_instance_of(klass, attributes, column_types = {}, &block) + if attributes[superstore_column].is_a?(String) + attributes = JSON.parse(attributes[superstore_column]).merge('id' => attributes['id']) + end + + if inheritance_column && attribute_types.key?(inheritance_column) + klass = find_sti_class(attributes[inheritance_column]) + end + + attributes.each_key { |k, v| attributes.delete(k) unless klass.attribute_types.key?(k) } + + super(klass, attributes, column_types, &block) + end def adapter @adapter ||= Superstore::Adapters::JsonbAdapter.new end end