Class: FreeForm::Form

Inherits:
Object
  • Object
show all
Extended by:
ActiveModel::Callbacks, ActiveModel::Naming, Forwardable
Includes:
ActiveModel::Conversion, ActiveModel::Validations, FormInputKey, Nested, Property, Validation
Defined in:
lib/freeform/form.rb

Instance Attribute Summary

Attributes included from Property

#parent_form

Instance Method Summary collapse

Methods included from Validation

included

Methods included from Property

#assign_params, included

Methods included from Nested

included

Methods included from FormInputKey

included

Constructor Details

#initialize(h = {}) ⇒ Form

Returns a new instance of Form.



27
28
29
30
# File 'lib/freeform/form.rb', line 27

def initialize(h={})
  h.each {|k,v| send("#{k}=",v)}
  initialize_child_models
end

Instance Method Details

#persisted?Boolean

Instance Methods


Required for ActiveModel

Returns:

  • (Boolean)


25
# File 'lib/freeform/form.rb', line 25

def persisted?; false end

#saveObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# File 'lib/freeform/form.rb', line 51

def save
  return false unless valid? || marked_for_destruction?

  # Loop through nested models first, sending them the save call
  self.class.models.each do |form_model|
    model = send(form_model)
    # This is for nested models.
    if model.is_a?(Array)
      model.each { |m| m.save }
    end
  end

  # After nested models are handled, save or destroy myself.
  self.class.models.each do |form_model|
    model = send(form_model)
    # This is for form models.
    unless model.is_a?(Array)
      if marked_for_destruction?
        model.destroy
      else
        model.save
      end
    end
  end
end

#save!Object



77
78
79
80
81
82
83
84
# File 'lib/freeform/form.rb', line 77

def save!
  raise FreeForm::FormInvalid, "form invalid." unless valid?

  self.class.models.each do |form_model|
    model = send(form_model)
    model.is_a?(Array) ? model.each { |m| m.save! } : save_or_destroy!(model)
  end
end