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

- old
+ new

@@ -1,141 +1,57 @@ module Superstore module Persistence extend ActiveSupport::Concern - included do - class_attribute :batch_statements - end - module ClassMethods - def delete(ids) - adapter.delete table_name, ids + def find_by_id(id) + find_by(id: id) end - def delete_all - adapter.execute "TRUNCATE #{table_name}" - end + def _insert_record(attributes) + id = attributes.fetch(primary_key) - def insert_record(id, attributes) - adapter.insert table_name, id, encode_attributes(attributes) + adapter.insert table_name, id, serialize_attributes(attributes) end - def update_record(id, attributes) - adapter.update table_name, id, encode_attributes(attributes) - end + def _update_record(attributes, constraints) + id = constraints.fetch(primary_key) - def batching? - adapter.batching? + adapter.update table_name, id, serialize_attributes(attributes) end - def batch(&block) - adapter.batch(&block) - end - - def instantiate(id, attributes) - allocate.tap do |object| - object.instance_variable_set("@id", id) if id - object.instance_variable_set("@new_record", false) - object.instance_variable_set("@destroyed", false) - object.instance_variable_set("@attributes", typecast_persisted_attributes(attributes)) - object.instance_variable_set("@association_cache", {}) - end - end - - def encode_attributes(attributes) - encoded = {} - attributes.each do |column_name, value| - if value.nil? - encoded[column_name] = nil - else - encoded[column_name] = attribute_definitions[column_name].type.encode(value) + 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 - end - encoded - end - private - - def quote_columns(column_names) - column_names.map { |name| "'#{name}'" } + super(klass, attributes, column_types, &block) end - - def typecast_persisted_attributes(attributes) - result = {} - - attributes.each do |key, value| - if definition = attribute_definitions[key] - result[key] = definition.instantiate(value) - 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 - result + super(attributes, column_types, &block) end - end - - def new_record? - @new_record - end - - def destroyed? - @destroyed - end - - def persisted? - !(new_record? || destroyed?) - end - - def save(*) - create_or_update - end - - def destroy - self.class.delete(id) - @destroyed = true - end - - def update_attribute(name, value) - name = name.to_s - send("#{name}=", value) - save(validate: false) - end - - def update(attributes) - self.attributes = attributes - save - end - - alias update_attributes update - - def update!(attributes) - self.attributes = attributes - save! - end - - alias update_attributes! update! - - def reload - clear_association_cache - @attributes = self.class.find(id).instance_variable_get('@attributes') - self - end - - private - - def create_or_update - new_record? ? create_self : update_self end - def create_self - write :insert_record + 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 - def update_self - write :update_record - end + private - def write(method) - @new_record = false - self.class.send(method, id, unapplied_changes) - end + def adapter + @adapter ||= Superstore::Adapters::JsonbAdapter.new + end + end end end