Module: SugarCRM::AttributeValidations

Defined in:
lib/sugarcrm/attributes/attribute_validations.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.full_messagesObject

for rails compatibility



11
12
13
14
15
16
# File 'lib/sugarcrm/attributes/attribute_validations.rb', line 11

def @errors.full_messages
  # After removing attributes without errors, flatten the error hash, repeating the name of the attribute before each message:
  # e.g. {'name' => ['cannot be blank', 'is too long'], 'website' => ['is not valid']}
  # will become 'name cannot be blank, name is too long, website is not valid
  self.inject([]){|memo, obj| memo.concat(obj[1].inject([]){|m, o| m << "#{obj[0].to_s.humanize} #{o}" })}
end

Instance Method Details

#valid?Boolean

Checks to see if we have all the neccessary attributes

Returns:

  • (Boolean)


3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
# File 'lib/sugarcrm/attributes/attribute_validations.rb', line 3

def valid?
  @errors = (defined?(HashWithIndifferentAccess) ? HashWithIndifferentAccess : ActiveSupport::HashWithIndifferentAccess).new
  
  self.class._module.required_fields.each do |attribute|
    valid_attribute?(attribute)
  end
  
  # for rails compatibility
  def @errors.full_messages
    # After removing attributes without errors, flatten the error hash, repeating the name of the attribute before each message:
    # e.g. {'name' => ['cannot be blank', 'is too long'], 'website' => ['is not valid']}
    # will become 'name cannot be blank, name is too long, website is not valid
    self.inject([]){|memo, obj| memo.concat(obj[1].inject([]){|m, o| m << "#{obj[0].to_s.humanize} #{o}" })}
  end
  
  # Rails needs each attribute to be present in the error hash (if the attribute has no error, it has [] as a value)
  # Redefine the [] method for the errors hash to return [] instead of nil is the hash doesn't contain the key
  class << @errors
    alias :old_key_lookup :[]
    def [](key)
      old_key_lookup(key) || Array.new
    end
  end
  
  @errors.size == 0
end