Module: HasDynamicColumns::Model::InstanceMethods

Defined in:
lib/has_dynamic_columns.rb

Instance Method Summary collapse

Instance Method Details

#validate_dynamic_column_dataObject

Validate all the dynamic_column_data at once



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
# File 'lib/has_dynamic_columns.rb', line 224

def validate_dynamic_column_data
	field_scope = self.field_scope

	if field_scope
		# All the fields defined on the parent model
		dynamic_columns = field_scope.send("activerecord_#{field_scope.dynamic_columns_as}")

		self.send("activerecord_#{self.dynamic_columns_as}_data").each { |dynamic_column_datum|
			# Collect all validation errors
			validation_errors = []

			if dynamic_column_datum.dynamic_column_option_id == -1
				validation_errors << "invalid_option"
			end

			# Find the dynamic_column defined for this datum
			dynamic_column = nil
			dynamic_columns.each { |i|
				if i == dynamic_column_datum.dynamic_column
					dynamic_column = i
					break
				end
			}
			# We have a dynamic_column - validate
			if dynamic_column
				dynamic_column.dynamic_column_validations.each { |validation|
					if !validation.is_valid?(dynamic_column_datum.value.to_s)
						validation_errors << validation.error
					end
				}
			else
				# No field found - this is probably bad - should we throw an error?
				validation_errors << "not_found"
			end

			# If any errors exist - add them
			if validation_errors.length > 0
				errors.add(:dynamic_columns, { "#{dynamic_column.key}" => validation_errors })
			end
		}
	end
end