Module: Scrivito::Associations::ClassMethods

Included in:
BasicObj
Defined in:
app/cms/scrivito/associations.rb

Instance Method Summary collapse

Instance Method Details

#belongs_to(name, obj_class: name)

This method returns an undefined value.

Specifies a one-to-one association with another class.

This method defines a foreign attribute for this class. If the other class contains the foreign attribute, then you should use has_many instead.

This method defines a reference attribute with the given name. Only instances of obj_class are valid values for this attribute.

Examples:

class Author < Obj
  attribute :first_name, :string
  attribute :last_name, :string
end

class Book < Obj
  attribute :title, :string
  belongs_to :author
end

s_hawking = Author.create(first_name: 'Stephen', last_name: 'Hawking')
history_of_time = Book.create(title: 'A Brief History of Time', author: s_hawking)

history_of_time.author.first_name
# => 'Stephen'

with an optional obj_class

class Book < Obj
  belongs_to 'publisher', obj_class: Person
end

Parameters:

  • name (Symbol, String)

    name of the attribute. See attribute for details.

  • obj_class (Class) (defaults to: name)

    (optional) specifies (for use in the UI) the valid class of the value in this attribute. If no obj_class is given, an obj_class is derived from the given name. For example, from the name ‘author’ the obj_class Author is derived.



60
61
62
63
# File 'app/cms/scrivito/associations.rb', line 60

def belongs_to(name, obj_class: name)
  klass = to_single_class(obj_class)
  attribute(name, :reference, only: klass)
end

#belongs_to_many(name, obj_class: name)

This method returns an undefined value.

Specifies a one-to-many association.

This method defines a foreign attribute for this class. If the other class contains the foreign attribute, then you should use has_many instead.

This method defines a referencelist attribute with the given name. Only instances of obj_class are valid values for this attribute.

Examples:

class Author < Obj
  attribute :first_name, :string
  attribute :last_name, :string
end

class Book < Obj
  attribute :title, :string
  belongs_to_many :authors
end

s_hawking = Author.create(first_name: 'Stephen' last_name: 'Hawking')
l_hawking = Author.create(first_name: 'Lucy', last_name: 'Hawking')
secret_key_to_the_universe = Book.create(
    title: "George's Secret Key to the Universe", authors: [s_hawking, l_hawking])

secret_key_to_the_universe.authors.map(&:first_name)
# => ['Stephen', 'Lucy']

with an optional obj_class

class Book < Obj
  belongs_to_many 'reviewers', obj_class: Person
end

Parameters:

  • name (Symbol, String)

    name of the attribute. See attribute for details.

  • obj_class (Class) (defaults to: name)

    (optional) specifies (for use in the UI) the valid class of the value in this attribute. If no obj_class is given, an obj_class is derived from the given name. For example, from the name ‘authors’ the obj_class Author is derived.



106
107
108
109
# File 'app/cms/scrivito/associations.rb', line 106

def belongs_to_many(name, obj_class: name)
  klass = to_single_class(obj_class)
  attribute(name, :referencelist, only: klass)
end

#has_many(name, obj_class: name, foreign_attribute: nil)

This method returns an undefined value.

Specifies a one-to-many or many-to-many association with another class.

This method should only be used if the other class contains the foreign attribute. If the current class contains the foreign attribute, then you should use belongs_to or belongs_to_many instead.

This methods generates a new instance method <name>. This newly generated method searches for all obj_class instances, where the foreign_attribute references this instance. This newly generated method returns an Enumerable of BasicObjs instances.

Examples:

class Book < Obj
  attribute :title, :string
  belongs_to :author
end

class Author < Obj
  attribute :first_name, :string
  attribute :last_name, :string
  has_many :books
end

s_hawking = Author.create(first_name: 'Stephen', last_name: 'Hawking')
history_of_time = Book.create(title: 'A Brief History of Time', author: s_hawking)
grand_design = Book.create(title: 'The Grand Design', author: s_hawking)

s_hawking.books.to_a
# => [<Book id='abc'>, <Book id='123'>]

s_hawking.books.map(&:title)
# => ['A Brief History of Time', 'The Grand Design']

with an optional obj_class and foreign_attribute

class Book < Obj
  belongs_to_many 'reviewers', obj_class: Person
end

class Person < Obj
  has_many :reviewed_books, foreign_attribute: 'reviewers', obj_class: Book
end

a_person = Person.all.first
# => <Person id='456'>

a_person.reviewed_books.to_a
# => [<Book id='abc'>, <Book id='123'>]

Parameters:

  • name (Symbol, String)

    name of the search method.

  • obj_class (Class) (defaults to: name)

    (optional) limit search to instances of obj_class. If no obj_class is given, the obj_class is derived from the given name. For example, from the name ‘books’ the obj_class Book is derived.

  • foreign_attribute (Symbol, String) (defaults to: nil)

    (optional) name of the foreign attribute of the other class (e.g. as defined by belongs_to or belongs_to_many). If no foreign_attribute is given, the foreign attribute is derived from this class. For example, from the class Author the foreign_attribute [:author, :authors] is derived.



175
176
177
178
179
180
181
182
# File 'app/cms/scrivito/associations.rb', line 175

def has_many(name, obj_class: name, foreign_attribute: nil)
  klass = to_single_class(obj_class)
  attributes = to_attribute_names(foreign_attribute)

  define_method(name) do
    klass.where(attributes, :refers_to, self)
  end
end