Persistence/RecordInvalidError
Persistence/ClassMethods
- USES_CLASSNAME
#_active_record_destroyed_behavior?
ruby
- def _active_record_destroyed_behavior?
fail 'Remove this workaround in 6.0.0' if Neo4j::VERSION >= '6.0.0'
!!Neo4j::Config[:_active_record_destroyed_behavior]
end
#_create_node
ruby
- def _create_node(*args)
session = self.class.neo4j_session props = self.class.default_property_values(self) props.merge!(args[0]) if args[0].is_a?(Hash) set_classname(props) labels = self.class.mapped_label_names session.create_node(props, labels)
end
- #_destroyed_double_check?
These two methods should be removed in 6.0.0
ruby
- def _destroyed_double_check?
- if _active_record_destroyed_behavior?
false
- else
(!new_record? && !exist?)
end
end
#apply_default_values
ruby
- def apply_default_values
return if self.class.declared_property_defaults.empty? self.class.declared_property_defaults.each_pair do self.send("#{key}=", value) if self.send(key).nil? end
end
#cache_key
ruby
- def cache_key
- if self.new_record?
"#{model_cache_key}/new"
- elsif self.respond_to?(:updated_at) && !self.updated_at.blank?
"#{model_cache_key}/#{neo_id}-#{self.updated_at.utc.to_s(:number)}"
- else
"#{model_cache_key}/#{neo_id}"
end
end
- #create_model
Creates a model with values matching those of the instance attributes and returns its id.
ruby
- def create_model(*)
create_magic_properties set_timestamps create_magic_properties properties = self.class.declared_property_manager.convert_properties_to(self, :db, props) node = _create_node(properties) init_on_load(node, node.props) send_props(@relationship_props) if @relationship_props @relationship_props = @deferred_nodes = nil true
end
#create_or_update
ruby
- 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 true end
- rescue => e
Neo4j::Transaction.current.failure if Neo4j::Transaction.current raise e
- ensure
@_create_or_updating = nil
end
#destroy
ruby
- def destroy
freeze _persisted_obj && _persisted_obj.del @_deleted = true
end
- #destroyed?
Returns +true+ if the object was destroyed.
ruby
- def destroyed?
@_deleted || _destroyed_double_check?
end
#exist?
ruby
- def exist?
_persisted_obj && _persisted_obj.exist?
end
#freeze
ruby
- def freeze
@attributes.freeze self
end
#frozen?
ruby
- def frozen?
@attributes.frozen?
end
- #new?
Returns +true+ if the record hasn't been saved to Neo4j yet.
ruby
- def new_record?
!_persisted_obj
end
- #new_record?
Returns +true+ if the record hasn't been saved to Neo4j yet.
ruby
- def new_record?
!_persisted_obj
end
- #persisted?
Returns +true+ if the record is persisted, i.e. it's not a new record and it was not destroyed
ruby
- def persisted?
!new_record? && !destroyed?
end
#props
ruby
- def props
attributes.reject { v.nil? }.symbolize_keys
end
#reload
ruby
- def reload
return self if new_record? association_proxy_cache.clear changed_attributes && changed_attributes.clear unless reload_from_database @_deleted = true freeze end self
end
#reload_from_database
ruby
- def reload_from_database
# TODO: - Neo4j::IdentityMap.remove_node_by_id(neo_id) if reloaded = self.class.load_entity(neo_id) send(:attributes=, reloaded.attributes) end reloaded
end
- #save
Saves the model.
If the model is new a record gets created in the database, otherwise the existing record gets updated. If perform_validation is true validations run. If any of them fail the action is cancelled and save returns false. If the flag is false validations are bypassed altogether. See ActiveRecord::Validations for more information. There's a series of callbacks associated with save. If any of the before* callbacks return false the action is cancelled and save returns false.
ruby
- def save(*)
update_magic_properties cascade_save do association_proxy_cache.clear create_or_update end
end
- #save!
Persist the object to the database. Validations and Callbacks are included by default but validation can be disabled by passing :validate => false to #save! Creates a new transaction.
ruby
def save!(args) fail RecordInvalidError, self unless save(args) end
- #update
Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved. If saving fails because the resource is invalid then false will be returned.
ruby
- def update(attributes)
self.attributes = process_attributes(attributes) save
end
- #update!
Same as {#update_attributes}, but raises an exception if saving fails.
ruby
- def update!(attributes)
self.attributes = process_attributes(attributes) save!
end
- #update_attribute
Convenience method to set attribute and #save at the same time
ruby
- def update_attribute(attribute, value)
send("#{attribute}=", value) self.save
end
- #update_attribute!
Convenience method to set attribute and #save! at the same time
ruby
- def update_attribute!(attribute, value)
send("#{attribute}=", value) self.save!
end
- #update_attributes
Updates this resource with all the attributes from the passed-in Hash and requests that the record be saved. If saving fails because the resource is invalid then false will be returned.
ruby
- def update(attributes)
self.attributes = process_attributes(attributes) save
end
- #update_attributes!
Same as {#update_attributes}, but raises an exception if saving fails.
ruby
- def update!(attributes)
self.attributes = process_attributes(attributes) save!
end
#update_model
ruby
- def update_model
return if !changed_attributes || changed_attributes.empty?
changed_props = attributes.select { 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