samvera-deprecated / activefedora-aggregation

[DEPRECATED; moved into active_fedora] Aggregation relationship for ActiveFedora
Other
3 stars 2 forks source link

Gem Version Build Status

ActiveFedora::Aggregation

Aggregations for ActiveFedora: manage a group of related objects using predicates from the OAI-ORE data model. Provides the foundation for flexible relationships, including items appearing multiple times in a group, flexible/optional ordering, etc.

Used extensively by Hydra::PCDM.

Example

class GenericFile < ActiveFedora::Base
end

generic_file1 = GenericFile.create(id: 'file1')
generic_file2 = GenericFile.create(id: 'file2')
generic_file3 = GenericFile.create(id: 'file3')

class Image < ActiveFedora::Base
  ordered_aggregation :generic_files, through: :list_source
end

image = Image.create(id: 'my_image')
image.ordered_generic_file_proxies.append_target generic_file2
image.ordered_generic_file_proxies.append_target generic_file1
image.save
image.generic_files # => [#<GenericFile id: "file2">, #<GenericFile id: "file1">]
image.ordered_generic_files.to_a # => [#<GenericFile id: "file2">, #<GenericFile id: "file1">]

# Not all generic files must be ordered.
image.generic_files += [generic_file3]
image.generic_files => [#<GenericFile id: "file2">, #<GenericFile id: "file1">, #<GenericFile id: "file3">]
image.ordered_generic_files.to_a => [#<GenericFile id: "file2">, #<GenericFile id: "file1">]

# non-ordered accessor is not ordered.
image.ordered_generic_file_proxies.insert_target_at(0, generic_file3)
image.generic_files # => [#<GenericFile id: "file2">, #<GenericFile id: "file1">, #<GenericFile id: "file3">]
image.ordered_generic_files.to_a # => [#<GenericFile id: "file3">, #<GenericFile id: "file2">, #<GenericFile id: "file1">] 

# Deletions
image.ordered_generic_file_proxies.delete_at(1) => #<GenericFile id: "file2">
image.ordered_generic_files.to_a # => [#<GenericFile id: "file3">, #<GenericFile id: "file1">]
image.generic_files # => [#<GenericFile id: "file2">, #<GenericFile id: "file1">, #<GenericFile id: "file3">]