activemodel 3.0.20 → 3.1.0.beta1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (36) hide show
  1. data/CHANGELOG +17 -73
  2. data/MIT-LICENSE +1 -1
  3. data/README.rdoc +5 -5
  4. data/lib/active_model.rb +2 -2
  5. data/lib/active_model/attribute_methods.rb +46 -53
  6. data/lib/active_model/callbacks.rb +2 -5
  7. data/lib/active_model/conversion.rb +0 -2
  8. data/lib/active_model/dirty.rb +3 -4
  9. data/lib/active_model/errors.rb +55 -56
  10. data/lib/active_model/lint.rb +2 -2
  11. data/lib/active_model/mass_assignment_security.rb +96 -47
  12. data/lib/active_model/naming.rb +55 -13
  13. data/lib/active_model/observer_array.rb +104 -0
  14. data/lib/active_model/observing.rb +53 -18
  15. data/lib/active_model/secure_password.rb +67 -0
  16. data/lib/active_model/serialization.rb +4 -11
  17. data/lib/active_model/serializers/json.rb +18 -18
  18. data/lib/active_model/serializers/xml.rb +26 -5
  19. data/lib/active_model/translation.rb +4 -11
  20. data/lib/active_model/validations.rb +23 -23
  21. data/lib/active_model/validations/acceptance.rb +3 -5
  22. data/lib/active_model/validations/callbacks.rb +5 -19
  23. data/lib/active_model/validations/confirmation.rb +6 -5
  24. data/lib/active_model/validations/exclusion.rb +27 -3
  25. data/lib/active_model/validations/format.rb +38 -12
  26. data/lib/active_model/validations/inclusion.rb +30 -23
  27. data/lib/active_model/validations/length.rb +3 -1
  28. data/lib/active_model/validations/numericality.rb +4 -2
  29. data/lib/active_model/validations/presence.rb +3 -2
  30. data/lib/active_model/validations/validates.rb +23 -9
  31. data/lib/active_model/validations/with.rb +14 -2
  32. data/lib/active_model/validator.rb +16 -18
  33. data/lib/active_model/version.rb +3 -3
  34. metadata +71 -58
  35. checksums.yaml +0 -7
  36. data/lib/active_model/deprecated_error_methods.rb +0 -33
@@ -84,7 +84,9 @@ module ActiveModel
84
84
  # * <tt>:too_short</tt> - The error message if the attribute goes under the minimum (default is: "is too short (min is %{count} characters)").
85
85
  # * <tt>:wrong_length</tt> - The error message if using the <tt>:is</tt> method and the attribute is the wrong size (default is: "is the wrong length (should be %{count} characters)").
86
86
  # * <tt>:message</tt> - The error message to use for a <tt>:minimum</tt>, <tt>:maximum</tt>, or <tt>:is</tt> violation. An alias of the appropriate <tt>too_long</tt>/<tt>too_short</tt>/<tt>wrong_length</tt> message.
87
- # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
87
+ # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
88
+ # validation contexts by default (+nil+), other options are <tt>:create</tt>
89
+ # and <tt>:update</tt>.
88
90
  # * <tt>:if</tt> - Specifies a method, proc or string to call to determine if the validation should
89
91
  # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>). The
90
92
  # method, proc or string should return or evaluate to a true or false value.
@@ -24,7 +24,7 @@ module ActiveModel
24
24
  def validate_each(record, attr_name, value)
25
25
  before_type_cast = "#{attr_name}_before_type_cast"
26
26
 
27
- raw_value = record.send("#{attr_name}_before_type_cast") if record.respond_to?(before_type_cast.to_sym)
27
+ raw_value = record.send(before_type_cast) if record.respond_to?(before_type_cast.to_sym)
28
28
  raw_value ||= value
29
29
 
30
30
  return if options[:allow_nil] && raw_value.nil?
@@ -93,7 +93,9 @@ module ActiveModel
93
93
  #
94
94
  # Configuration options:
95
95
  # * <tt>:message</tt> - A custom error message (default is: "is not a number").
96
- # * <tt>:on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>, <tt>:update</tt>).
96
+ # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
97
+ # validation contexts by default (+nil+), other options are <tt>:create</tt>
98
+ # and <tt>:update</tt>.
97
99
  # * <tt>:only_integer</tt> - Specifies whether the value has to be an integer, e.g. an integral value (default is +false+).
98
100
  # * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+ (default is +false+). Notice that for fixnum and float columns empty strings are converted to +nil+.
99
101
  # * <tt>:greater_than</tt> - Specifies the value must be greater than the supplied value.
@@ -26,8 +26,9 @@ module ActiveModel
26
26
  #
27
27
  # Configuration options:
28
28
  # * <tt>message</tt> - A custom error message (default is: "can't be blank").
29
- # * <tt>on</tt> - Specifies when this validation is active (default is <tt>:save</tt>, other options <tt>:create</tt>,
30
- # <tt>:update</tt>).
29
+ # * <tt>:on</tt> - Specifies when this validation is active. Runs in all
30
+ # validation contexts by default (+nil+), other options are <tt>:create</tt>
31
+ # and <tt>:update</tt>.
31
32
  # * <tt>if</tt> - Specifies a method, proc or string to call to determine if the validation should
32
33
  # occur (e.g. <tt>:if => :allow_validation</tt>, or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
33
34
  # The method, proc or string should return or evaluate to a true or false value.
@@ -55,14 +55,23 @@ module ActiveModel
55
55
  # validates :name, :title => true
56
56
  # end
57
57
  #
58
- # The validators hash can also handle regular expressions, ranges and arrays:
58
+ # Additionally validator classes may be in another namespace and still used within any class.
59
+ #
60
+ # validates :name, :'file/title' => true
61
+ #
62
+ # The validators hash can also handle regular expressions, ranges,
63
+ # arrays and strings in shortcut form, e.g.
59
64
  #
60
65
  # validates :email, :format => /@/
61
66
  # validates :gender, :inclusion => %w(male female)
62
67
  # validates :password, :length => 6..20
63
68
  #
64
- # Finally, the options :if, :unless, :on, :allow_blank and :allow_nil can be given
65
- # to one specific validator:
69
+ # When using shortcut form, ranges and arrays are passed to your
70
+ # validator's initializer as +options[:in]+ while other types including
71
+ # regular expressions and strings are passed as +options[:with]+
72
+ #
73
+ # Finally, the options +:if+, +:unless+, +:on+, +:allow_blank+ and +:allow_nil+ can be given
74
+ # to one specific validator, as a hash:
66
75
  #
67
76
  # validates :password, :presence => { :if => :password_required? }, :confirmation => true
68
77
  #
@@ -72,17 +81,18 @@ module ActiveModel
72
81
  #
73
82
  def validates(*attributes)
74
83
  defaults = attributes.extract_options!
75
- validations = defaults.slice!(:if, :unless, :on, :allow_blank, :allow_nil)
84
+ validations = defaults.slice!(*_validates_default_keys)
76
85
 
77
86
  raise ArgumentError, "You need to supply at least one attribute" if attributes.empty?
78
- raise ArgumentError, "Attribute names must be symbols" if attributes.any?{ |attribute| !attribute.is_a?(Symbol) }
79
87
  raise ArgumentError, "You need to supply at least one validation" if validations.empty?
80
88
 
81
89
  defaults.merge!(:attributes => attributes)
82
90
 
83
91
  validations.each do |key, options|
92
+ key = "#{key.to_s.camelize}Validator"
93
+
84
94
  begin
85
- validator = const_get("#{key.to_s.camelize}Validator")
95
+ validator = key.include?('::') ? key.constantize : const_get(key)
86
96
  rescue NameError
87
97
  raise ArgumentError, "Unknown validator: '#{key}'"
88
98
  end
@@ -93,18 +103,22 @@ module ActiveModel
93
103
 
94
104
  protected
95
105
 
106
+ # When creating custom validators, it might be useful to be able to specify
107
+ # additional default keys. This can be done by overwriting this method.
108
+ def _validates_default_keys
109
+ [ :if, :unless, :on, :allow_blank, :allow_nil ]
110
+ end
111
+
96
112
  def _parse_validates_options(options) #:nodoc:
97
113
  case options
98
114
  when TrueClass
99
115
  {}
100
116
  when Hash
101
117
  options
102
- when Regexp
103
- { :with => options }
104
118
  when Range, Array
105
119
  { :in => options }
106
120
  else
107
- raise ArgumentError, "#{options.inspect} is an invalid option. Expecting true, Hash, Regexp, Range, or Array"
121
+ { :with => options }
108
122
  end
109
123
  end
110
124
  end
@@ -8,6 +8,18 @@ module ActiveModel
8
8
  end
9
9
  end
10
10
 
11
+ class WithValidator < EachValidator
12
+ def validate_each(record, attr, val)
13
+ method_name = options[:with]
14
+
15
+ if record.method(method_name).arity == 0
16
+ record.send method_name
17
+ else
18
+ record.send method_name, attr
19
+ end
20
+ end
21
+ end
22
+
11
23
  module ClassMethods
12
24
  # Passes the record off to the class or classes specified and allows them
13
25
  # to add errors based on more complex conditions.
@@ -38,9 +50,9 @@ module ActiveModel
38
50
  # end
39
51
  #
40
52
  # Configuration options:
41
- # * <tt>on</tt> - Specifies when this validation is active
53
+ # * <tt>:on</tt> - Specifies when this validation is active
42
54
  # (<tt>:create</tt> or <tt>:update</tt>
43
- # * <tt>if</tt> - Specifies a method, proc or string to call to determine
55
+ # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
44
56
  # if the validation should occur (e.g. <tt>:if => :allow_validation</tt>,
45
57
  # or <tt>:if => Proc.new { |user| user.signup_step > 2 }</tt>).
46
58
  # The method, proc or string should return or evaluate to a true or false value.
@@ -1,6 +1,7 @@
1
1
  require 'active_support/core_ext/array/wrap'
2
2
  require "active_support/core_ext/module/anonymous"
3
3
  require 'active_support/core_ext/object/blank'
4
+ require 'active_support/core_ext/object/inclusion'
4
5
 
5
6
  module ActiveModel #:nodoc:
6
7
 
@@ -63,27 +64,27 @@ module ActiveModel #:nodoc:
63
64
  # end
64
65
  #
65
66
  # The easiest way to add custom validators for validating individual attributes
66
- # is with the convenient ActiveModel::EachValidator for example:
67
+ # is with the convenient <tt>ActiveModel::EachValidator</tt>. For example:
67
68
  #
68
69
  # class TitleValidator < ActiveModel::EachValidator
69
70
  # def validate_each(record, attribute, value)
70
- # record.errors[attribute] << 'must be Mr. Mrs. or Dr.' unless ['Mr.', 'Mrs.', 'Dr.'].include?(value)
71
+ # record.errors[attribute] << 'must be Mr. Mrs. or Dr.' unless value.in?(['Mr.', 'Mrs.', 'Dr.'])
71
72
  # end
72
73
  # end
73
74
  #
74
75
  # This can now be used in combination with the +validates+ method
75
- # (see ActiveModel::Validations::ClassMethods.validates for more on this)
76
+ # (see <tt>ActiveModel::Validations::ClassMethods.validates</tt> for more on this)
76
77
  #
77
78
  # class Person
78
79
  # include ActiveModel::Validations
79
80
  # attr_accessor :title
80
81
  #
81
- # validates :title, :presence => true, :title => true
82
+ # validates :title, :presence => true
82
83
  # end
83
84
  #
84
85
  # Validator may also define a +setup+ instance method which will get called
85
- # with the class that using that validator as it's argument. This can be
86
- # useful when there are prerequisites such as an attr_accessor being present
86
+ # with the class that using that validator as its argument. This can be
87
+ # useful when there are prerequisites such as an +attr_accessor+ being present
87
88
  # for example:
88
89
  #
89
90
  # class MyValidator < ActiveModel::Validator
@@ -98,9 +99,7 @@ module ActiveModel #:nodoc:
98
99
  class Validator
99
100
  attr_reader :options
100
101
 
101
- # Returns the kind of the validator.
102
- #
103
- # == Examples
102
+ # Returns the kind of the validator. Examples:
104
103
  #
105
104
  # PresenceValidator.kind # => :presence
106
105
  # UniquenessValidator.kind # => :uniqueness
@@ -122,15 +121,15 @@ module ActiveModel #:nodoc:
122
121
  # Override this method in subclasses with validation logic, adding errors
123
122
  # to the records +errors+ array where necessary.
124
123
  def validate(record)
125
- raise NotImplementedError
124
+ raise NotImplementedError, "Subclasses must implement a validate(record) method."
126
125
  end
127
126
  end
128
127
 
129
- # EachValidator is a validator which iterates through the attributes given
130
- # in the options hash invoking the validate_each method passing in the
128
+ # +EachValidator+ is a validator which iterates through the attributes given
129
+ # in the options hash invoking the <tt>validate_each</tt> method passing in the
131
130
  # record, attribute and value.
132
131
  #
133
- # All Active Model validations are built on top of this Validator.
132
+ # All Active Model validations are built on top of this validator.
134
133
  class EachValidator < Validator
135
134
  attr_reader :attributes
136
135
 
@@ -158,19 +157,18 @@ module ActiveModel #:nodoc:
158
157
  # Override this method in subclasses with the validation logic, adding
159
158
  # errors to the records +errors+ array where necessary.
160
159
  def validate_each(record, attribute, value)
161
- raise NotImplementedError
160
+ raise NotImplementedError, "Subclasses must implement a validate_each(record, attribute, value) method"
162
161
  end
163
162
 
164
163
  # Hook method that gets called by the initializer allowing verification
165
164
  # that the arguments supplied are valid. You could for example raise an
166
- # ArgumentError when invalid options are supplied.
165
+ # +ArgumentError+ when invalid options are supplied.
167
166
  def check_validity!
168
167
  end
169
168
  end
170
169
 
171
- # BlockValidator is a special EachValidator which receives a block on initialization
172
- # and call this block for each attribute being validated. +validates_each+ uses this
173
- # Validator.
170
+ # +BlockValidator+ is a special +EachValidator+ which receives a block on initialization
171
+ # and call this block for each attribute being validated. +validates_each+ uses this validator.
174
172
  class BlockValidator < EachValidator
175
173
  def initialize(options, &block)
176
174
  @block = block
@@ -1,9 +1,9 @@
1
1
  module ActiveModel
2
2
  module VERSION #:nodoc:
3
3
  MAJOR = 3
4
- MINOR = 0
5
- TINY = 20
6
- PRE = nil
4
+ MINOR = 1
5
+ TINY = 0
6
+ PRE = "beta1"
7
7
 
8
8
  STRING = [MAJOR, MINOR, TINY, PRE].compact.join('.')
9
9
  end
metadata CHANGED
@@ -1,72 +1,77 @@
1
- --- !ruby/object:Gem::Specification
1
+ --- !ruby/object:Gem::Specification
2
2
  name: activemodel
3
- version: !ruby/object:Gem::Version
4
- version: 3.0.20
3
+ version: !ruby/object:Gem::Version
4
+ prerelease: 6
5
+ version: 3.1.0.beta1
5
6
  platform: ruby
6
- authors:
7
+ authors:
7
8
  - David Heinemeier Hansson
8
9
  autorequire:
9
10
  bindir: bin
10
11
  cert_chain: []
11
- date: 2013-01-28 00:00:00.000000000 Z
12
- dependencies:
13
- - !ruby/object:Gem::Dependency
12
+
13
+ date: 2011-05-04 00:00:00 -05:00
14
+ default_executable:
15
+ dependencies:
16
+ - !ruby/object:Gem::Dependency
14
17
  name: activesupport
15
- requirement: !ruby/object:Gem::Requirement
16
- requirements:
17
- - - '='
18
- - !ruby/object:Gem::Version
19
- version: 3.0.20
20
- type: :runtime
21
18
  prerelease: false
22
- version_requirements: !ruby/object:Gem::Requirement
23
- requirements:
24
- - - '='
25
- - !ruby/object:Gem::Version
26
- version: 3.0.20
27
- - !ruby/object:Gem::Dependency
28
- name: builder
29
- requirement: !ruby/object:Gem::Requirement
30
- requirements:
31
- - - "~>"
32
- - !ruby/object:Gem::Version
33
- version: 2.1.2
19
+ requirement: &id001 !ruby/object:Gem::Requirement
20
+ none: false
21
+ requirements:
22
+ - - "="
23
+ - !ruby/object:Gem::Version
24
+ version: 3.1.0.beta1
34
25
  type: :runtime
26
+ version_requirements: *id001
27
+ - !ruby/object:Gem::Dependency
28
+ name: builder
35
29
  prerelease: false
36
- version_requirements: !ruby/object:Gem::Requirement
37
- requirements:
38
- - - "~>"
39
- - !ruby/object:Gem::Version
40
- version: 2.1.2
41
- - !ruby/object:Gem::Dependency
30
+ requirement: &id002 !ruby/object:Gem::Requirement
31
+ none: false
32
+ requirements:
33
+ - - ~>
34
+ - !ruby/object:Gem::Version
35
+ version: 3.0.0
36
+ type: :runtime
37
+ version_requirements: *id002
38
+ - !ruby/object:Gem::Dependency
42
39
  name: i18n
43
- requirement: !ruby/object:Gem::Requirement
44
- requirements:
45
- - - "~>"
46
- - !ruby/object:Gem::Version
47
- version: 0.5.0
40
+ prerelease: false
41
+ requirement: &id003 !ruby/object:Gem::Requirement
42
+ none: false
43
+ requirements:
44
+ - - ~>
45
+ - !ruby/object:Gem::Version
46
+ version: 0.6.0beta1
48
47
  type: :runtime
48
+ version_requirements: *id003
49
+ - !ruby/object:Gem::Dependency
50
+ name: bcrypt-ruby
49
51
  prerelease: false
50
- version_requirements: !ruby/object:Gem::Requirement
51
- requirements:
52
- - - "~>"
53
- - !ruby/object:Gem::Version
54
- version: 0.5.0
55
- description: A toolkit for building modeling frameworks like Active Record and Active
56
- Resource. Rich support for attributes, callbacks, validations, observers, serialization,
57
- internationalization, and testing.
52
+ requirement: &id004 !ruby/object:Gem::Requirement
53
+ none: false
54
+ requirements:
55
+ - - ~>
56
+ - !ruby/object:Gem::Version
57
+ version: 2.1.4
58
+ type: :runtime
59
+ version_requirements: *id004
60
+ description: A toolkit for building modeling frameworks like Active Record and Active Resource. Rich support for attributes, callbacks, validations, observers, serialization, internationalization, and testing.
58
61
  email: david@loudthinking.com
59
62
  executables: []
63
+
60
64
  extensions: []
65
+
61
66
  extra_rdoc_files: []
62
- files:
67
+
68
+ files:
63
69
  - CHANGELOG
64
70
  - MIT-LICENSE
65
71
  - README.rdoc
66
72
  - lib/active_model/attribute_methods.rb
67
73
  - lib/active_model/callbacks.rb
68
74
  - lib/active_model/conversion.rb
69
- - lib/active_model/deprecated_error_methods.rb
70
75
  - lib/active_model/dirty.rb
71
76
  - lib/active_model/errors.rb
72
77
  - lib/active_model/lint.rb
@@ -75,8 +80,10 @@ files:
75
80
  - lib/active_model/mass_assignment_security/sanitizer.rb
76
81
  - lib/active_model/mass_assignment_security.rb
77
82
  - lib/active_model/naming.rb
83
+ - lib/active_model/observer_array.rb
78
84
  - lib/active_model/observing.rb
79
85
  - lib/active_model/railtie.rb
86
+ - lib/active_model/secure_password.rb
80
87
  - lib/active_model/serialization.rb
81
88
  - lib/active_model/serializers/json.rb
82
89
  - lib/active_model/serializers/xml.rb
@@ -97,27 +104,33 @@ files:
97
104
  - lib/active_model/validator.rb
98
105
  - lib/active_model/version.rb
99
106
  - lib/active_model.rb
107
+ has_rdoc: true
100
108
  homepage: http://www.rubyonrails.org
101
109
  licenses: []
102
- metadata: {}
110
+
103
111
  post_install_message:
104
112
  rdoc_options: []
105
- require_paths:
113
+
114
+ require_paths:
106
115
  - lib
107
- required_ruby_version: !ruby/object:Gem::Requirement
108
- requirements:
116
+ required_ruby_version: !ruby/object:Gem::Requirement
117
+ none: false
118
+ requirements:
109
119
  - - ">="
110
- - !ruby/object:Gem::Version
120
+ - !ruby/object:Gem::Version
111
121
  version: 1.8.7
112
- required_rubygems_version: !ruby/object:Gem::Requirement
113
- requirements:
114
- - - ">="
115
- - !ruby/object:Gem::Version
116
- version: '0'
122
+ required_rubygems_version: !ruby/object:Gem::Requirement
123
+ none: false
124
+ requirements:
125
+ - - ">"
126
+ - !ruby/object:Gem::Version
127
+ version: 1.3.1
117
128
  requirements: []
129
+
118
130
  rubyforge_project: activemodel
119
- rubygems_version: 2.0.0.preview3.1
131
+ rubygems_version: 1.6.2
120
132
  signing_key:
121
- specification_version: 4
133
+ specification_version: 3
122
134
  summary: A toolkit for building modeling frameworks (part of Rails).
123
135
  test_files: []
136
+
checksums.yaml DELETED
@@ -1,7 +0,0 @@
1
- ---
2
- SHA1:
3
- metadata.gz: 711a0c8b07ab4da81228b150f5037acf2db8e4c7
4
- data.tar.gz: 9e21930d3a5461f19d059319336effef3967caa1
5
- SHA512:
6
- metadata.gz: 79b66e256aafe01c2e911fcc2a6e1aece080387a99945de8abbf2d32ba9cd0a4ca2e393fca66b9ce03ea63c5edd405b54a96c268a772035e2a9792dc71e2f4df
7
- data.tar.gz: 69b726a99060f4fd93efa39e616dc37832252a9613e12556d005f8510c00063b2bac00805dfb600c3bdee4ea361cd149eff81936a85d89c4d4861d2ecc18c8c7
@@ -1,33 +0,0 @@
1
- module ActiveModel
2
- module DeprecatedErrorMethods
3
- def on(attribute)
4
- message = "Errors#on have been deprecated, use Errors#[] instead.\n"
5
- message << "Also note that the behaviour of Errors#[] has changed. Errors#[] now always returns an Array. An empty Array is "
6
- message << "returned when there are no errors on the specified attribute."
7
- ActiveSupport::Deprecation.warn(message)
8
-
9
- errors = self[attribute]
10
- errors.size < 2 ? errors.first : errors
11
- end
12
-
13
- def on_base
14
- ActiveSupport::Deprecation.warn "Errors#on_base have been deprecated, use Errors#[:base] instead"
15
- ActiveSupport::Deprecation.silence { on(:base) }
16
- end
17
-
18
- def add_to_base(msg)
19
- ActiveSupport::Deprecation.warn "Errors#add_to_base(msg) has been deprecated, use Errors#add(:base, msg) instead"
20
- self[:base] << msg
21
- end
22
-
23
- def invalid?(attribute)
24
- ActiveSupport::Deprecation.warn "Errors#invalid?(attribute) has been deprecated, use Errors#[attribute].any? instead"
25
- self[attribute].any?
26
- end
27
-
28
- def each_full
29
- ActiveSupport::Deprecation.warn "Errors#each_full has been deprecated, use Errors#to_a.each instead"
30
- to_a.each { |error| yield error }
31
- end
32
- end
33
- end