class Book < ActiveFedora::Base
  belongs_to :library
  has_many :pages
  has_and_belongs_to_many :authors
end


# lib/active_fedora/associations.rb

def belongs_to(name, options = {})
  raise "You must specify a property name for #{name}" if !options[:property]
  Builder::BelongsTo.build(self, name, options)
end

Builder creates a Reflection


Book.create_reflection(:has_and_belongs_to_many, 'pages', { property: 'hasPages'}, Book)

# Adds a reflection:
Book.reflections
#=> { 'pages' => <#AssociationReflection
#           @options={ :property => :has_pages }
#           @macro=:has_and_belongs_to_many ...> }

Builder creates the accessors


def "#{name}_id"
  association(name).id_reader
end

def "#{name}_id="(id)
  association(name).id_writer(id)
end

Builder creates the accessors


def "#{name}"
  association(name).reader
end

def "#{name}="(value)
  association(name).writer(value)
end

The association method


def association(name)
  reflect = self.class.reflect_on_association(name)
  reflect.association_class.new(self, reflect)
end

The SingularAssociation

The CollectionAssociation

class Book < ActiveFedora::Base
 belongs_to :library, property: :has_constituent
end

class Library < ActiveFedora::Base
  has_many :books
end

CollectionProxy


library = Library.new
library.books.build
library.books.build
library.books.class
=> ActiveFedora::Associations::CollectionProxy
library.books

=> [#<Book pid: nil, library_id: nil>, #<Book pid: nil, library_id: nil>]

Fin

/