Module: Granite::Form::Model::Persistence::ClassMethods

Defined in:
lib/granite/form/model/persistence.rb

Instance Method Summary collapse

Instance Method Details

#instantiate(data) ⇒ Object



8
9
10
11
12
13
14
15
16
# File 'lib/granite/form/model/persistence.rb', line 8

def instantiate(data)
  data = data.stringify_keys
  instance = allocate

  instance.instance_variable_set(:@initial_attributes, data.slice(*attribute_names))
  instance.send(:mark_persisted!)

  instance
end

#instantiate_collection(data) ⇒ Object



18
19
20
21
22
# File 'lib/granite/form/model/persistence.rb', line 18

def instantiate_collection(data)
  collection = Array.wrap(data).map { |attrs| instantiate attrs }
  collection = scope(collection, true) if respond_to?(:scope)
  collection
end