module ActiveFedora::Persistence

Active Fedora Persistence

Public Instance Methods

base_path_for_resource=(path) click to toggle source

Used when setting containment

# File lib/active_fedora/persistence.rb, line 111
def base_path_for_resource=(path)
  @base_path = path
end
delete(opts = {}) click to toggle source

Deletes an object from Fedora and deletes the indexed record from Solr. Delete does not run any callbacks, so consider using destroy instead. @param [Hash] opts @option opts [Boolean] :eradicate if passed in, eradicate the tombstone from Fedora

# File lib/active_fedora/persistence.rb, line 68
def delete(opts = {})
  return self if new_record?

  @destroyed = true

  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 ActiveFedora.enable_solr_updates?
  self.class.eradicate(id) if opts[:eradicate]
  freeze
end
destroy(*opts) click to toggle source

Delete the object from Fedora and Solr. Run any before/after/around callbacks for destroy @param [Hash] opts @option opts [Boolean] :eradicate if passed in, eradicate the tombstone from Fedora

# File lib/active_fedora/persistence.rb, line 90
def destroy(*opts)
  raise ReadOnlyRecord if readonly?
  delete(*opts)
end
destroy!() click to toggle source

Deletes the record in the database and freezes this instance to reflect that no changes should be made (since they can’t be persisted).

There’s a series of callbacks associated with destroy!. If the before_destroy callback throws :abort the action is cancelled and destroy! raises ActiveFedora::RecordNotDestroyed. See ActiveFedora::Callbacks for further details.

# File lib/active_fedora/persistence.rb, line 102
def destroy!
  destroy || _raise_record_not_destroyed
end
destroyed?() click to toggle source

Returns true if this object has been destroyed, otherwise returns false.

# File lib/active_fedora/persistence.rb, line 28
def destroyed?
  @destroyed
end
eradicate() click to toggle source
# File lib/active_fedora/persistence.rb, line 106
def eradicate
  self.class.eradicate(id)
end
new_record?() click to toggle source
# File lib/active_fedora/persistence.rb, line 13
def new_record?
  return true if @ldp_source.subject.nil?
  @ldp_source.get
  false
rescue Ldp::Gone
  false
rescue Ldp::NotFound
  true
end
persisted?() click to toggle source
# File lib/active_fedora/persistence.rb, line 23
def persisted?
  !(destroyed? || new_record?)
end
save(*options) click to toggle source

Saves a Base object, and any dirty attached files, then updates the Solr index for this object, unless option :update_index=>false is present. Indexing is also controlled by the ‘create_needs_index?’ and ‘update_needs_index?’ methods.

@param [Hash] options @option options [Boolean] :update_index (true) set false to skip indexing @return [Boolean] true if save was successful, otherwise false

# File lib/active_fedora/persistence.rb, line 39
def save(*options)
  create_or_update(*options)
end
save!(*args) click to toggle source
# File lib/active_fedora/persistence.rb, line 43
def save!(*args)
  create_or_update(*args)
end
update(attributes) click to toggle source

Pushes the object and all of its new or dirty attached files into Fedora

# File lib/active_fedora/persistence.rb, line 48
def update(attributes)
  assign_attributes(attributes)
  save
end
Also aliased as: update_attributes
update!(attributes) click to toggle source

Updates its receiver just like update but calls save! instead of save, so an exception is raised if the record is invalid and saving will fail.

# File lib/active_fedora/persistence.rb, line 57
def update!(attributes)
  assign_attributes(attributes)
  save!
end
Also aliased as: update_attributes!
update_attributes(attributes)
Alias for: update
update_attributes!(attributes)
Alias for: update!

Private Instance Methods

_create_record(_options = {}) click to toggle source

Deals with preparing new object to be saved to Fedora, then pushes it and its attached files into Fedora.

# File lib/active_fedora/persistence.rb, line 184
def _create_record(_options = {})
  assign_rdf_subject
  serialize_attached_files
  @ldp_source = @ldp_source.create
  assign_uri_to_contained_resources
  save_contained_resources
  refresh
end
_raise_record_not_destroyed() click to toggle source
# File lib/active_fedora/persistence.rb, line 200
def _raise_record_not_destroyed
  @_association_destroy_exception ||= nil
  raise @_association_destroy_exception || RecordNotDestroyed.new("Failed to destroy the record", self)
ensure
  @_association_destroy_exception = nil
end
_update_record(_options = {}) click to toggle source
# File lib/active_fedora/persistence.rb, line 193
def _update_record(_options = {})
  serialize_attached_files
  execute_sparql_update
  save_contained_resources
  refresh
end
assign_id() click to toggle source

Override to tie in an ID minting service

# File lib/active_fedora/persistence.rb, line 219
def assign_id
  identifier_service.mint
end
assign_rdf_subject() click to toggle source

This is only used when creating a new record. If the object doesn’t have an id and assign_id can mint an id for the object, then assign it to the resource. Otherwise the resource will have the id assigned by the LDP server

# File lib/active_fedora/persistence.rb, line 230
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, base_path_for_resource)
                end
end
assign_uri_to_contained_resources() click to toggle source
# File lib/active_fedora/persistence.rb, line 255
def assign_uri_to_contained_resources
  contained_resources.each do |name, source|
    source.uri = "#{uri}/#{name}"
  end
end
base_path_for_resource() click to toggle source
# File lib/active_fedora/persistence.rb, line 238
def base_path_for_resource
  @base_path ||= ActiveFedora.fedora.host + default_base_path_for_resource
end
contained_rdf_sources() click to toggle source
# File lib/active_fedora/persistence.rb, line 271
def contained_rdf_sources
  @contained_rdf_sources ||=
    AssociationHash.new(self, self.class.contained_rdf_source_reflections)
end
contained_resources() click to toggle source
# File lib/active_fedora/persistence.rb, line 267
def contained_resources
  @contained_resources ||= attached_files.merge(contained_rdf_sources)
end
create_or_update(*args) click to toggle source
# File lib/active_fedora/persistence.rb, line 177
def create_or_update(*args)
  raise ReadOnlyRecord if readonly?
  result = new_record? ? _create_record(*args) : _update_record(*args)
  result != false
end
default_base_path_for_resource() click to toggle source
# File lib/active_fedora/persistence.rb, line 242
def default_base_path_for_resource
  init_root_path if has_uri_prefix?
  root_resource_path
end
execute_sparql_update() click to toggle source
# File lib/active_fedora/persistence.rb, line 212
def execute_sparql_update
  change_set = ChangeSet.new(self, resource, changed_attributes.keys)
  return true if change_set.empty?
  ActiveFedora.fedora.ldp_resource_service.update(change_set, self.class, id)
end
identifier_service() click to toggle source
# File lib/active_fedora/persistence.rb, line 223
def identifier_service
  @identifier_service ||= identifier_service_class.new
end
init_root_path() click to toggle source

Check to see if the :base_path (from fedora.yml) exists in Fedora. If it doesn’t exist, then create it.

# File lib/active_fedora/persistence.rb, line 248
def init_root_path
  path = root_resource_path.gsub(/^\//, "")
  ActiveFedora.fedora.connection.head(path)
rescue Ldp::NotFound
  ActiveFedora.fedora.connection.put(path, "")
end
refresh() click to toggle source
# File lib/active_fedora/persistence.rb, line 207
def refresh
  @ldp_source = build_ldp_resource(id)
  @resource = nil
end
save_contained_resources() click to toggle source
# File lib/active_fedora/persistence.rb, line 261
def save_contained_resources
  contained_resources.changed.each do |_, resource|
    resource.save
  end
end