lib/reform/form/validate.rb in reform-1.1.1 vs lib/reform/form/validate.rb in reform-1.2.0.beta1
- old
+ new
@@ -24,12 +24,22 @@
end
# FIXME: solve this with a dedicated Populate Decorator per Form.
representable_attrs.each do |attr|
attr.merge!(:parse_filter => Representable::Coercion::Coercer.new(attr[:coercion_type])) if attr[:coercion_type]
+
+ attr.merge!(:skip_if => Skip::AllBlank.new) if attr[:skip_if] == :all_blank
+ attr.merge!(:skip_parse => attr[:skip_if]) if attr[:skip_if]
end
+
+ representable_attrs.each do |attr|
+ next if attr[:form]
+
+ attr.merge!(:parse_filter => Changed.new)
+ end
+
super
end
end
@@ -69,9 +79,37 @@
else
fields.send("#{binding.setter}", form) # :setter is currently overwritten by :parse_strategy.
end
end
end # PopulateIfEmpty
+ end
+
+
+ module Skip
+ class AllBlank
+ include Uber::Callable
+
+ def call(form, params, options)
+ # TODO: hahahahahaha.
+ properties = options.binding.representer_module.representer_class.representable_attrs[:definitions].keys
+
+ properties.each { |name| params[name].present? and return false }
+ true # skip
+ end
+ end
+ end
+
+
+ class Changed
+ def call(fragment, params, options)
+ # options is a Representable::Options object holding all the stakeholders. this is here becaues of pass_options: true.
+ form = options.represented
+ name = options.binding.name
+
+ form.changed[name] = form.send(name) != fragment
+
+ fragment
+ end
end
# 1. Populate the form object graph so that each incoming object has a representative form object.
# 2. Deserialize. This is wrong and should be done in 1.
# 3. Validate the form object graph.