unepwcmc/SAPI — pull request #132 by agnessa (2013-12-05 13:06:02+00:00)
Title: Secondary validations for individual shipments

This adds secondary validations for individual shipment create / update.

In order to facilitate secondary validations of shipment records using the same rules as for sandbox records, the rules have been extended with a method validation_errors_for_shipment. For that to work, the underlying valid values views have been modified to include 'id' fields, i.e. where there was 'term_code' we now have both 'term_code' and 'term_id'.

The secondary validations are handled as part of the AR validations flow; there's a custom validator that iterates through all defined secondary validations and groups the resulting error messages under an attribute called 'warnings' (does not add those to the errors has just yet). There is now a virtual attribute that controls whether or not warnings should prevent the record from saving; it is called 'ignoreWarnings' and it is taken into consideration in the after_validation block, and if it is set to false, warnings are appended to the errors hash, thus preventing the save.

The frontend sends ignoreWarnings = false as default. The only time it should set it to true is when:
1. user attempts to save a record with no primary errors in, but secondary errors present
2. without modifying any prperties of the records clicks 'save changes' again to force save

NOTE: you need to run rake db:migrate to refresh the valid values views and also rebuild the mview which is used in taxon - appendix - year validations:

rails dbconsole
> SELECT * FROM rebuild_valid_species_name_appendix_year_mview();