Module: ROM::SQL::Plugin::Associates::ClassMethods

Defined in:
lib/rom/sql/plugin/associates.rb

Instance Method Summary collapse

Instance Method Details

#associates(name, options = EMPTY_HASH) ⇒ Object

Set command to associate tuples with a parent tuple using provided keys

Examples:

class CreateTask < ROM::Commands::Create[:sql]
  relation :tasks
  associates :user, key: [:user_id, :id]
end

create_user = rom.command(:user).create.curry(name: 'Jane')

create_tasks = rom.command(:tasks).create
  .curry [{ title: 'One' }, { title: 'Two' } ]

command = create_user >> create_tasks
command.call

Parameters:

  • name (Symbol)

    The name of associated table

  • options (Hash) (defaults to: EMPTY_HASH)

    The options

Options Hash (options):

  • :key (Array)

    The association keys



126
127
128
129
130
131
132
133
134
135
# File 'lib/rom/sql/plugin/associates.rb', line 126

def associates(name, options = EMPTY_HASH)
  if associations.key?(name)
    raise ArgumentError,
          "#{name} association is already defined for #{self.class}"
  end

  associations[name] = options

  self
end

#build(relation, **options) ⇒ Object

See Also:

  • Command::ClassInterface.build


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
# File 'lib/rom/sql/plugin/associates.rb', line 85

def build(relation, **options)
  command = super

  configured_assocs = command.configured_associations

  associate_options = command.associations.map { |(name, opts)|
    next if configured_assocs.include?(name)

    AssociateOptions.new(name, relation, opts)
  }.compact

  before_hooks = associate_options.reject(&:after?).map(&:to_hash)
  after_hooks = associate_options.select(&:after?).map(&:to_hash)

  command
    .with(configured_associations: configured_assocs + associate_options.map(&:name))
    .before(*before_hooks)
    .after(*after_hooks)
end

#create_class(relation:, rel_meta: {}, parent_relation: nil, &block) ⇒ 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.



50
51
52
53
54
55
56
57
58
# File 'lib/rom/sql/plugin/associates.rb', line 50

def create_class(relation:, rel_meta: {}, parent_relation: nil, **, &block)
  klass = super

  if relation && rel_meta[:combine_type]
    setup_associates(klass, relation, parent_relation)
  end

  klass
end

#setup_associates(klass, relation, parent_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.

Sets up associates plugin for a given command class and relation

Parameters:

  • klass (Class)

    The command class

  • relation (Relation)

    The relation for the command



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rom/sql/plugin/associates.rb', line 66

def setup_associates(klass, relation, parent_relation)
  assoc_name =
    if relation.associations.key?(parent_relation)
      parent_relation
    else
      singular_name = relation.inflector.singularize(parent_relation).to_sym
      singular_name if relation.associations.key?(singular_name)
    end

  if assoc_name
    klass.associates(assoc_name)
  else
    klass.associates(parent_relation)
  end
end