Module: SimpleRecord::Validations

Included in:
Base
Defined in:
lib/simple_record/validations.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object

if defined?(:valid?) # from ActiveModel

  alias_method :am_valid?, :valid?
end


11
12
13
14
15
16
17
18
# File 'lib/simple_record/validations.rb', line 11

def self.included(base)
#      puts 'Validations included ' + base.inspect
#      if defined?(ActiveModel)
#        base.class_eval do
#          alias_method :am_valid?, :valid?
#        end
#      end
end

Instance Method Details

#invalid?Boolean

Returns:

  • (Boolean)


59
60
61
# File 'lib/simple_record/validations.rb', line 59

def invalid?
  !valid?
end

#read_attribute_for_validation(key) ⇒ Object



64
65
66
# File 'lib/simple_record/validations.rb', line 64

def read_attribute_for_validation(key)
  @attributes[key.to_s]
end

#valid?Boolean

Returns:

  • (Boolean)


34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# File 'lib/simple_record/validations.rb', line 34

def valid?
#      puts 'in rails2 valid?'
  errors.clear

  if respond_to?(:am_valid?)
    # And now ActiveModel validations too
    am_valid?
  end

    #            run_callbacks(:validate)
  validate
  validate_uniques

  if new_record?
    #                run_callbacks(:validate_on_create)
    validate_on_create
  else
    #                run_callbacks(:validate_on_update)
    validate_on_update
  end


  errors.empty?
end

#validateObject



83
84
85
# File 'lib/simple_record/validations.rb', line 83

def validate
  true
end

#validate_on_createObject



87
88
89
# File 'lib/simple_record/validations.rb', line 87

def validate_on_create
  true
end

#validate_on_updateObject



91
92
93
# File 'lib/simple_record/validations.rb', line 91

def validate_on_update
  true
end

#validate_uniquesObject



68
69
70
71
72
73
74
75
76
77
78
79
80
81
# File 'lib/simple_record/validations.rb', line 68

def validate_uniques
  puts 'uniques=' + self.class.uniques.inspect
  self.class.uniques.each_pair do |k, v|
    val = self.send(k)
    puts 'val=' + val.inspect
    if val
      ret = self.class.find(:first, :conditions=>["#{k}=?", val])
      puts 'ret=' + ret.inspect
      if ret
        errors.add(k, "must be unique.")
      end
    end
  end
end