Class: Reform::Form

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, PropertyMethods
Includes:
ActiveModel::Validations, ValidateMethods
Defined in:
lib/reform/form.rb,
lib/reform/form/coercion.rb,
lib/reform/form/composition.rb,
lib/reform/form/active_record.rb,
lib/reform/form/virtual_attributes.rb

Defined Under Namespace

Modules: ActiveModel, ActiveRecord, Coercion, Composition, DSL, EmptyAttributesOptions, PropertyMethods, ReadonlyAttributesOptions, Setup, Sync, ValidateMethods Classes: Errors, Forms

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from PropertyMethods

collection, properties, property, representer_class, setup_form_definition

Methods included from ValidateMethods

#validate

Constructor Details

#initialize(model) ⇒ Form

Returns a new instance of Form.



59
60
61
62
# File 'lib/reform/form.rb', line 59

def initialize(model)
  @model  = model # we need this for #save.
  @fields = setup_fields(model)  # delegate all methods to Fields instance.
end

Instance Attribute Details

#modelObject

Returns the value of attribute model.



116
117
118
# File 'lib/reform/form.rb', line 116

def model
  @model
end

Instance Method Details

#errorsObject



112
113
114
# File 'lib/reform/form.rb', line 112

def errors
  @errors ||= Errors.new(self)
end

#from_hash(params, *args) ⇒ Object



108
109
110
# File 'lib/reform/form.rb', line 108

def from_hash(params, *args)
  mapper.new(self).from_hash(params) # sets form properties found in params on self.
end

#saveObject



89
90
91
92
93
94
# File 'lib/reform/form.rb', line 89

def save
  # DISCUSS: we should never hit @mapper here (which writes to the models) when a block is passed.
  return yield self, to_nested_hash if block_given?

  save_to_models
end

#to_hash(*args) ⇒ Object

Use representer to return current key-value form hash.



97
98
99
# File 'lib/reform/form.rb', line 97

def to_hash(*args)
  mapper.new(self).to_hash(*args)
end

#to_nested_hashObject



102
103
104
105
106
# File 'lib/reform/form.rb', line 102

def to_nested_hash
  map = mapper.new(self)

  ActiveSupport::HashWithIndifferentAccess.new(map.to_hash)
end