Class: Moogle::Commands::CreateTarget

Inherits:
Object
  • Object
show all
Includes:
Serf::Command
Defined in:
lib/moogle/commands/create_target.rb

Instance Method Summary collapse

Instance Method Details

#callMoogle::Target

Returns the created target, properly subclassed by type.

Returns:

  • (Moogle::Target)

    the created target, properly subclassed by type.



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
# File 'lib/moogle/commands/create_target.rb', line 21

def call
  event_class = opts :event_class, Moogle::Events::TargetCreated
  representer = opts :representer, Moogle::TargetRepresenter

  # Determine our model class name, and get the constant.
  model_name = "moogle/#{request.type}_target".classify
  target_model = model_name.constantize rescue Moogle::Target

  # Now, create the target, and raise on errors.
  target = target_model.create(
    owner_ref:  request.owner_ref,
    options: request.options)
  raise target.errors.full_messages.join('. ') unless target.saved?

  target_rep = target.dup.extend representer
  return event_class.new(
    request.create_child_uuids.merge(target: target_rep))
rescue => e
  e.extend Moogle::Error
  raise e
end