activemodel 3.0.0.rc → 3.0.0.rc2
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +6 -1
- data/README.rdoc +34 -34
- data/lib/active_model/attribute_methods.rb +43 -43
- data/lib/active_model/callbacks.rb +31 -28
- data/lib/active_model/conversion.rb +14 -11
- data/lib/active_model/dirty.rb +22 -24
- data/lib/active_model/errors.rb +44 -48
- data/lib/active_model/lint.rb +2 -1
- data/lib/active_model/naming.rb +11 -8
- data/lib/active_model/observing.rb +7 -7
- data/lib/active_model/serialization.rb +23 -21
- data/lib/active_model/serializers/xml.rb +1 -1
- data/lib/active_model/translation.rb +8 -8
- data/lib/active_model/validations.rb +40 -29
- data/lib/active_model/validations/acceptance.rb +11 -11
- data/lib/active_model/validations/callbacks.rb +18 -4
- data/lib/active_model/validations/confirmation.rb +10 -10
- data/lib/active_model/validations/length.rb +10 -11
- data/lib/active_model/validations/validates.rb +8 -8
- data/lib/active_model/validator.rb +14 -14
- data/lib/active_model/version.rb +1 -1
- metadata +7 -7
@@ -9,7 +9,7 @@ module ActiveModel
|
|
9
9
|
# validator classes ending in 'Validator'. Note that Rails default
|
10
10
|
# validators can be overridden inside specific classes by creating
|
11
11
|
# custom validator classes in their place such as PresenceValidator.
|
12
|
-
#
|
12
|
+
#
|
13
13
|
# Examples of using the default rails validators:
|
14
14
|
#
|
15
15
|
# validates :terms, :acceptance => true
|
@@ -21,26 +21,26 @@ module ActiveModel
|
|
21
21
|
# validates :age, :numericality => true
|
22
22
|
# validates :username, :presence => true
|
23
23
|
# validates :username, :uniqueness => true
|
24
|
-
#
|
24
|
+
#
|
25
25
|
# The power of the +validates+ method comes when using custom validators
|
26
26
|
# and default validators in one call for a given attribute e.g.
|
27
27
|
#
|
28
28
|
# class EmailValidator < ActiveModel::EachValidator
|
29
29
|
# def validate_each(record, attribute, value)
|
30
30
|
# record.errors[attribute] << (options[:message] || "is not an email") unless
|
31
|
-
# value =~
|
31
|
+
# value =~ /\A([^@\s]+)@((?:[-a-z0-9]+\.)+[a-z]{2,})\z/i
|
32
32
|
# end
|
33
33
|
# end
|
34
|
-
#
|
34
|
+
#
|
35
35
|
# class Person
|
36
36
|
# include ActiveModel::Validations
|
37
37
|
# attr_accessor :name, :email
|
38
|
-
#
|
38
|
+
#
|
39
39
|
# validates :name, :presence => true, :uniqueness => true, :length => { :maximum => 100 }
|
40
40
|
# validates :email, :presence => true, :email => true
|
41
41
|
# end
|
42
|
-
#
|
43
|
-
# Validator classes
|
42
|
+
#
|
43
|
+
# Validator classes may also exist within the class being validated
|
44
44
|
# allowing custom modules of validators to be included as needed e.g.
|
45
45
|
#
|
46
46
|
# class Film
|
@@ -48,7 +48,7 @@ module ActiveModel
|
|
48
48
|
#
|
49
49
|
# class TitleValidator < ActiveModel::EachValidator
|
50
50
|
# def validate_each(record, attribute, value)
|
51
|
-
# record.errors[attribute] << "must start with 'the'" unless =~
|
51
|
+
# record.errors[attribute] << "must start with 'the'" unless value =~ /\Athe/i
|
52
52
|
# end
|
53
53
|
# end
|
54
54
|
#
|
@@ -5,8 +5,8 @@ require 'active_support/core_ext/object/blank'
|
|
5
5
|
module ActiveModel #:nodoc:
|
6
6
|
|
7
7
|
# == Active Model Validator
|
8
|
-
#
|
9
|
-
# A simple base class that can be used along with
|
8
|
+
#
|
9
|
+
# A simple base class that can be used along with
|
10
10
|
# +ActiveModel::Validations::ClassMethods.validates_with+
|
11
11
|
#
|
12
12
|
# class Person
|
@@ -61,31 +61,31 @@ module ActiveModel #:nodoc:
|
|
61
61
|
# @my_custom_field = options[:field_name] || :first_name
|
62
62
|
# end
|
63
63
|
# end
|
64
|
-
#
|
64
|
+
#
|
65
65
|
# The easiest way to add custom validators for validating individual attributes
|
66
66
|
# is with the convenient ActiveModel::EachValidator for example:
|
67
|
-
#
|
67
|
+
#
|
68
68
|
# class TitleValidator < ActiveModel::EachValidator
|
69
69
|
# def validate_each(record, attribute, value)
|
70
70
|
# record.errors[attribute] << 'must be Mr. Mrs. or Dr.' unless ['Mr.', 'Mrs.', 'Dr.'].include?(value)
|
71
71
|
# end
|
72
72
|
# end
|
73
|
-
#
|
73
|
+
#
|
74
74
|
# This can now be used in combination with the +validates+ method
|
75
75
|
# (see ActiveModel::Validations::ClassMethods.validates for more on this)
|
76
|
-
#
|
76
|
+
#
|
77
77
|
# class Person
|
78
78
|
# include ActiveModel::Validations
|
79
79
|
# attr_accessor :title
|
80
|
-
#
|
80
|
+
#
|
81
81
|
# validates :title, :presence => true, :title => true
|
82
82
|
# end
|
83
|
-
#
|
83
|
+
#
|
84
84
|
# Validator may also define a +setup+ instance method which will get called
|
85
85
|
# with the class that using that validator as it's argument. This can be
|
86
86
|
# useful when there are prerequisites such as an attr_accessor being present
|
87
87
|
# for example:
|
88
|
-
#
|
88
|
+
#
|
89
89
|
# class MyValidator < ActiveModel::Validator
|
90
90
|
# def setup(klass)
|
91
91
|
# klass.send :attr_accessor, :custom_attribute
|
@@ -94,7 +94,7 @@ module ActiveModel #:nodoc:
|
|
94
94
|
#
|
95
95
|
# This setup method is only called when used with validation macros or the
|
96
96
|
# class level <tt>validates_with</tt> method.
|
97
|
-
#
|
97
|
+
#
|
98
98
|
class Validator
|
99
99
|
attr_reader :options
|
100
100
|
|
@@ -102,8 +102,8 @@ module ActiveModel #:nodoc:
|
|
102
102
|
#
|
103
103
|
# == Examples
|
104
104
|
#
|
105
|
-
# PresenceValidator.kind
|
106
|
-
# UniquenessValidator.kind
|
105
|
+
# PresenceValidator.kind # => :presence
|
106
|
+
# UniquenessValidator.kind # => :uniqueness
|
107
107
|
#
|
108
108
|
def self.kind
|
109
109
|
@kind ||= name.split('::').last.underscore.sub(/_validator$/, '').to_sym unless anonymous?
|
@@ -111,7 +111,7 @@ module ActiveModel #:nodoc:
|
|
111
111
|
|
112
112
|
# Accepts options that will be made available through the +options+ reader.
|
113
113
|
def initialize(options)
|
114
|
-
@options = options
|
114
|
+
@options = options.freeze
|
115
115
|
end
|
116
116
|
|
117
117
|
# Return the kind for this validator.
|
@@ -133,7 +133,7 @@ module ActiveModel #:nodoc:
|
|
133
133
|
# All Active Model validations are built on top of this Validator.
|
134
134
|
class EachValidator < Validator
|
135
135
|
attr_reader :attributes
|
136
|
-
|
136
|
+
|
137
137
|
# Returns a new validator instance. All options will be available via the
|
138
138
|
# +options+ reader, however the <tt>:attributes</tt> option will be removed
|
139
139
|
# and instead be made available through the +attributes+ reader.
|
data/lib/active_model/version.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: activemodel
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
hash:
|
4
|
+
hash: 977940607
|
5
5
|
prerelease: true
|
6
6
|
segments:
|
7
7
|
- 3
|
8
8
|
- 0
|
9
9
|
- 0
|
10
|
-
-
|
11
|
-
version: 3.0.0.
|
10
|
+
- rc2
|
11
|
+
version: 3.0.0.rc2
|
12
12
|
platform: ruby
|
13
13
|
authors:
|
14
14
|
- David Heinemeier Hansson
|
@@ -16,7 +16,7 @@ autorequire:
|
|
16
16
|
bindir: bin
|
17
17
|
cert_chain: []
|
18
18
|
|
19
|
-
date: 2010-
|
19
|
+
date: 2010-08-23 00:00:00 -05:00
|
20
20
|
default_executable:
|
21
21
|
dependencies:
|
22
22
|
- !ruby/object:Gem::Dependency
|
@@ -27,13 +27,13 @@ dependencies:
|
|
27
27
|
requirements:
|
28
28
|
- - "="
|
29
29
|
- !ruby/object:Gem::Version
|
30
|
-
hash:
|
30
|
+
hash: 977940607
|
31
31
|
segments:
|
32
32
|
- 3
|
33
33
|
- 0
|
34
34
|
- 0
|
35
|
-
-
|
36
|
-
version: 3.0.0.
|
35
|
+
- rc2
|
36
|
+
version: 3.0.0.rc2
|
37
37
|
type: :runtime
|
38
38
|
version_requirements: *id001
|
39
39
|
- !ruby/object:Gem::Dependency
|