Class: ChildValidationRule

Inherits:
ValidationRule show all
Defined in:
lib/validation_profiler/rules/rules.rb

Instance Method Summary collapse

Methods inherited from ValidationRule

#get_field_value, #is_required?

Constructor Details

#initializeChildValidationRule

Returns a new instance of ChildValidationRule.



585
586
587
# File 'lib/validation_profiler/rules/rules.rb', line 585

def initialize
  @validation_manager = ValidationManager.new
end

Instance Method Details

#error_message(field, attributes = {}, parent = nil) ⇒ Object



589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
# File 'lib/validation_profiler/rules/rules.rb', line 589

def error_message(field, attributes = {}, parent = nil)

  field_name = field.to_s
  if parent != nil
    field_name = "#{parent.to_s}.#{field.to_s}"
  end

  #check if a custom error message has been specified in the attributes
  if attributes[:message] == nil
    #no custom error message has been specified so create the default message.
    "#{field_name} is required."
  else
    attributes[:message]
  end
end

#validate(obj, field, attributes = {}, parent = nil) ⇒ Object



605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/validation_profiler/rules/rules.rb', line 605

def validate(obj, field, attributes = {}, parent = nil)

  #attempt to get the field value from the object
  field_value = get_field_value(obj, field)

  if !is_required?(field_value, attributes)
    return true
  end

  profile = attributes[:profile]
  if profile == nil
    raise InvalidRuleAttributes.new(ChildValidationRule, field)
  end

  if field_value == nil
    return false
  end

  parent_field = field.to_s
  if parent != nil
    parent_field = "#{parent.to_s}.#{field.to_s}"
  end

  return @validation_manager.validate(field_value, profile, parent_field)

end