can_has_validations 0.4.2 → 0.4.3
Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml
CHANGED
@@ -1,15 +1,15 @@
|
|
1
1
|
---
|
2
2
|
!binary "U0hBMQ==":
|
3
3
|
metadata.gz: !binary |-
|
4
|
-
|
4
|
+
ZjBjODY1MTlkYjZlYzA1NmU4MWI3NWRlZWU2OWMxNzE2MDI1OTA2OA==
|
5
5
|
data.tar.gz: !binary |-
|
6
|
-
|
6
|
+
YzJiYzNiYmMyMjZmOWNlNjNlN2JkOTI2ODRhMWI5YmFlMjJhZTY0Yg==
|
7
7
|
SHA512:
|
8
8
|
metadata.gz: !binary |-
|
9
|
-
|
10
|
-
|
11
|
-
|
9
|
+
OWYzMDhjMTc3MjgxMjBhMDY4M2MwYTQ5ZTI1MjIzNmU4MTcxNjg1NGZhNWFm
|
10
|
+
ZjRmOWNlYjc4YTkxNjNmYThiZTkyODA4NzA0YTU4ZjhmOTVhYzhkM2Q1MWZh
|
11
|
+
ZGFhNTdmNThhMjZmMDkwMTcyY2FkYzRiYWM4YTMyOThjNmZjMWM=
|
12
12
|
data.tar.gz: !binary |-
|
13
|
-
|
14
|
-
|
15
|
-
|
13
|
+
M2ZlYTc5OWJjODNlMGEzNDZjZjBlMzk0ZGM0NzljYzA2NjZiODg3YTNiMWY2
|
14
|
+
OGIzOTAzY2Y1M2E5MTEwOWU1M2FmOTNiMjNlOTM4MTU3YzliYjVhMTYyY2U4
|
15
|
+
ZDliMTUzNzFjNmM3NTRmNjgxYmZlZTY5N2EyMzcxY2JhOTE3NzE=
|
@@ -2,7 +2,9 @@
|
|
2
2
|
# Allows a value to be set to a non-nil value once, and then makes it immutable.
|
3
3
|
# Combine with :existence=>true to accomplish the same thing as attr_readonly,
|
4
4
|
# except with error messages (instead of silently refusing to save the change).
|
5
|
-
# eg: validates :user_id, :
|
5
|
+
# eg: validates :user_id, write_once: true
|
6
|
+
# Optionally refuses changing from nil => non-nil, always making field immutable.
|
7
|
+
# eg: validates :source, write_once: {immutable_nil: true}
|
6
8
|
|
7
9
|
module ActiveModel::Validations
|
8
10
|
class WriteOnceValidator < ActiveModel::EachValidator
|
@@ -16,8 +18,10 @@ module ActiveModel::Validations
|
|
16
18
|
end
|
17
19
|
|
18
20
|
def validate_each(record, attribute, value)
|
19
|
-
if record.persisted? && record.send("#{attribute}_changed?")
|
20
|
-
record.
|
21
|
+
if record.persisted? && record.send("#{attribute}_changed?")
|
22
|
+
if options[:immutable_nil] || !record.send("#{attribute}_was").nil?
|
23
|
+
record.errors.add(attribute, :unchangeable, options)
|
24
|
+
end
|
21
25
|
end
|
22
26
|
end
|
23
27
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_has_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.4.
|
4
|
+
version: 0.4.3
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- thomas morgan
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2016-
|
11
|
+
date: 2016-08-26 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: rails
|
@@ -60,7 +60,6 @@ files:
|
|
60
60
|
- lib/can_has_validations/validators/existence_validator.rb
|
61
61
|
- lib/can_has_validations/validators/grandparent_validator.rb
|
62
62
|
- lib/can_has_validations/validators/ordering_validator.rb
|
63
|
-
- lib/can_has_validations/validators/safe_uniqueness_validator.rb
|
64
63
|
- lib/can_has_validations/validators/url_validator.rb
|
65
64
|
- lib/can_has_validations/validators/write_once_validator.rb
|
66
65
|
- lib/can_has_validations/version.rb
|
@@ -1,15 +0,0 @@
|
|
1
|
-
# better behaved version of the uniqueness validator
|
2
|
-
# Difference is it doesn't puke if the foreign_key is an invalid type (for
|
3
|
-
# example, an invalid string being provided for UUID type key). Instead,
|
4
|
-
# returns an error about being unable to find the key value.
|
5
|
-
# eg: validates :user_id, :safe_uniqueness=>true
|
6
|
-
|
7
|
-
class SafeUniquenessValidator < ActiveRecord::Validations::UniquenessValidator
|
8
|
-
def validate_each(record, attribute, value)
|
9
|
-
record.transaction(:requires_new=>true) do
|
10
|
-
super
|
11
|
-
end
|
12
|
-
rescue ActiveRecord::StatementInvalid
|
13
|
-
record.errors.add(attribute, "%{attribute} %{value} not found", options.except(:case_sensitive, :scope).merge(:value=>value))
|
14
|
-
end
|
15
|
-
end
|