can_has_validations 0.3.0 → 0.3.1
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.
data/README.md
CHANGED
@@ -153,7 +153,7 @@ created and allow it to be set once at some arbitrary point in the future, but
|
|
153
153
|
once set, still make it immutable. A WORM (write once, read many) attribute of
|
154
154
|
sorts.
|
155
155
|
|
156
|
-
validates :user_id, write_once: true
|
156
|
+
validates :user_id, allow_nil: true, write_once: true
|
157
157
|
|
158
158
|
|
159
159
|
## Error messages
|
@@ -173,4 +173,4 @@ Default messages are as follows:
|
|
173
173
|
|
174
174
|
## Compatibility ##
|
175
175
|
|
176
|
-
Tested with Ruby 1.9 and ActiveSupport and ActiveModel
|
176
|
+
Tested with Ruby 1.9 and ActiveSupport and ActiveModel 4.0.0.
|
@@ -1,11 +1,20 @@
|
|
1
1
|
# write-once, read-many
|
2
2
|
# Allows a value to be set to a non-nil value once, and then makes it immutable.
|
3
|
-
# Combine with :
|
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
5
|
# eg: validates :user_id, :write_once=>true
|
6
6
|
|
7
7
|
module ActiveModel::Validations
|
8
8
|
class WriteOnceValidator < ActiveModel::EachValidator
|
9
|
+
# as of ActiveModel 4, :allow_nil=>true causes a change from a value back to
|
10
|
+
# nil to be allowed. prevent this.
|
11
|
+
def validate(record)
|
12
|
+
attributes.each do |attribute|
|
13
|
+
value = record.read_attribute_for_validation(attribute)
|
14
|
+
validate_each(record, attribute, value)
|
15
|
+
end
|
16
|
+
end
|
17
|
+
|
9
18
|
def validate_each(record, attribute, value)
|
10
19
|
if record.persisted? && record.send("#{attribute}_changed?") && !record.send("#{attribute}_was").nil?
|
11
20
|
record.errors.add(attribute, :unchangeable, options)
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: can_has_validations
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.3.
|
4
|
+
version: 0.3.1
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -9,7 +9,7 @@ authors:
|
|
9
9
|
autorequire:
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
|
-
date: 2013-
|
12
|
+
date: 2013-10-10 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rails
|
@@ -114,7 +114,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
114
114
|
version: '0'
|
115
115
|
segments:
|
116
116
|
- 0
|
117
|
-
hash:
|
117
|
+
hash: 2495871614076405699
|
118
118
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
119
119
|
none: false
|
120
120
|
requirements:
|
@@ -123,7 +123,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
123
123
|
version: '0'
|
124
124
|
segments:
|
125
125
|
- 0
|
126
|
-
hash:
|
126
|
+
hash: 2495871614076405699
|
127
127
|
requirements: []
|
128
128
|
rubyforge_project:
|
129
129
|
rubygems_version: 1.8.25
|