Skip to content

Commit 9131a88

Browse files
joshkCarl Lerche
authored andcommitted
validation macros can now be used within an instance
1 parent 2203c78 commit 9131a88

File tree

13 files changed

+114
-14
lines changed

13 files changed

+114
-14
lines changed

activemodel/lib/active_model/validations.rb

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,9 @@ module Validations
4646

4747
included do
4848
extend ActiveModel::Translation
49+
50+
extend HelperMethods; include HelperMethods
51+
4952
define_callbacks :validate, :scope => :name
5053

5154
attr_accessor :validation_context
@@ -138,12 +141,6 @@ def validators_on(attribute)
138141
def attribute_method?(attribute)
139142
method_defined?(attribute)
140143
end
141-
private
142-
143-
def _merge_attributes(attr_names)
144-
options = attr_names.extract_options!
145-
options.merge(:attributes => attr_names.flatten)
146-
end
147144
end
148145

149146
# Returns the Errors object that holds all information about attribute error messages.

activemodel/lib/active_model/validations/acceptance.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ def setup(klass)
2121
end
2222
end
2323

24-
module ClassMethods
24+
module HelperMethods
2525
# Encapsulates the pattern of wanting to validate the acceptance of a terms of service check box (or similar agreement). Example:
2626
#
2727
# class Person < ActiveRecord::Base

activemodel/lib/active_model/validations/confirmation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def setup(klass)
1212
end
1313
end
1414

15-
module ClassMethods
15+
module HelperMethods
1616
# Encapsulates the pattern of wanting to validate a password or email address field with a confirmation. Example:
1717
#
1818
# Model:

activemodel/lib/active_model/validations/exclusion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def validate_each(record, attribute, value)
1212
end
1313
end
1414

15-
module ClassMethods
15+
module HelperMethods
1616
# Validates that the value of the specified attribute is not in a particular enumerable object.
1717
#
1818
# class Person < ActiveRecord::Base

activemodel/lib/active_model/validations/format.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def check_validity!
2424
end
2525
end
2626

27-
module ClassMethods
27+
module HelperMethods
2828
# Validates whether the value of the specified attribute is of the correct form, going by the regular expression provided.
2929
# You can require that the attribute matches the regular expression:
3030
#
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
module ActiveModel
2+
module Validations
3+
module HelperMethods
4+
private
5+
6+
def _merge_attributes(attr_names)
7+
options = attr_names.extract_options!
8+
options.merge(:attributes => attr_names.flatten)
9+
end
10+
end
11+
end
12+
end

activemodel/lib/active_model/validations/inclusion.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ def validate_each(record, attribute, value)
1212
end
1313
end
1414

15-
module ClassMethods
15+
module HelperMethods
1616
# Validates whether the value of the specified attribute is available in a particular enumerable object.
1717
#
1818
# class Person < ActiveRecord::Base

activemodel/lib/active_model/validations/length.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ def validate_each(record, attribute, value)
5151
end
5252
end
5353

54-
module ClassMethods
54+
module HelperMethods
5555

5656
# Validates that the specified attribute matches the length restrictions supplied. Only one option can be used at a time:
5757
#

activemodel/lib/active_model/validations/numericality.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ def parse_raw_value_as_an_integer(raw_value)
7070

7171
end
7272

73-
module ClassMethods
73+
module HelperMethods
7474
# Validates whether the value of the specified attribute is numeric by trying to convert it to
7575
# a float with Kernel.Float (if <tt>only_integer</tt> is false) or applying it to the regular expression
7676
# <tt>/\A[\+\-]?\d+\Z/</tt> (if <tt>only_integer</tt> is set to true).

activemodel/lib/active_model/validations/presence.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ def validate(record)
88
end
99
end
1010

11-
module ClassMethods
11+
module HelperMethods
1212
# Validates that the specified attributes are not blank (as defined by Object#blank?). Happens by default on save. Example:
1313
#
1414
# class Person < ActiveRecord::Base

0 commit comments

Comments
 (0)