module ActiveFedora::Persistence
def assign_id
def assign_id end
def assign_rdf_subject
and assign_id can mint an id for the object, then assign it to the resource.
This is only used when creating a new record. If the object doesn't have an id
def assign_rdf_subject @ldp_source = if !id && new_id = assign_id LdpResource.new(ActiveFedora.fedora.connection, self.class.id_to_uri(new_id), @resource) else LdpResource.new(ActiveFedora.fedora.connection, @ldp_source.subject, @resource, ActiveFedora.fedora.host + ActiveFedora.fedora.base_path) end end
def assign_uri_to_attached_files
def assign_uri_to_attached_files attached_files.each do |name, ds| ds.uri= "#{uri}/#{name}" end end
def create_or_update(*args)
def create_or_update(*args) raise ReadOnlyRecord if readonly? new_record? ? create_record(*args) : update_record(*args) end
def create_record(options = {})
def create_record(options = {}) assign_rdf_subject serialize_attached_files @ldp_source = @ldp_source.create @resource = nil assign_uri_to_attached_files save_attached_files refresh end
def delete(opts = {})
(**opts)
-
:eradicate
(Boolean
) -- if passed in, eradicate the tombstone from Fedora
Parameters:
-
opts
(Hash
) --
def delete(opts = {}) return self if new_record? @destroyed = true reflections.each_pair do |name, reflection| if reflection.macro == :has_many association(name.to_sym).delete_all end end id = self.id ## cache so it's still available after delete # Clear out the ETag @ldp_source = build_ldp_resource(id) begin @ldp_source.delete rescue Ldp::NotFound raise ObjectNotFoundError, "Unable to find #{id} in the repository" end ActiveFedora::SolrService.delete(id) if ENABLE_SOLR_UPDATES if opts[:eradicate] self.class.eradicate(id) end freeze end
def destroy(*opts)
(**opts)
-
:eradicate
(Boolean
) -- if passed in, eradicate the tombstone from Fedora
Parameters:
-
opts
(Hash
) --
def destroy(*opts) raise ReadOnlyRecord if readonly? delete(*opts) end
def destroyed?
def destroyed? @destroyed end
def eradicate
def eradicate self.class.eradicate(self.id) end
def execute_sparql_update
def execute_sparql_update change_set = ChangeSet.new(self, self.resource, self.changed_attributes.keys) return true if change_set.empty? ActiveFedora.fedora.ldp_resource_service.update(change_set, self.class, id) end
def new_record?
def new_record? @ldp_source.new? end
def persisted?
def persisted? !(destroyed? || new_record?) end
def refresh
def refresh @ldp_source = build_ldp_resource(id) @resource = nil end
def save(*options)
-
(Boolean)
- true if save was successful, otherwise false
Options Hash:
(**options)
-
:update_index
(Boolean
) -- set false to skip indexing
Parameters:
-
options
(Hash
) --
def save(*options) create_or_update(*options) end
def save!(*args)
def save!(*args) create_or_update(*args) end
def save_attached_files
def save_attached_files attached_files.select { |_, file| file.changed? }.each do |_, file| file.save # Don't call save! because if the content_changed? returns false, it'll raise an error. end end
def update(attributes)
def update(attributes) self.attributes=attributes save end
def update_record(options = {})
def update_record(options = {}) serialize_attached_files execute_sparql_update save_attached_files refresh end