Class: Handlers::Delegate

Inherits:
Base
  • Object
show all
Defined in:
lib/handlers/delegates.rb

Instance Method Summary collapse

Methods inherited from Base

handle, #raise_error, #upper

Constructor Details

#initialize(klass, delegate) ⇒ Delegate

Returns a new instance of Delegate.



15
16
17
18
19
20
21
22
23
24
# File 'lib/handlers/delegates.rb', line 15

def initialize(klass, delegate)
  @delegate = delegate

  @form = @delegate[:form]
  @foreign_key = @delegate[:foreign_key]
  @relation = @delegate[:relation]
  @attributes = @delegate[:attributes]

  super(klass)
end

Instance Method Details

#handleObject



26
27
28
29
30
31
32
33
# File 'lib/handlers/delegates.rb', line 26

def handle
  @attributes = @raw_params.slice(*@attributes.map(&:to_s)).merge({
    @foreign_key => @resource.id
  })

  @form.new(@attributes, @resource.send(@relation)).save!
  @resource.reload
end