Class: Reform::Form

Inherits:
SimpleDelegator
  • Object
show all
Includes:
ActiveModel::Validations
Defined in:
lib/reform/form.rb,
lib/reform/form/dsl.rb,
lib/reform/form/active_record.rb

Defined Under Namespace

Modules: ActiveModel, ActiveRecord, DSL

Instance Method Summary collapse

Constructor Details

#initialize(mapper, composition) ⇒ Form

reasons for delegation: presentation: this object is used in the presentation layer by #form_for. problem: #form_for uses respond_to?(:email_before_type_cast) which goes to an internal hash in the actual record. validation: this object also contains the validation rules itself, should be separated. TODO: figure out #to_key issues.



11
12
13
14
15
16
17
# File 'lib/reform/form.rb', line 11

def initialize(mapper, composition)
  @mapper     = mapper
  @model      = composition
  representer = @mapper.new(composition)

  super Fields.new(representer.fields, representer.to_hash)  # decorate composition and transform to hash.
end

Instance Method Details

#saveObject



26
27
28
29
30
31
# File 'lib/reform/form.rb', line 26

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?

  @mapper.new(model).from_hash(to_hash) # DISCUSS: move to Composition?
end

#validate(params) ⇒ Object



19
20
21
22
23
24
# File 'lib/reform/form.rb', line 19

def validate(params)
  # here it would be cool to have a validator object containing the validation rules representer-like and then pass it the formed model.
  update_with(params)

  valid?  # this validates on <Fields> using AM::Validations, currently.
end