Class: Reform::Form

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, PropertyMethods
Includes:
ActiveModel::Validations, MultiParameterAttributes, 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,
lib/reform/form/multi_parameter_attributes.rb

Defined Under Namespace

Modules: ActiveModel, ActiveRecord, Coercion, Composition, DSL, EmptyAttributesOptions, MultiParameterAttributes, 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 MultiParameterAttributes

#validate

Methods included from ValidateMethods

#validate

Constructor Details

#initialize(model) ⇒ Form

Returns a new instance of Form.



61
62
63
64
# File 'lib/reform/form.rb', line 61

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.



119
120
121
# File 'lib/reform/form.rb', line 119

def model
  @model
end

Instance Method Details

#errorsObject



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

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

#from_hash(params, *args) ⇒ Object



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

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

#saveObject



92
93
94
95
96
97
# File 'lib/reform/form.rb', line 92

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.



100
101
102
# File 'lib/reform/form.rb', line 100

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

#to_nested_hashObject



105
106
107
108
109
# File 'lib/reform/form.rb', line 105

def to_nested_hash
  map = mapper.new(self)

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