Module: ROM::SQL::Plugin::AssocMacros

Defined in:
lib/rom/sql/plugin/assoc_macros.rb,
lib/rom/sql/plugin/assoc_macros/class_interface.rb

Defined Under Namespace

Modules: ClassInterface

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(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.

Extends a relation class with assoc-macros and instance-level methods



10
11
12
13
# File 'lib/rom/sql/plugin/assoc_macros.rb', line 10

def self.included(relation)
  super
  relation.extend(ClassInterface)
end

Instance Method Details

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

Join configured association.

Uses INNER JOIN type.

Examples:


setup.relation(:tasks)

setup.relations(:users) do
  one_to_many :tasks, key: :user_id

  def with_tasks
    association_join(:tasks, select: [:title])
  end
end


37
38
39
# File 'lib/rom/sql/plugin/assoc_macros.rb', line 37

def association_join(name, options = {})
  graph_join(name, :inner, options)
end

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

Join configured association

Uses LEFT JOIN type.

Examples:


setup.relation(:tasks)

setup.relations(:users) do
  one_to_many :tasks, key: :user_id

  def with_tasks
    association_left_join(:tasks, select: [:title])
  end
end


58
59
60
# File 'lib/rom/sql/plugin/assoc_macros.rb', line 58

def association_left_join(name, options = {})
  graph_join(name, :left_outer, options)
end

#graph(*args) ⇒ 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.



89
90
91
# File 'lib/rom/sql/plugin/assoc_macros.rb', line 89

def graph(*args)
  __new__(dataset.__send__(__method__, *args))
end

#graph_join(assoc_name, join_type, options = {}) ⇒ 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.



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/rom/sql/plugin/assoc_macros.rb', line 63

def graph_join(assoc_name, join_type, options = {})
  assoc = model.association_reflection(assoc_name)

  if assoc.nil?
    raise NoAssociationError,
      "Association #{assoc_name.inspect} has not been " \
      "defined for relation #{name.relation.inspect}"
  end

  type = assoc[:type]
  table_name = assoc[:class].table_name

  graph_rel =
    if type == :many_to_many
      select = options[:select] || {}
      graph_join_many_to_many(table_name, assoc, select)
    else
      graph_join_other(table_name, assoc, type, join_type, options)
    end

  graph_rel = graph_rel.where(assoc[:conditions]) if assoc[:conditions]

  graph_rel
end

#modelObject

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.



16
17
18
# File 'lib/rom/sql/plugin/assoc_macros.rb', line 16

def model
  self.class.model
end