Class: ROM::Changeset::Associated

Inherits:
Object
  • Object
show all
Defined in:
changeset/lib/rom/changeset/associated.rb

Overview

Associated changesets automatically set up FKs

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#associationsArray (readonly)

Returns List of association identifiers from relation schema.

Returns:

  • (Array)

    List of association identifiers from relation schema



19
# File 'changeset/lib/rom/changeset/associated.rb', line 19

option :associations

#leftChangeset::Create (readonly)

Returns Child changeset.

Returns:



15
# File 'changeset/lib/rom/changeset/associated.rb', line 15

param :left

Instance Method Details

#associate(other, name = Associated.infer_assoc_name(other)) ⇒ Associated

Associate with other changesets

Returns:

See Also:

  • Changeset#associate


65
66
67
# File 'changeset/lib/rom/changeset/associated.rb', line 65

def associate(other, name = Associated.infer_assoc_name(other))
  self.class.new(left, associations: associations.merge(name => other))
end

#commandROM::Command::Composite

Create a composed command

This works only with parent => child(ren) changeset hierarchy

Examples:

using existing parent data

user_changeset = users.changeset(name: 'Jane')
task_changeset = tasks.changeset(title: 'Task One')

user = users.create(user_changeset)
task = tasks.create(task_changeset.associate(user, :user))

saving both parent and child in one go

user_changeset = users.changeset(name: 'Jane')
task_changeset = tasks.changeset(title: 'Task One')

task = tasks.create(task_changeset.associate(user, :user))

Returns:



89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'changeset/lib/rom/changeset/associated.rb', line 89

def command
  associations.reduce(left.command.curry(left)) do |a, (assoc, other)|
    case other
    when Changeset
      a >> other.command.with_association(assoc).curry(other)
    when Associated
      a >> other.command.with_association(assoc)
    when Array
      raise NotImplementedError, 'Changeset::Associate does not support arrays yet'
    else
      a.with_association(assoc, parent: other)
    end
  end
end

#commitArray<Hash>, Hash

Commit changeset's composite command

Examples:

task_changeset = tasks.
  changeset(title: 'Task One').
  associate(user, :user).
  commit
# {:id => 1, :user_id => 1, title: 'Task One'}

Returns:

  • (Array<Hash>, Hash)


54
55
56
# File 'changeset/lib/rom/changeset/associated.rb', line 54

def commit
  command.call
end