[ca2115]: lib / active_record_ext.rb Maximize Restore History

Download this file

active_record_ext.rb    59 lines (55 with data), 2.2 kB

 1
 2
 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
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
#
# This file is part of the Jikes RVM project (http://jikesrvm.org).
#
# This file is licensed to You under the Eclipse Public License (EPL);
# You may not use this file except in compliance with the License. You
# may obtain a copy of the License at
#
# http://www.opensource.org/licenses/cpl1.0.php
#
# See the COPYRIGHT.txt file distributed with this work for information
# regarding copyright ownership.
#
module ActiveRecord
class Base
def self.validates_reference_exists(name, type, options={})
configuration = {:message => 'does not reference a valid record.', :on => :save}
configuration.update(options)
validates_each([name], configuration) do |record, attr_name, value|
value = record.send(attr_name)
record.errors.add(attr_name, configuration[:message]) if (not value.nil? and not type.exists?(value))
end
end
def self.validates_positiveness_of(name, options={})
configuration = {:message => 'must be positive.', :on => :save}
configuration.update(options)
validates_each([name], configuration) do |record, attr_name, value|
value = record.send(attr_name)
record.errors.add(attr_name, configuration[:message]) if (not value.nil? and value < 0)
end
end
def self.validates_non_presence_of(name, options={})
configuration = {:message => 'must be blank.', :on => :save}
configuration.update(options)
validates_each([name], configuration) do |record, attr_name, value|
value = record.send(attr_name)
record.errors.add(attr_name, configuration[:message]) unless value.blank?
end
end
def self.validates_not_null(name, options={})
configuration = {:message => ActiveRecord::Errors.default_error_messages[:blank], :on => :save}
configuration.update(options)
validates_each([name], configuration) do |record, attr_name, value|
value = record.send(attr_name)
record.errors.add(attr_name, configuration[:message]) if value.nil?
end
end
def label
if self.respond_to?(:name)
self.name
else
self.to_s
end
end
end
end