Sha256: b5f13e72aee987af7aad205674088eb505d508c2a234e914fa56cc9a7b47e21b
Contents?: true
Size: 1.6 KB
Versions: 1
Compression:
Stored size: 1.6 KB
Contents
# #sync! # 1. assign scalars to model (respecting virtual, excluded attributes) # 2. call sync! on nested module Reform::Form::Sync # Mechanics for writing input to model. # Writes input to model. module Writer def from_hash(*) # process output from InputRepresenter {title: "Mint Car", hit: <Form>} # and just call sync! on nested forms. nested_forms do |attr| attr.merge!( :instance => lambda { |fragment, *| fragment }, :deserialize => lambda { |object, *| object.sync! }, :setter => lambda { |*| } # don't write hit=<Form>. ) end super end end # Transforms form input into what actually gets written to model. # output: {title: "Mint Car", hit: <Form>} module InputRepresenter include Reform::Representer::WithOptions # TODO: make dynamic. include Reform::Form::EmptyAttributesOptions include Reform::Form::ReadonlyAttributesOptions def to_hash(*) nested_forms do |attr| attr.merge!( :representable => false, :prepare => lambda { |obj, *| obj } ) end super end end ### TODO: add ToHash with :prepare => lambda { |form, args| form }, def sync_models sync! end alias_method :sync, :sync_models # reading from fields allows using readers in form for presentation # and writers still pass to fields in #validate???? def sync! # semi-public. input_representer = mapper.new(fields).extend(InputRepresenter) input = input_representer.to_hash mapper.new(aliased_model).extend(Writer).from_hash(input) end end
Version data entries
1 entries across 1 versions & 1 rubygems
Version | Path |
---|---|
reform-1.0.4 | lib/reform/form/sync.rb |