# File app/models/smerf_item.rb, line 158 def validate_sub_objects(object_class, hash, sort_order_field, object_array) errors = "" array_of_objects = Array.new # Process each item passed to us in the hash for item in hash do # Create a new onject using the class name passed to us, pass # in the item contents and the sort order field to the new object class_object = object_class.new(item, sort_order_field, @object_ident) # Perform the validation on the new object, checking manadatory fields # are present and performing any custom validations as defined by the # fields array setup for each class errors += class_object.validate() # Add the new object to the object array that will be used by the calling # object, e.g. smerffile object collects and uses groups array_of_objects << class_object # If the object has a 'code' field move it to the code array to make # sure all codes are unique either for a complete form or within the # current object, e.g. answers for a question if class_object.respond_to?('code') @class_code_array << class_object.code @code_array << class_object.code end end # Check all items have a unique code errors += check_duplicate_codes(object_class) # Sort the items using the supplied sort order field if errors.size <= 0 and !sort_order_field.blank? array_of_objects = array_of_objects.sort {|a,b| eval("a.#{sort_order_field}<=>b.#{sort_order_field}")} # The above function is faster than the one below #object_array = object_array.sort_by {|item| eval("item.#{sort_order_field}")} end # Copy all items to the destination array. #array_of_objects.each {|item| object_array[item.code.to_s] = item} object_array.replace(array_of_objects) return errors end