Class: ROM::Schema::AssociationsDSL

Inherits:
BasicObject
Defined in:
lib/rom/schema/associations_dsl.rb

Overview

Additional schema DSL for definition SQL associations

This DSL is exposed in ‘associations do .. end` blocks in schema defintions.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(source, &block) ⇒ AssociationsDSL

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Returns a new instance of AssociationsDSL.



24
25
26
27
28
# File 'lib/rom/schema/associations_dsl.rb', line 24

def initialize(source, &block)
  @source = source
  @registry = {}
  instance_exec(&block)
end

Instance Attribute Details

#registryObject (readonly)



21
22
23
# File 'lib/rom/schema/associations_dsl.rb', line 21

def registry
  @registry
end

#sourceObject (readonly)



17
18
19
# File 'lib/rom/schema/associations_dsl.rb', line 17

def source
  @source
end

Instance Method Details

#belongs_to(target, **options) ⇒ Associations::ManyToOne

Shortcut for many_to_one which sets alias automatically

Examples:

with an alias (relation identifier is inferred via pluralization)

belongs_to :user

with an explicit alias

belongs_to :users, as: :author

Returns:

See Also:



154
155
156
# File 'lib/rom/schema/associations_dsl.rb', line 154

def belongs_to(target, **options)
  many_to_one(dataset_name(target), as: target, **options)
end

#callAssociationSet

This method is part of a private API. You should avoid using this method if possible, as it may be removed or be changed in the future.

Return an association set for a schema

Returns:



180
181
182
# File 'lib/rom/schema/associations_dsl.rb', line 180

def call
  AssociationSet[source.relation].build(registry)
end

#has_one(target, **options) ⇒ Associations::OneToOne

Shortcut for one_to_one which sets alias automatically

Examples:

with an alias (relation identifier is inferred via pluralization)

has_one :address

with an explicit alias and a custom view

has_one :posts, as: :priority_post, view: :prioritized

Returns:

See Also:



171
172
173
# File 'lib/rom/schema/associations_dsl.rb', line 171

def has_one(target, **options)
  one_to_one(dataset_name(target), as: target, **options)
end

#many_to_many(target, **options) ⇒ Associations::ManyToMany

Establish a many-to-many association

Examples:

using relation identifier

many_to_many :tasks, through: :users_tasks

Parameters:

  • target (Symbol)

    The target relation identifier

  • options (Hash)

    A hash with additional options

Returns:

See Also:



120
121
122
# File 'lib/rom/schema/associations_dsl.rb', line 120

def many_to_many(target, **options)
  add(::ROM::Associations::Definitions::ManyToMany.new(source, target, **options))
end

#many_to_one(target, **options) ⇒ Associations::ManyToOne

Establish a many-to-one association

Examples:

using relation identifier

many_to_one :users, as: :author

Parameters:

  • target (Symbol)

    The target relation identifier

  • options (Hash)

    A hash with additional options

Returns:

See Also:



137
138
139
# File 'lib/rom/schema/associations_dsl.rb', line 137

def many_to_one(target, **options)
  add(::ROM::Associations::Definitions::ManyToOne.new(source, target, **options))
end

#one_to_many(target, **options) ⇒ Associations::OneToMany Also known as: has_many

Establish a one-to-many association

Examples:

using relation identifier

has_many :tasks

setting custom foreign key name

has_many :tasks, foreign_key: :assignee_id

with a :through option

# this establishes many-to-many association
has_many :tasks, through: :users_tasks

using a custom view which overrides default one

has_many :posts, view: :published, override: true

using aliased association with a custom view

has_many :posts, as: :published_posts, view: :published

using custom target relation

has_many :user_posts, relation: :posts

using custom target relation and an alias

has_many :user_posts, relation: :posts, as: :published, view: :published

Parameters:

  • target (Symbol)

    The target relation identifier

  • options (Hash)

    A hash with additional options

Returns:

See Also:



62
63
64
65
66
67
68
# File 'lib/rom/schema/associations_dsl.rb', line 62

def one_to_many(target, **options)
  if options[:through]
    many_to_many(target, **options)
  else
    add(::ROM::Associations::Definitions::OneToMany.new(source, target, **options))
  end
end

#one_to_one(target, **options) ⇒ Associations::OneToOne

Establish a one-to-one association

Examples:

using relation identifier

one_to_one :addresses, as: :address

with an intermediate join relation

one_to_one :tasks, as: :priority_task, through: :assignments

Parameters:

  • target (Symbol)

    The target relation identifier

  • options (Hash)

    A hash with additional options

Returns:

See Also:



87
88
89
90
91
92
93
# File 'lib/rom/schema/associations_dsl.rb', line 87

def one_to_one(target, **options)
  if options[:through]
    one_to_one_through(target, **options)
  else
    add(::ROM::Associations::Definitions::OneToOne.new(source, target, **options))
  end
end

#one_to_one_through(target, **options) ⇒ Associations::OneToOneThrough

Establish a one-to-one association with a :through option

Examples:

one_to_one_through :users, as: :author, through: :users_posts

Returns:



103
104
105
# File 'lib/rom/schema/associations_dsl.rb', line 103

def one_to_one_through(target, **options)
  add(::ROM::Associations::Definitions::OneToOneThrough.new(source, target, **options))
end