Module: ROM::SQL::Relation::ClassMethods Private

Included in:
ROM::SQL::Relation
Defined in:
lib/rom/sql/relation/class_methods.rb

Overview

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

Class DSL for SQL relations

Instance Method Summary collapse

Instance Method Details

#finalize(relations, relation) ⇒ Object

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.

Finalize the relation by setting up its associations (if any)



99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/rom/sql/relation/class_methods.rb', line 99

def finalize(relations, relation)
  return unless relation.dataset.db.table_exists?(dataset)

  model.set_dataset(relation.dataset)
  model.dataset.naked!

  associations.each do |*args, assoc_opts|
    options = Hash[assoc_opts]
    other = relations[options.delete(:relation) || args[1]].model
    model.public_send(*args, options.merge(class: other))
  end

  model.freeze

  super
end

#inherited(klass) ⇒ Object

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.

Set up model and association ivars for descendant class



11
12
13
14
15
16
17
18
19
20
# File 'lib/rom/sql/relation/class_methods.rb', line 11

def inherited(klass)
  klass.class_eval do
    class << self
      attr_reader :model, :associations
    end
  end
  klass.instance_variable_set('@model', Class.new(Sequel::Model))
  klass.instance_variable_set('@associations', [])
  super
end

#many_to_many(name, options = {}) ⇒ Object

Set up a many-to-many association

Examples:

class Tasks < ROM::Relation[:sql]
  many_to_many :tags,
    join_table: :task_tags,
    left_key: :task_id,
    right_key: :tag_id,

  def with_tags
    association_join(:tags)
  end
end

Parameters:

  • name (Symbol)

    The name of the association

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

    The options hash

Options Hash (options):

  • :join_table (Symbol)

    Name of the join table

  • :left_key (Hash)

    Name of the left join key

  • :right_key (Hash)

    Name of the right join key

  • :on (Hash)

    Additional conditions for join

  • :conditions (Hash)

    Additional conditions for WHERE



67
68
69
# File 'lib/rom/sql/relation/class_methods.rb', line 67

def many_to_many(name, options = {})
  associations << [__method__, name, { relation: name }.merge(options)]
end

#many_to_one(name, options = {}) ⇒ Object

Set up a many-to-one association

Examples:

class Tasks < ROM::Relation[:sql]
  many_to_one :users, key: :user_id

  def with_users
    association_join(:users)
  end
end

Parameters:

  • name (Symbol)

    The name of the association

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

    The options hash

Options Hash (options):

  • :join_table (Symbol)

    Name of the join table

  • :key (Hash)

    Name of the join key

  • :on (Hash)

    Additional conditions for join

  • :conditions (Hash)

    Additional conditions for WHERE



90
91
92
93
94
# File 'lib/rom/sql/relation/class_methods.rb', line 90

def many_to_one(name, options = {})
  relation_name = Inflector.pluralize(name).to_sym
  new_options = options.merge(relation: relation_name)
  associations << [__method__, name, new_options]
end

#one_to_many(name, options) ⇒ Object

Set up a one-to-many association

Examples:

class Users < ROM::Relation[:sql]
  one_to_many :tasks, key: :user_id

  def with_tasks
    association_join(:tasks)
  end
end

Parameters:

  • name (Symbol)

    The name of the association

  • options (Hash)

    The options hash

Options Hash (options):

  • :key (Symbol)

    Name of the key to join on

  • :on (Hash)

    Additional conditions for join

  • :conditions (Hash)

    Additional conditions for WHERE



40
41
42
# File 'lib/rom/sql/relation/class_methods.rb', line 40

def one_to_many(name, options)
  associations << [__method__, name, { relation: name }.merge(options)]
end