lib/neo4j/shared/persistence.rb in neo4j-4.1.5 vs lib/neo4j/shared/persistence.rb in neo4j-5.0.0.rc.1

- old
+ new

@@ -1,19 +1,18 @@ module Neo4j::Shared module Persistence extend ActiveSupport::Concern - include Neo4j::Shared::TypeConverters USES_CLASSNAME = [] def update_model - if changed_attributes && !changed_attributes.empty? - changed_props = attributes.select { |k, _| changed_attributes.include?(k) } - changed_props = convert_properties_to :db, changed_props - _persisted_obj.update_props(changed_props) - changed_attributes.clear - end + return if !changed_attributes || changed_attributes.empty? + + changed_props = attributes.select { |k, _| changed_attributes.include?(k) } + changed_props = self.class.declared_property_manager.convert_properties_to(self, :db, changed_props) + _persisted_obj.update_props(changed_props) + changed_attributes.clear end # Convenience method to set attribute and #save at the same time # @param [Symbol, String] attribute of the attribute to update # @param [Object] value to set @@ -31,10 +30,11 @@ end def create_or_update # since the same model can be created or updated twice from a relationship we have to have this guard @_create_or_updating = true + apply_default_values result = _persisted_obj ? update_model : create_model if result == false Neo4j::Transaction.current.failure if Neo4j::Transaction.current false else @@ -45,10 +45,17 @@ raise e ensure @_create_or_updating = nil end + def apply_default_values + return if self.class.declared_property_defaults.empty? + self.class.declared_property_defaults.each_pair do |key, value| + self.send("#{key}=", value) if self.send(key).nil? + end + end + # Returns +true+ if the record is persisted, i.e. it's not a new record and it was not destroyed def persisted? !new_record? && !destroyed? end @@ -90,10 +97,10 @@ self end def reload return self if new_record? - clear_association_cache + association_proxy_cache.clear changed_attributes && changed_attributes.clear unless reload_from_database @_deleted = true freeze end