activemodel 4.1.0.beta2 → 4.1.0.rc1

Sign up to get free protection for your applications and to get access to all the features.

Potentially problematic release.


This version of activemodel might be problematic. Click here for more details.

checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: c111a2e47286b33dd18788e85333a858b85d014f
4
- data.tar.gz: 81f66e9831772921ced4e1bb09211278bb808515
3
+ metadata.gz: 5c2e46709d7f8d23a7f279b60e968ed229340e15
4
+ data.tar.gz: bdd281762b3380a85ba7bea143e1864aed5cc25f
5
5
  SHA512:
6
- metadata.gz: 64495f6ee4759d16b1cfcdfdba553272b2b8b6f96f71d8d8a068e04d25e48eb91786f2dd1f1a77d6d0910d31ccea7f290fe6b8c58e4fdc9196f79ee1a8f4a9ca
7
- data.tar.gz: c3b0595e7b0560dc7a6b4e888c9640d3d06ecfbf65e3d5ed35afc9cde7ec761577d5641835e60a95e689b1ebea366aedcaa089877e6468ea924fbeea7ddcb54b
6
+ metadata.gz: ea32bb3ee8545b82dd63b1975ff79ad1ceaa7546b24170f1782c72539f800ac137dfc99445f617bb9ab8022faa5b6a3c23df2b021935ba0ededd963da113b861
7
+ data.tar.gz: 31bcdc6c6a1dfdcd06ce6ca967160dfbb388916f66c44e317304074e1d211139a163c76e24eb016239220c9f05517d7333837c6471db8f352f8fd8ab25046fc5
data/CHANGELOG.md CHANGED
@@ -1,3 +1,36 @@
1
+ * `#to_param` returns `nil` if `#to_key` returns `nil`. Fixes #11399.
2
+
3
+ *Yves Senn*
4
+
5
+ * Ability to specify multiple contexts when defining a validation.
6
+
7
+ Example:
8
+
9
+ class Person
10
+ include ActiveModel::Validations
11
+
12
+ attr_reader :name
13
+ validates_presence_of :name, on: [:verify, :approve]
14
+ end
15
+
16
+ person = Person.new
17
+ person.valid? # => true
18
+ person.valid?(:verify) # => false
19
+ person.errors.full_messages_for(:name) # => ["Name can't be blank"]
20
+ person.valid?(:approve) # => false
21
+ person.errors.full_messages_for(:name) # => ["Name can't be blank"]
22
+
23
+ *Vince Puzzella*
24
+
25
+ * `attribute_changed?` now accepts a hash to check if the attribute was
26
+ changed `:from` and/or `:to` a given value.
27
+
28
+ Example:
29
+
30
+ model.name_changed?(from: "Pete", to: "Ringo")
31
+
32
+ *Tejas Dinkar*
33
+
1
34
  * Fix `has_secure_password` to honor bcrypt-ruby's cost attribute.
2
35
 
3
36
  *T.J. Schuck*
@@ -13,8 +46,8 @@
13
46
 
14
47
  *Bogdan Gusiev*
15
48
 
16
- * Fix has_secure_password. `password_confirmation` validations are triggered
17
- even if no `password_confirmation` is set.
49
+ * Fix `has_secure_password` not to trigger `password_confirmation` validations
50
+ if no `password_confirmation` is set.
18
51
 
19
52
  *Vladimir Kiselev*
20
53
 
@@ -27,7 +60,7 @@
27
60
 
28
61
  *Charles Bergeron*
29
62
 
30
- * Fix regression in has_secure_password. When a password is set, but a
63
+ * Fix regression in `has_secure_password`. When a password is set, but a
31
64
  confirmation is an empty string, it would incorrectly save.
32
65
 
33
66
  *Steve Klabnik* and *Phillip Calvin*
data/MIT-LICENSE CHANGED
@@ -1,4 +1,4 @@
1
- Copyright (c) 2004-2013 David Heinemeier Hansson
1
+ Copyright (c) 2004-2014 David Heinemeier Hansson
2
2
 
3
3
  Permission is hereby granted, free of charge, to any person obtaining
4
4
  a copy of this software and associated documentation files (the
data/README.rdoc CHANGED
@@ -109,7 +109,7 @@ behavior out of the box:
109
109
  attr_reader :errors
110
110
 
111
111
  def validate!
112
- errors.add(:name, "can not be nil") if name.nil?
112
+ errors.add(:name, "cannot be nil") if name.nil?
113
113
  end
114
114
 
115
115
  def self.human_attribute_name(attr, options = {})
@@ -118,7 +118,7 @@ behavior out of the box:
118
118
  end
119
119
 
120
120
  person.errors.full_messages
121
- # => ["Name can not be nil"]
121
+ # => ["Name cannot be nil"]
122
122
 
123
123
  {Learn more}[link:classes/ActiveModel/Errors.html]
124
124
 
data/lib/active_model.rb CHANGED
@@ -1,5 +1,5 @@
1
1
  #--
2
- # Copyright (c) 2004-2013 David Heinemeier Hansson
2
+ # Copyright (c) 2004-2014 David Heinemeier Hansson
3
3
  #
4
4
  # Permission is hereby granted, free of charge, to any person obtaining
5
5
  # a copy of this software and associated documentation files (the
@@ -59,9 +59,9 @@ module ActiveModel
59
59
  end
60
60
  end
61
61
 
62
- def eager_load!
62
+ def self.eager_load!
63
63
  super
64
- ActiveModel::Serializer.eager_load!
64
+ ActiveModel::Serializers.eager_load!
65
65
  end
66
66
  end
67
67
 
@@ -14,11 +14,11 @@ module ActiveModel
14
14
  class MissingAttributeError < NoMethodError
15
15
  end
16
16
 
17
- # == Active \Model Attribute Methods
17
+ # == Active \Model \Attribute \Methods
18
18
  #
19
- # <tt>ActiveModel::AttributeMethods</tt> provides a way to add prefixes and
20
- # suffixes to your methods as well as handling the creation of
21
- # <tt>ActiveRecord::Base</tt>-like class methods such as +table_name+.
19
+ # Provides a way to add prefixes and suffixes to your methods as
20
+ # well as handling the creation of <tt>ActiveRecord::Base</tt>-like
21
+ # class methods such as +table_name+.
22
22
  #
23
23
  # The requirements to implement <tt>ActiveModel::AttributeMethods</tt> are to:
24
24
  #
@@ -27,7 +27,9 @@ module ActiveModel
27
27
  # or +attribute_method_prefix+.
28
28
  # * Call +define_attribute_methods+ after the other methods are called.
29
29
  # * Define the various generic +_attribute+ methods that you have declared.
30
- # * Define an +attributes+ method, see below.
30
+ # * Define an +attributes+ method which returns a hash with each
31
+ # attribute name in your model as hash key and the attribute value as hash value.
32
+ # Hash keys must be strings.
31
33
  #
32
34
  # A minimal implementation could be:
33
35
  #
@@ -42,7 +44,7 @@ module ActiveModel
42
44
  # attr_accessor :name
43
45
  #
44
46
  # def attributes
45
- # {'name' => @name}
47
+ # { 'name' => @name }
46
48
  # end
47
49
  #
48
50
  # private
@@ -59,13 +61,6 @@ module ActiveModel
59
61
  # send("#{attr}=", 'Default Name')
60
62
  # end
61
63
  # end
62
- #
63
- # Note that whenever you include <tt>ActiveModel::AttributeMethods</tt> in
64
- # your class, it requires you to implement an +attributes+ method which
65
- # returns a hash with each attribute name in your model as hash key and the
66
- # attribute value as hash value.
67
- #
68
- # Hash keys must be strings.
69
64
  module AttributeMethods
70
65
  extend ActiveSupport::Concern
71
66
 
@@ -173,14 +168,14 @@ module ActiveModel
173
168
  # private
174
169
  #
175
170
  # def reset_attribute_to_default!(attr)
176
- # ...
171
+ # send("#{attr}=", 'Default Name')
177
172
  # end
178
173
  # end
179
174
  #
180
175
  # person = Person.new
181
176
  # person.name # => 'Gem'
182
177
  # person.reset_name_to_default!
183
- # person.name # => 'Gemma'
178
+ # person.name # => 'Default Name'
184
179
  def attribute_method_affix(*affixes)
185
180
  self.attribute_method_matchers += affixes.map! { |affix| AttributeMethodMatcher.new prefix: affix[:prefix], suffix: affix[:suffix] }
186
181
  undefine_attribute_methods
@@ -250,7 +245,7 @@ module ActiveModel
250
245
  # private
251
246
  #
252
247
  # def clear_attribute(attr)
253
- # ...
248
+ # send("#{attr}=", nil)
254
249
  # end
255
250
  # end
256
251
  def define_attribute_methods(*attr_names)
@@ -30,7 +30,7 @@ module ActiveModel
30
30
  # end
31
31
  #
32
32
  # Then in your class, you can use the +before_create+, +after_create+ and
33
- # +around_create+ methods, just as you would in an Active Record module.
33
+ # +around_create+ methods, just as you would in an Active Record model.
34
34
  #
35
35
  # before_create :action_before_create
36
36
  #
@@ -1,5 +1,5 @@
1
1
  module ActiveModel
2
- # == Active \Model Conversion
2
+ # == Active \Model \Conversion
3
3
  #
4
4
  # Handles default conversions: to_model, to_key, to_param, and to_partial_path.
5
5
  #
@@ -62,7 +62,7 @@ module ActiveModel
62
62
  # person = Person.create
63
63
  # person.to_param # => "1"
64
64
  def to_param
65
- persisted? ? to_key.join('-') : nil
65
+ (persisted? && key = to_key) ? key.join('-') : nil
66
66
  end
67
67
 
68
68
  # Returns a +string+ identifying the path associated with the object.
@@ -54,6 +54,7 @@ module ActiveModel
54
54
  # person.name = 'Bob'
55
55
  # person.changed? # => true
56
56
  # person.name_changed? # => true
57
+ # person.name_changed?(from: "Uncle Bob", to: "Bob") # => true
57
58
  # person.name_was # => "Uncle Bob"
58
59
  # person.name_change # => ["Uncle Bob", "Bob"]
59
60
  # person.name = 'Bill'
@@ -135,7 +136,7 @@ module ActiveModel
135
136
  # person.save
136
137
  # person.previous_changes # => {"name" => ["bob", "robert"]}
137
138
  def previous_changes
138
- @previously_changed ||= {}
139
+ @previously_changed ||= ActiveSupport::HashWithIndifferentAccess.new
139
140
  end
140
141
 
141
142
  # Returns a hash of the attributes with unsaved changes indicating their original
@@ -149,8 +150,11 @@ module ActiveModel
149
150
  end
150
151
 
151
152
  # Handle <tt>*_changed?</tt> for +method_missing+.
152
- def attribute_changed?(attr) # :nodoc:
153
- changed_attributes.include?(attr)
153
+ def attribute_changed?(attr, options = {}) #:nodoc:
154
+ result = changed_attributes.include?(attr)
155
+ result &&= options[:to] == __send__(attr) if options.key?(:to)
156
+ result &&= options[:from] == changed_attributes[attr] if options.key?(:from)
157
+ result
154
158
  end
155
159
 
156
160
  # Handle <tt>*_was</tt> for +method_missing+.
@@ -163,13 +167,13 @@ module ActiveModel
163
167
  # Removes current changes and makes them accessible through +previous_changes+.
164
168
  def changes_applied
165
169
  @previously_changed = changes
166
- @changed_attributes = {}
170
+ @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
167
171
  end
168
172
 
169
173
  # Removes all dirty data: current changes and previous changes
170
174
  def reset_changes
171
- @previously_changed = {}
172
- @changed_attributes = {}
175
+ @previously_changed = ActiveSupport::HashWithIndifferentAccess.new
176
+ @changed_attributes = ActiveSupport::HashWithIndifferentAccess.new
173
177
  end
174
178
 
175
179
  # Handle <tt>*_change</tt> for +method_missing+.
@@ -7,7 +7,7 @@ module ActiveModel
7
7
  # == Active \Model \Errors
8
8
  #
9
9
  # Provides a modified +Hash+ that you can include in your object
10
- # for handling error messages and interacting with Action Pack helpers.
10
+ # for handling error messages and interacting with Action View helpers.
11
11
  #
12
12
  # A minimal implementation could be:
13
13
  #
@@ -23,7 +23,7 @@ module ActiveModel
23
23
  # attr_reader :errors
24
24
  #
25
25
  # def validate!
26
- # errors.add(:name, "can not be nil") if name == nil
26
+ # errors.add(:name, "cannot be nil") if name == nil
27
27
  # end
28
28
  #
29
29
  # # The following methods are needed to be minimally implemented
@@ -51,8 +51,8 @@ module ActiveModel
51
51
  # The above allows you to do:
52
52
  #
53
53
  # person = Person.new
54
- # person.validate! # => ["can not be nil"]
55
- # person.errors.full_messages # => ["name can not be nil"]
54
+ # person.validate! # => ["cannot be nil"]
55
+ # person.errors.full_messages # => ["name cannot be nil"]
56
56
  # # etc..
57
57
  class Errors
58
58
  include Enumerable
@@ -80,7 +80,7 @@ module ActiveModel
80
80
 
81
81
  # Clear the error messages.
82
82
  #
83
- # person.errors.full_messages # => ["name can not be nil"]
83
+ # person.errors.full_messages # => ["name cannot be nil"]
84
84
  # person.errors.clear
85
85
  # person.errors.full_messages # => []
86
86
  def clear
@@ -90,19 +90,19 @@ module ActiveModel
90
90
  # Returns +true+ if the error messages include an error for the given key
91
91
  # +attribute+, +false+ otherwise.
92
92
  #
93
- # person.errors.messages # => {:name=>["can not be nil"]}
93
+ # person.errors.messages # => {:name=>["cannot be nil"]}
94
94
  # person.errors.include?(:name) # => true
95
95
  # person.errors.include?(:age) # => false
96
96
  def include?(attribute)
97
- (v = messages[attribute]) && v.any?
97
+ messages[attribute].present?
98
98
  end
99
99
  # aliases include?
100
100
  alias :has_key? :include?
101
101
 
102
102
  # Get messages for +key+.
103
103
  #
104
- # person.errors.messages # => {:name=>["can not be nil"]}
105
- # person.errors.get(:name) # => ["can not be nil"]
104
+ # person.errors.messages # => {:name=>["cannot be nil"]}
105
+ # person.errors.get(:name) # => ["cannot be nil"]
106
106
  # person.errors.get(:age) # => nil
107
107
  def get(key)
108
108
  messages[key]
@@ -110,7 +110,7 @@ module ActiveModel
110
110
 
111
111
  # Set messages for +key+ to +value+.
112
112
  #
113
- # person.errors.get(:name) # => ["can not be nil"]
113
+ # person.errors.get(:name) # => ["cannot be nil"]
114
114
  # person.errors.set(:name, ["can't be nil"])
115
115
  # person.errors.get(:name) # => ["can't be nil"]
116
116
  def set(key, value)
@@ -119,8 +119,8 @@ module ActiveModel
119
119
 
120
120
  # Delete messages for +key+. Returns the deleted messages.
121
121
  #
122
- # person.errors.get(:name) # => ["can not be nil"]
123
- # person.errors.delete(:name) # => ["can not be nil"]
122
+ # person.errors.get(:name) # => ["cannot be nil"]
123
+ # person.errors.delete(:name) # => ["cannot be nil"]
124
124
  # person.errors.get(:name) # => nil
125
125
  def delete(key)
126
126
  messages.delete(key)
@@ -129,8 +129,8 @@ module ActiveModel
129
129
  # When passed a symbol or a name of a method, returns an array of errors
130
130
  # for the method.
131
131
  #
132
- # person.errors[:name] # => ["can not be nil"]
133
- # person.errors['name'] # => ["can not be nil"]
132
+ # person.errors[:name] # => ["cannot be nil"]
133
+ # person.errors['name'] # => ["cannot be nil"]
134
134
  def [](attribute)
135
135
  get(attribute.to_sym) || set(attribute.to_sym, [])
136
136
  end
@@ -175,15 +175,15 @@ module ActiveModel
175
175
 
176
176
  # Returns all message values.
177
177
  #
178
- # person.errors.messages # => {:name=>["can not be nil", "must be specified"]}
179
- # person.errors.values # => [["can not be nil", "must be specified"]]
178
+ # person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
179
+ # person.errors.values # => [["cannot be nil", "must be specified"]]
180
180
  def values
181
181
  messages.values
182
182
  end
183
183
 
184
184
  # Returns all message keys.
185
185
  #
186
- # person.errors.messages # => {:name=>["can not be nil", "must be specified"]}
186
+ # person.errors.messages # => {:name=>["cannot be nil", "must be specified"]}
187
187
  # person.errors.keys # => [:name]
188
188
  def keys
189
189
  messages.keys
@@ -211,7 +211,7 @@ module ActiveModel
211
211
  # Returns +true+ if no errors are found, +false+ otherwise.
212
212
  # If the error message is a string it can be empty.
213
213
  #
214
- # person.errors.full_messages # => ["name can not be nil"]
214
+ # person.errors.full_messages # => ["name cannot be nil"]
215
215
  # person.errors.empty? # => false
216
216
  def empty?
217
217
  all? { |k, v| v && v.empty? && !v.is_a?(String) }
@@ -238,8 +238,8 @@ module ActiveModel
238
238
  # object. You can pass the <tt>:full_messages</tt> option. This determines
239
239
  # if the json object should contain full messages or not (false by default).
240
240
  #
241
- # person.errors.as_json # => {:name=>["can not be nil"]}
242
- # person.errors.as_json(full_messages: true) # => {:name=>["name can not be nil"]}
241
+ # person.errors.as_json # => {:name=>["cannot be nil"]}
242
+ # person.errors.as_json(full_messages: true) # => {:name=>["name cannot be nil"]}
243
243
  def as_json(options=nil)
244
244
  to_hash(options && options[:full_messages])
245
245
  end
@@ -247,8 +247,8 @@ module ActiveModel
247
247
  # Returns a Hash of attributes with their error messages. If +full_messages+
248
248
  # is +true+, it will contain full messages (see +full_message+).
249
249
  #
250
- # person.errors.to_hash # => {:name=>["can not be nil"]}
251
- # person.errors.to_hash(true) # => {:name=>["name can not be nil"]}
250
+ # person.errors.to_hash # => {:name=>["cannot be nil"]}
251
+ # person.errors.to_hash(true) # => {:name=>["name cannot be nil"]}
252
252
  def to_hash(full_messages = false)
253
253
  if full_messages
254
254
  messages = {}
@@ -279,7 +279,7 @@ module ActiveModel
279
279
  # If +message+ is a proc, it will be called, allowing for things like
280
280
  # <tt>Time.now</tt> to be used within an error.
281
281
  #
282
- # If the <tt>:strict</tt> option is set to true will raise
282
+ # If the <tt>:strict</tt> option is set to +true+, it will raise
283
283
  # ActiveModel::StrictValidationFailed instead of adding the error.
284
284
  # <tt>:strict</tt> option can also be set to any other exception.
285
285
  #
@@ -13,7 +13,7 @@ module ActiveModel
13
13
  #
14
14
  # These tests do not attempt to determine the semantic correctness of the
15
15
  # returned values. For instance, you could implement <tt>valid?</tt> to
16
- # always return true, and the tests would pass. It is up to you to ensure
16
+ # always return +true+, and the tests would pass. It is up to you to ensure
17
17
  # that the values are semantically meaningful.
18
18
  #
19
19
  # Objects you pass in are expected to return a compliant object from a call
@@ -1,6 +1,6 @@
1
1
  module ActiveModel
2
2
 
3
- # == Active \Model Basic \Model
3
+ # == Active \Model \Basic \Model
4
4
  #
5
5
  # Includes the required interface for an object to interact with
6
6
  # <tt>ActionPack</tt>, using different <tt>ActiveModel</tt> modules.
@@ -9,7 +9,7 @@ module ActiveModel
9
9
 
10
10
  module ClassMethods
11
11
  # Adds methods to set and authenticate against a BCrypt password.
12
- # This mechanism requires you to have a password_digest attribute.
12
+ # This mechanism requires you to have a +password_digest+ attribute.
13
13
  #
14
14
  # Validations for presence of password on create, confirmation of password
15
15
  # (using a +password_confirmation+ attribute) are automatically added. If
@@ -57,11 +57,15 @@ module ActiveModel
57
57
  include InstanceMethodsOnActivation
58
58
 
59
59
  if options.fetch(:validations, true)
60
- validates_confirmation_of :password, if: :should_confirm_password?
61
- validates_presence_of :password, on: :create
62
- validates_presence_of :password_confirmation, if: :should_confirm_password?
60
+ # This ensures the model has a password by checking whether the password_digest
61
+ # is present, so that this works with both new and existing records. However,
62
+ # when there is an error, the message is added to the password attribute instead
63
+ # so that the error message will make sense to the end-user.
64
+ validate do |record|
65
+ record.errors.add(:password, :blank) unless record.password_digest.present?
66
+ end
63
67
 
64
- before_create { raise "Password digest missing on new record" if password_digest.blank? }
68
+ validates_confirmation_of :password, if: ->{ password.present? }
65
69
  end
66
70
 
67
71
  if respond_to?(:attributes_protected_by_default)
@@ -100,7 +104,9 @@ module ActiveModel
100
104
  # user.password = 'mUc3m00RsqyRe'
101
105
  # user.password_digest # => "$2a$10$4LEA7r4YmNHtvlAvHhsYAeZmk/xeUVtMTYqwIvYY76EW5GUqDiP4."
102
106
  def password=(unencrypted_password)
103
- unless unencrypted_password.blank?
107
+ if unencrypted_password.nil?
108
+ self.password_digest = nil
109
+ elsif unencrypted_password.present?
104
110
  @password = unencrypted_password
105
111
  cost = ActiveModel::SecurePassword.min_cost ? BCrypt::Engine::MIN_COST : BCrypt::Engine.cost
106
112
  self.password_digest = BCrypt::Password.create(unencrypted_password, cost: cost)
@@ -110,12 +116,6 @@ module ActiveModel
110
116
  def password_confirmation=(unencrypted_password)
111
117
  @password_confirmation = unencrypted_password
112
118
  end
113
-
114
- private
115
-
116
- def should_confirm_password?
117
- password_confirmation && password.present?
118
- end
119
119
  end
120
120
  end
121
121
  end
@@ -128,7 +128,7 @@ module ActiveModel
128
128
  # retrieve the value for a given attribute differently:
129
129
  #
130
130
  # class MyClass
131
- # include ActiveModel::Validations
131
+ # include ActiveModel::Serialization
132
132
  #
133
133
  # def initialize(data = {})
134
134
  # @data = data
@@ -2,7 +2,7 @@ require 'active_support/json'
2
2
 
3
3
  module ActiveModel
4
4
  module Serializers
5
- # == Active Model JSON Serializer
5
+ # == Active \Model \JSON \Serializer
6
6
  module JSON
7
7
  extend ActiveSupport::Concern
8
8
  include ActiveModel::Serialization
@@ -4,7 +4,7 @@ require 'active_support/core_ext/hash/except'
4
4
 
5
5
  module ActiveModel
6
6
 
7
- # == Active \Model Validations
7
+ # == Active \Model \Validations
8
8
  #
9
9
  # Provides a full validation framework to your objects.
10
10
  #
@@ -66,8 +66,10 @@ module ActiveModel
66
66
  # end
67
67
  #
68
68
  # Options:
69
- # * <tt>:on</tt> - Specifies the context where this validation is active
70
- # (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt>)
69
+ # * <tt>:on</tt> - Specifies the contexts where this validation is active.
70
+ # You can pass a symbol or an array of symbols.
71
+ # (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt> or
72
+ # <tt>on: [:create, :custom_validation_context]</tt>)
71
73
  # * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
72
74
  # * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
73
75
  # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
@@ -124,10 +126,10 @@ module ActiveModel
124
126
  # end
125
127
  #
126
128
  # Options:
127
- # * <tt>:on</tt> - Specifies the context where this validation is active
128
- # (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt>)
129
- # * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+.
130
- # * <tt>:allow_blank</tt> - Skip validation if attribute is blank.
129
+ # * <tt>:on</tt> - Specifies the contexts where this validation is active.
130
+ # You can pass a symbol or an array of symbols.
131
+ # (e.g. <tt>on: :create</tt> or <tt>on: :custom_validation_context</tt> or
132
+ # <tt>on: [:create, :custom_validation_context]</tt>)
131
133
  # * <tt>:if</tt> - Specifies a method, proc or string to call to determine
132
134
  # if the validation should occur (e.g. <tt>if: :allow_validation</tt>,
133
135
  # or <tt>if: Proc.new { |user| user.signup_step > 2 }</tt>). The method,
@@ -143,7 +145,7 @@ module ActiveModel
143
145
  options = options.dup
144
146
  options[:if] = Array(options[:if])
145
147
  options[:if].unshift lambda { |o|
146
- o.validation_context == options[:on]
148
+ Array(options[:on]).include?(o.validation_context)
147
149
  }
148
150
  end
149
151
  args << options
@@ -199,12 +201,12 @@ module ActiveModel
199
201
  # # #<StrictValidator:0x007fbff3204a30 @options={strict:true}>
200
202
  # # ]
201
203
  #
202
- # If one runs Person.clear_validators! and then checks to see what
204
+ # If one runs <tt>Person.clear_validators!</tt> and then checks to see what
203
205
  # validators this class has, you would obtain:
204
206
  #
205
207
  # Person.validators # => []
206
208
  #
207
- # Also, the callback set by +validate :cannot_be_robot+ will be erased
209
+ # Also, the callback set by <tt>validate :cannot_be_robot</tt> will be erased
208
210
  # so that:
209
211
  #
210
212
  # Person._validate_callbacks.empty? # => true
@@ -21,7 +21,7 @@ module ActiveModel
21
21
  # * <tt>:message</tt> - A custom error message (default is: "must be blank").
22
22
  #
23
23
  # There is also a list of default options supported by every validator:
24
- # +:if+, +:unless+, +:on+ and +:strict+.
24
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
25
25
  # See <tt>ActiveModel::Validation#validates</tt> for more information
26
26
  def validates_absence_of(*attr_names)
27
27
  validates_with AbsenceValidator, _merge_attributes(attr_names)
@@ -38,8 +38,6 @@ module ActiveModel
38
38
  # Configuration options:
39
39
  # * <tt>:message</tt> - A custom error message (default is: "must be
40
40
  # accepted").
41
- # * <tt>:allow_nil</tt> - Skip validation if attribute is +nil+ (default
42
- # is +true+).
43
41
  # * <tt>:accept</tt> - Specifies value that is considered accepted.
44
42
  # The default value is a string "1", which makes it easy to relate to
45
43
  # an HTML checkbox. This should be set to +true+ if you are validating
@@ -47,8 +45,8 @@ module ActiveModel
47
45
  # before validation.
48
46
  #
49
47
  # There is also a list of default options supported by every validator:
50
- # +:if+, +:unless+, +:on+ and +:strict+.
51
- # See <tt>ActiveModel::Validation#validates</tt> for more information
48
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
49
+ # See <tt>ActiveModel::Validation#validates</tt> for more information.
52
50
  def validates_acceptance_of(*attr_names)
53
51
  validates_with AcceptanceValidator, _merge_attributes(attr_names)
54
52
  end
@@ -1,6 +1,6 @@
1
1
  module ActiveModel
2
2
  module Validations
3
- # == Active \Model Validation Callbacks
3
+ # == Active \Model \Validation \Callbacks
4
4
  #
5
5
  # Provides an interface for any class to have +before_validation+ and
6
6
  # +after_validation+ callbacks.
@@ -57,7 +57,7 @@ module ActiveModel
57
57
  # confirmation").
58
58
  #
59
59
  # There is also a list of default options supported by every validator:
60
- # +:if+, +:unless+, +:on+ and +:strict+.
60
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
61
61
  # See <tt>ActiveModel::Validation#validates</tt> for more information
62
62
  def validates_confirmation_of(*attr_names)
63
63
  validates_with ConfirmationValidator, _merge_attributes(attr_names)
@@ -34,13 +34,9 @@ module ActiveModel
34
34
  # <tt>Range#cover?</tt>, otherwise with <tt>include?</tt>.
35
35
  # * <tt>:message</tt> - Specifies a custom error message (default is: "is
36
36
  # reserved").
37
- # * <tt>:allow_nil</tt> - If set to true, skips this validation if the
38
- # attribute is +nil+ (default is +false+).
39
- # * <tt>:allow_blank</tt> - If set to true, skips this validation if the
40
- # attribute is blank(default is +false+).
41
37
  #
42
38
  # There is also a list of default options supported by every validator:
43
- # +:if+, +:unless+, +:on+ and +:strict+.
39
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
44
40
  # See <tt>ActiveModel::Validation#validates</tt> for more information
45
41
  def validates_exclusion_of(*attr_names)
46
42
  validates_with ExclusionValidator, _merge_attributes(attr_names)
@@ -91,10 +91,6 @@ module ActiveModel
91
91
  #
92
92
  # Configuration options:
93
93
  # * <tt>:message</tt> - A custom error message (default is: "is invalid").
94
- # * <tt>:allow_nil</tt> - If set to true, skips this validation if the
95
- # attribute is +nil+ (default is +false+).
96
- # * <tt>:allow_blank</tt> - If set to true, skips this validation if the
97
- # attribute is blank (default is +false+).
98
94
  # * <tt>:with</tt> - Regular expression that if the attribute matches will
99
95
  # result in a successful validation. This can be provided as a proc or
100
96
  # lambda returning regular expression which will be called at runtime.
@@ -107,7 +103,7 @@ module ActiveModel
107
103
  # beginning or end of the string. These anchors are <tt>^</tt> and <tt>$</tt>.
108
104
  #
109
105
  # There is also a list of default options supported by every validator:
110
- # +:if+, +:unless+, +:on+ and +:strict+.
106
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
111
107
  # See <tt>ActiveModel::Validation#validates</tt> for more information
112
108
  def validates_format_of(*attr_names)
113
109
  validates_with FormatValidator, _merge_attributes(attr_names)
@@ -29,17 +29,14 @@ module ActiveModel
29
29
  # * <tt>:in</tt> - An enumerable object of available items. This can be
30
30
  # supplied as a proc, lambda or symbol which returns an enumerable. If the
31
31
  # enumerable is a numerical range the test is performed with <tt>Range#cover?</tt>,
32
- # otherwise with <tt>include?</tt>.
32
+ # otherwise with <tt>include?</tt>. When using a proc or lambda the instance
33
+ # under validation is passed as an argument.
33
34
  # * <tt>:within</tt> - A synonym(or alias) for <tt>:in</tt>
34
35
  # * <tt>:message</tt> - Specifies a custom error message (default is: "is
35
36
  # not included in the list").
36
- # * <tt>:allow_nil</tt> - If set to +true+, skips this validation if the
37
- # attribute is +nil+ (default is +false+).
38
- # * <tt>:allow_blank</tt> - If set to +true+, skips this validation if the
39
- # attribute is blank (default is +false+).
40
37
  #
41
38
  # There is also a list of default options supported by every validator:
42
- # +:if+, +:unless+, +:on+ and +:strict+.
39
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
43
40
  # See <tt>ActiveModel::Validation#validates</tt> for more information
44
41
  def validates_inclusion_of(*attr_names)
45
42
  validates_with InclusionValidator, _merge_attributes(attr_names)
@@ -1,6 +1,6 @@
1
1
  module ActiveModel
2
2
 
3
- # == Active \Model Length \Validator
3
+ # == Active \Model Length Validator
4
4
  module Validations
5
5
  class LengthValidator < EachValidator # :nodoc:
6
6
  MESSAGES = { is: :wrong_length, minimum: :too_short, maximum: :too_long }.freeze
@@ -110,7 +110,7 @@ module ActiveModel
110
110
  # * <tt>:even</tt> - Specifies the value must be an even number.
111
111
  #
112
112
  # There is also a list of default options supported by every validator:
113
- # +:if+, +:unless+, +:on+ and +:strict+ .
113
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+ .
114
114
  # See <tt>ActiveModel::Validation#validates</tt> for more information
115
115
  #
116
116
  # The following checks can also be supplied with a proc or a symbol which
@@ -29,7 +29,7 @@ module ActiveModel
29
29
  # * <tt>:message</tt> - A custom error message (default is: "can't be blank").
30
30
  #
31
31
  # There is also a list of default options supported by every validator:
32
- # +:if+, +:unless+, +:on+ and +:strict+.
32
+ # +:if+, +:unless+, +:on+, +:allow_nil+, +:allow_blank+, and +:strict+.
33
33
  # See <tt>ActiveModel::Validation#validates</tt> for more information
34
34
  def validates_presence_of(*attr_names)
35
35
  validates_with PresenceValidator, _merge_attributes(attr_names)
@@ -83,7 +83,9 @@ module ActiveModel
83
83
  # or <tt>unless: Proc.new { |user| user.signup_step <= 2 }</tt>). The
84
84
  # method, proc or string should return or evaluate to a +true+ or
85
85
  # +false+ value.
86
- # * <tt>:strict</tt> - if the <tt>:strict</tt> option is set to true
86
+ # * <tt>:allow_nil</tt> - Skip validation if the attribute is +nil+.
87
+ # * <tt>:allow_blank</tt> - Skip validation if the attribute is blank.
88
+ # * <tt>:strict</tt> - If the <tt>:strict</tt> option is set to true
87
89
  # will raise ActiveModel::StrictValidationFailed instead of adding the error.
88
90
  # <tt>:strict</tt> option can also be set to any other exception.
89
91
  #
@@ -109,7 +109,7 @@ module ActiveModel
109
109
  deprecated_setup(options)
110
110
  end
111
111
 
112
- # Return the kind for this validator.
112
+ # Returns the kind for this validator.
113
113
  #
114
114
  # PresenceValidator.new.kind # => :presence
115
115
  # UniquenessValidator.new.kind # => :uniqueness
@@ -1,7 +1,7 @@
1
1
  module ActiveModel
2
2
  # Returns the version of the currently loaded ActiveModel as a Gem::Version
3
3
  def self.version
4
- Gem::Version.new "4.1.0.beta2"
4
+ Gem::Version.new "4.1.0.rc1"
5
5
  end
6
6
 
7
7
  module VERSION #:nodoc:
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: activemodel
3
3
  version: !ruby/object:Gem::Version
4
- version: 4.1.0.beta2
4
+ version: 4.1.0.rc1
5
5
  platform: ruby
6
6
  authors:
7
7
  - David Heinemeier Hansson
@@ -16,14 +16,14 @@ dependencies:
16
16
  requirements:
17
17
  - - '='
18
18
  - !ruby/object:Gem::Version
19
- version: 4.1.0.beta2
19
+ version: 4.1.0.rc1
20
20
  type: :runtime
21
21
  prerelease: false
22
22
  version_requirements: !ruby/object:Gem::Requirement
23
23
  requirements:
24
24
  - - '='
25
25
  - !ruby/object:Gem::Version
26
- version: 4.1.0.beta2
26
+ version: 4.1.0.rc1
27
27
  - !ruby/object:Gem::Dependency
28
28
  name: builder
29
29
  requirement: !ruby/object:Gem::Requirement