Class: Reform::Form

Inherits:
Object
  • Object
show all
Extended by:
Forwardable, PropertyMethods, Uber::InheritableAttr
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, setup_form_definition

Methods included from MultiParameterAttributes

#validate

Methods included from ValidateMethods

#validate

Constructor Details

#initialize(model) ⇒ Form

Returns a new instance of Form.



66
67
68
69
# File 'lib/reform/form.rb', line 66

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.



126
127
128
# File 'lib/reform/form.rb', line 126

def model
  @model
end

Instance Method Details

#errorsObject



121
122
123
124
# File 'lib/reform/form.rb', line 121

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

#from_hash(params, *args) ⇒ Object



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

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

#saveObject



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

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.



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

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

#to_nested_hashObject



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

def to_nested_hash
  map = mapper.new(self)

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