Class: ROM::Schema::AssociationsDSL

Inherits:
BasicObject
Includes:
Associations::Definitions
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, inflector = Inflector, &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.



34
35
36
37
38
39
# File 'lib/rom/schema/associations_dsl.rb', line 34

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

Instance Attribute Details

#inflectorObject (readonly)



27
28
29
# File 'lib/rom/schema/associations_dsl.rb', line 27

def inflector
  @inflector
end

#registryObject (readonly)



31
32
33
# File 'lib/rom/schema/associations_dsl.rb', line 31

def registry
  @registry
end

#sourceObject (readonly)



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

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:



165
166
167
# File 'lib/rom/schema/associations_dsl.rb', line 165

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

#callArray<Association::Definition]

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:

  • (Array<Association::Definition])

    Array<Association::Definition]



191
192
193
# File 'lib/rom/schema/associations_dsl.rb', line 191

def call
  registry.values
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:



182
183
184
# File 'lib/rom/schema/associations_dsl.rb', line 182

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:



131
132
133
# File 'lib/rom/schema/associations_dsl.rb', line 131

def many_to_many(target, **options)
  add(build(ManyToMany, 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:



148
149
150
# File 'lib/rom/schema/associations_dsl.rb', line 148

def many_to_one(target, **options)
  add(build(ManyToOne, 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:



73
74
75
76
77
78
79
# File 'lib/rom/schema/associations_dsl.rb', line 73

def one_to_many(target, **options)
  if options[:through]
    many_to_many(target, **options, inflector: inflector)
  else
    add(build(OneToMany, 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:



98
99
100
101
102
103
104
# File 'lib/rom/schema/associations_dsl.rb', line 98

def one_to_one(target, **options)
  if options[:through]
    one_to_one_through(target, **options)
  else
    add(build(OneToOne, 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:



114
115
116
# File 'lib/rom/schema/associations_dsl.rb', line 114

def one_to_one_through(target, **options)
  add(build(OneToOneThrough, target, options))
end