lib/reform/form/validate.rb in reform-1.0.1 vs lib/reform/form/validate.rb in reform-1.0.2

- old
+ new

@@ -19,43 +19,46 @@ module Populator class PopulateIfEmpty def initialize(*args) - @form, @fragment, args = args + @fields, @fragment, args = args @index = args.first @args = args.last end def call binding = @args.binding form = binding.get + parent_form = @args.user_options[:parent_form] + form_model = parent_form.model # FIXME: sort out who's responsible for sync. + return if binding.array? and form and form[@index] # TODO: this should be handled by the Binding. return if !binding.array? and form # only get here when above form is nil. if binding[:populate_if_empty].is_a?(Proc) - model = @form.instance_exec(@fragment, @args, &binding[:populate_if_empty]) # call user block. + model = parent_form.instance_exec(@fragment, @args, &binding[:populate_if_empty]) # call user block. else model = binding[:populate_if_empty].new end form = binding[:form].new(model) # free service: wrap model with Form. this usually happens in #setup. if binding.array? - @form.model.send("#{binding.getter}") << model # FIXME: i don't like this, but we have to add the model to the parent object to make associating work. i have to use #<< to stay compatible with AR's has_many API. DISCUSS: what happens when we get out-of-sync here? - @form.send("#{binding.getter}")[@index] = form + form_model.send("#{binding.getter}") << model # FIXME: i don't like this, but we have to add the model to the parent object to make associating work. i have to use #<< to stay compatible with AR's has_many API. DISCUSS: what happens when we get out-of-sync here? + @fields.send("#{binding.getter}")[@index] = form else - @form.model.send("#{binding.setter}", model) # FIXME: i don't like this, but we have to add the model to the parent object to make associating work. - @form.send("#{binding.setter}", form) # :setter is currently overwritten by :parse_strategy. + form_model.send("#{binding.setter}", model) # FIXME: i don't like this, but we have to add the model to the parent object to make associating work. + @fields.send("#{binding.setter}", form) # :setter is currently overwritten by :parse_strategy. end end end # PopulateIfEmpty - def from_hash(params, *args) + def from_hash(params, args) populated_attrs = [] nested_forms do |attr| next unless attr[:populate_if_empty] @@ -76,11 +79,11 @@ :representable => false ) populated_attrs << attr.name.to_sym end - super(params, {:include => populated_attrs}) + super(params, {:include => populated_attrs}.merge(args)) end end def validate(params) @@ -88,17 +91,22 @@ super() end def update!(params) - # puts "updating in #{self.class.name}" populate!(params) - - mapper.new(self).extend(Update).from_hash(params) + deserialize!(params) end private def populate!(params) - mapper.new(self).extend(Populator).from_hash(params) + target = deprecate_potential_writers_used_in_validate(fields) # TODO: remove in 1.1. + + mapper.new(target).extend(Populator).from_hash(params, :parent_form => self) # TODO: remove model(form) once we found out how to synchronize the model correctly. see https://github.com/apotonick/reform/issues/86#issuecomment-43402047 end + def deserialize!(params) + target = deprecate_potential_writers_used_in_validate(fields) # TODO: remove in 1.1. + + mapper.new(target).extend(Update).from_hash(params) + end end