Module: ActiveFedora::Associations::ClassMethods
- Defined in:
- lib/active_fedora/associations.rb
Instance Method Summary collapse
-
#belongs_to(name, options = {}) ⇒ Object
Specifies a one-to-one association with another class.
-
#directly_contains(name, options = {}) ⇒ Object
This method is used to declare an ldp:DirectContainer on a resource you must specify an is_member_of_relation or a has_member_relation.
- #directly_contains_one(name, options = {}) ⇒ Object
-
#filters_association(extending_from, options = {}) ⇒ Object
Create an association filter on the class.
-
#has_and_belongs_to_many(name, options = {}) ⇒ Object
Specifies a many-to-many relationship with another class.
- #has_many(name, options = {}) ⇒ Object
-
#has_subresource(name, options = {}) { ... } ⇒ Object
This method is used to specify the details of a contained resource.
-
#indirectly_contains(name, options = {}) ⇒ Object
This method is used to declare an ldp:IndirectContainer on a resource you must specify an is_member_of_relation or a has_member_relation.
-
#is_a_container(options = {}) ⇒ Object
This method is used to declare this resource acts like an LDP BasicContainer.
-
#ordered_aggregation(name, options = {}) ⇒ Object
Convenience method for building an ordered aggregation.
-
#orders(name, options = {}) ⇒ Object
Allows ordering of an association.
Instance Method Details
#belongs_to(name, options = {}) ⇒ Object
Specifies a one-to-one association with another class. This method should only be used if this class contains the foreign key.
Methods will be added for retrieval and query for a single associated object, for which this object holds an id:
- association()
-
Returns the associated object.
nil
is returned if none is found. - association=(associate)
-
Assigns the associate object, extracts the primary key, and sets it as the foreign key.
(association
is replaced with the symbol passed as the first argument, so belongs_to :author
would add among others author.nil?
.)
Example
A Post class declares belongs_to :author
, which will add:
-
Post#author
(similar toAuthor.find(author_id)
) -
Post#author=(author)
The declaration can also include an options hash to specialize the behavior of the association.
Options
- :predicate
-
the association predicate to use when storing the association
REQUIRED
- :class_name
-
Specify the class name of the association. Use it only if that name can’t be inferred from the association name. So
has_one :author
will by default be linked to the Author class, but if the real class name is Person, you’ll have to specify it with this option.
Option examples:
belongs_to :firm, predicate: OurVocab.clientOf
belongs_to :author, class_name: "Person", predicate: OurVocab.
236 237 238 239 240 |
# File 'lib/active_fedora/associations.rb', line 236 def belongs_to(name, = {}) Builder::BelongsTo.build(self, name, ) Builder::SingularProperty.build(self, name, ) end |
#directly_contains(name, options = {}) ⇒ Object
This method is used to declare an ldp:DirectContainer on a resource you must specify an is_member_of_relation or a has_member_relation
example:
class FooHistory < ActiveFedora::Base
directly_contains :files, has_member_relation:
::RDF::URI.new("http://example.com/hasFiles"), class_name: 'Thing'
directly_contains :other_stuff, is_member_of_relation:
::RDF::URI.new("http://example.com/isContainedBy"), class_name: 'Thing'
end
143 144 145 |
# File 'lib/active_fedora/associations.rb', line 143 def directly_contains(name, = {}) Builder::DirectlyContains.build(self, name, { class_name: 'ActiveFedora::File' }.merge()) end |
#directly_contains_one(name, options = {}) ⇒ Object
147 148 149 |
# File 'lib/active_fedora/associations.rb', line 147 def directly_contains_one(name, = {}) Builder::DirectlyContainsOne.build(self, name, { class_name: 'ActiveFedora::File' }.merge()) end |
#filters_association(extending_from, options = {}) ⇒ Object
Create an association filter on the class
338 339 340 341 |
# File 'lib/active_fedora/associations.rb', line 338 def filters_association(extending_from, = {}) name = .delete(:as) Builder::Filter.build(self, name, .merge(extending_from: extending_from)) end |
#has_and_belongs_to_many(name, options = {}) ⇒ Object
Specifies a many-to-many relationship with another class. The relatioship is written to both classes simultaneously.
Adds the following methods for retrieval and query:
- collection(force_reload = false)
-
Returns an array of all the associated objects. An empty array is returned if none are found.
- collection<<(object, …)
-
Adds one or more objects to the collection by creating associations in the join table (
collection.push
andcollection.concat
are aliases to this method). Note that this operation instantly fires update sql without waiting for the save or update call on the parent object. - collection.delete(object, …)
-
Removes one or more objects from the collection by removing their associations from the join table. This does not destroy the objects.
- collection=objects
-
Replaces the collection’s content by deleting and adding objects as appropriate.
- collection_singular_ids
-
Returns an array of the associated objects’ ids.
- collection_singular_ids=ids
-
Replace the collection by the objects identified by the primary keys in
ids
. - collection.clear
-
Removes every object from the collection. This does not destroy the objects.
- collection.empty?
-
Returns
true
if there are no associated objects. - collection.size
-
Returns the number of associated objects.
(collection
is replaced with the symbol passed as the first argument, so has_and_belongs_to_many :categories
would add among others categories.empty?
.)
Example
A Developer class declares has_and_belongs_to_many :projects
, which will add:
-
Developer#projects
-
Developer#projects<<
-
Developer#projects.delete
-
Developer#projects=
-
Developer#project_ids
-
Developer#project_ids=
-
Developer#projects.clear
-
Developer#projects.empty?
-
Developer#projects.size
-
Developer#projects.find(id)
-
Developer#projects.exists?(...)
The declaration may include an options hash to specialize the behavior of the association.
Options
- :class_name
-
Specify the class name of the association. Use it only if that name can’t be inferred from the association name. So
has_and_belongs_to_many :projects
will by default be linked to the Project class, but if the real class name is SuperProject, you’ll have to specify it with this option. - :predicate
-
REQUIRED Specify the predicate to use when storing the relationship.
- :inverse_of
-
Specify the predicate to use when storing the relationship on the foreign object. If it is not provided, the relationship will not set the foriegn association.
Option examples:
has_and_belongs_to_many :projects, predicate: OurVocab.worksOn
has_and_belongs_to_many :nations, class_name: "Country", predicate: OurVocab.isCitizenOf
has_and_belongs_to_many :topics, predicate: RDF::FOAF.isPrimaryTopicOf, inverse_of: :is_topic_of
304 305 306 307 |
# File 'lib/active_fedora/associations.rb', line 304 def has_and_belongs_to_many(name, = {}) Builder::HasAndBelongsToMany.build(self, name, ) Builder::Property.build(self, name, .slice(:class_name, :predicate)) end |
#has_many(name, options = {}) ⇒ Object
182 183 184 |
# File 'lib/active_fedora/associations.rb', line 182 def has_many(name, = {}) Builder::HasMany.build(self, name, ) end |
#has_subresource(name, options = {}) { ... } ⇒ Object
This method is used to specify the details of a contained resource. Pass the name as the first argument and a hash of options as the second argument Note that this method doesn’t actually execute the block, but stores it, to be executed by any the implementation of the resource(specified as :class_name)
197 198 199 200 201 |
# File 'lib/active_fedora/associations.rb', line 197 def has_subresource(name, = {}, &block) [:block] = block if block raise ArgumentError, "You must provide a path name (f.k.a. dsid) for the resource" unless name Associations::Builder::HasSubresource.build(self, name.to_sym, ) end |
#indirectly_contains(name, options = {}) ⇒ Object
This method is used to declare an ldp:IndirectContainer on a resource you must specify an is_member_of_relation or a has_member_relation
example:
class Proxy < ActiveFedora::Base
belongs_to :proxy_for, predicate: ::RDF::URI.new('http://www.openarchives.org/ore/terms/proxyFor'), class_name: 'ActiveFedora::Base'
end
class FooHistory < ActiveFedora::Base
indirectly_contains :files, has_member_relation: RDF::Vocab::ORE.aggregates,
inserted_content_relation: RDF::Vocab::ORE.proxyFor, class_name: 'Thing',
through: 'Proxy', foreign_key: :proxy_for
indirectly_contains :other_stuff, is_member_of_relation:
::RDF::URI.new("http://example.com/isContainedBy"), class_name: 'Thing',
through: 'Proxy', foreign_key: :proxy_for
end
178 179 180 |
# File 'lib/active_fedora/associations.rb', line 178 def indirectly_contains(name, = {}) Builder::IndirectlyContains.build(self, name, ) end |
#is_a_container(options = {}) ⇒ Object
This method is used to declare this resource acts like an LDP BasicContainer
example:
class FooHistory < ActiveFedora::Base
is_a_container class_name: 'Thing'
end
120 121 122 123 124 |
# File 'lib/active_fedora/associations.rb', line 120 def is_a_container( = {}) defaults = { class_name: 'ActiveFedora::File', predicate: ::RDF::Vocab::LDP.contains } Builder::BasicContains.build(self, :contains, defaults.merge()) end |
#ordered_aggregation(name, options = {}) ⇒ Object
Convenience method for building an ordered aggregation.
327 328 329 |
# File 'lib/active_fedora/associations.rb', line 327 def ordered_aggregation(name, = {}) Builder::Aggregation.build(self, name, ) end |