Class: ROM::SQL::Schema::AssociationsDSL

Inherits:
BasicObject
Defined in:
lib/rom/sql/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.



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

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

Instance Attribute Details

#registryObject (readonly)



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

def registry
  @registry
end

#sourceObject (readonly)



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

def source
  @source
end

Instance Method Details

#belongs_to(name, 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:



144
145
146
# File 'lib/rom/sql/schema/associations_dsl.rb', line 144

def belongs_to(name, options = {})
  many_to_one(dataset_name(name), {as: name}.merge(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:

  • (AssociationSet)


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

def call
  AssociationSet.new(registry)
end

#has_one(name, options = {}) ⇒ Associations::ManyToOne

Shortcut for one_to_one which sets alias automatically

Examples:

with an alias (relation identifier is inferred via pluralization)

one_to_one :address

with an explicit alias and a custom view

one_to_one :posts, as: :priority_post, view: :prioritized

Returns:

See Also:



161
162
163
# File 'lib/rom/sql/schema/associations_dsl.rb', line 161

def has_one(name, options = {})
  one_to_one(dataset_name(name), {as: name}.merge(options))
end

#many_to_many(target, options = {}) ⇒ Associations::OneToOne

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) (defaults to: {})

    A hash with additional options

Returns:

See Also:



110
111
112
# File 'lib/rom/sql/schema/associations_dsl.rb', line 110

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

#many_to_one(target, options = {}) ⇒ Associations::OneToOne

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) (defaults to: {})

    A hash with additional options

Returns:

See Also:



127
128
129
# File 'lib/rom/sql/schema/associations_dsl.rb', line 127

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

with a :through option

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

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

Parameters:

  • target (Symbol)

    The target relation identifier

  • options (Hash) (defaults to: {})

    A hash with additional options

Returns:

See Also:



52
53
54
55
56
57
58
# File 'lib/rom/sql/schema/associations_dsl.rb', line 52

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) (defaults to: {})

    A hash with additional options

Returns:

See Also:



77
78
79
80
81
82
83
# File 'lib/rom/sql/schema/associations_dsl.rb', line 77

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:



93
94
95
# File 'lib/rom/sql/schema/associations_dsl.rb', line 93

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