Class: Cave::ModelForm

Inherits:
Form
  • Object
show all
Defined in:
lib/cave/model_form.rb

Class Method Summary collapse

Instance Method Summary collapse

Methods inherited from Form

#bind, bind, #bound?, field, fields, #save!, #unbound?

Constructor Details

#initialize(instance = nil, attrs = {}) ⇒ ModelForm

Returns a new instance of ModelForm.


7
8
9
10
11
# File 'lib/cave/model_form.rb', line 7

def initialize instance=nil, attrs={}
  (instance, attrs = nil, instance) if instance.is_a? Hash
  super attrs
  self.for instance
end

Class Method Details

.model(klass = nil) ⇒ Object


3
4
5
# File 'lib/cave/model_form.rb', line 3

def self.model klass=nil
  @@model ||= klass
end

Instance Method Details

#for(instance) ⇒ Object


13
14
15
16
# File 'lib/cave/model_form.rb', line 13

def for instance
  @instance = instance
  check_instance_model
end

#persist!Object


18
19
20
21
22
23
24
# File 'lib/cave/model_form.rb', line 18

def persist!
  if @instance
    @instance.update_attributes attributes
  else
    @instance = self.class.model.create! attributes
  end
end