sexy_validators 0.0.1 → 0.0.5
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/lib/change_validator.rb +22 -0
- data/lib/sexy_validators.rb +3 -1
- data/lib/sexy_validators/version.rb +1 -1
- data/lib/slug_validator.rb +7 -0
- data/sexy_validators.gemspec +2 -1
- metadata +20 -2
@@ -0,0 +1,22 @@
|
|
1
|
+
class ChangeValidator < ActiveModel::EachValidator
|
2
|
+
|
3
|
+
# Enforce/prevent attribute change
|
4
|
+
#
|
5
|
+
# Example: Make attribute immutable once saved
|
6
|
+
# validates :attribute, change: false, on: :update
|
7
|
+
#
|
8
|
+
# Example: Force attribute change on every save
|
9
|
+
# validates :attribute, change: true
|
10
|
+
|
11
|
+
def initialize(options)
|
12
|
+
options[:change] = !(options[:with] === false)
|
13
|
+
super
|
14
|
+
end
|
15
|
+
|
16
|
+
def validate_each(record, attribute, value)
|
17
|
+
unless record.public_send(:"#{attribute}_changed?") == options[:change]
|
18
|
+
record.errors[attribute] << ("#{options[:change] ? 'must' : 'cannot'} be modified")
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
data/lib/sexy_validators.rb
CHANGED
@@ -0,0 +1,7 @@
|
|
1
|
+
class SlugValidator < ActiveModel::EachValidator
|
2
|
+
def validate_each(object, attribute, value)
|
3
|
+
unless value =~ /^\D(\w*-*)*$/
|
4
|
+
object.errors[attribute] << (options[:message] || "is not formatted properly (should contain only letters, digits, dashes and underscores and can't start with digit)")
|
5
|
+
end
|
6
|
+
end
|
7
|
+
end
|
data/sexy_validators.gemspec
CHANGED
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: sexy_validators
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.5
|
5
5
|
prerelease:
|
6
6
|
platform: ruby
|
7
7
|
authors:
|
@@ -10,7 +10,23 @@ autorequire:
|
|
10
10
|
bindir: bin
|
11
11
|
cert_chain: []
|
12
12
|
date: 2012-11-03 00:00:00.000000000 Z
|
13
|
-
dependencies:
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: activemodel
|
16
|
+
requirement: !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ! '>='
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: !ruby/object:Gem::Requirement
|
25
|
+
none: false
|
26
|
+
requirements:
|
27
|
+
- - ! '>='
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '0'
|
14
30
|
description: Use it as your validators.
|
15
31
|
email:
|
16
32
|
- maks.rydkin@gmail.com
|
@@ -23,8 +39,10 @@ files:
|
|
23
39
|
- LICENSE
|
24
40
|
- README.md
|
25
41
|
- Rakefile
|
42
|
+
- lib/change_validator.rb
|
26
43
|
- lib/sexy_validators.rb
|
27
44
|
- lib/sexy_validators/version.rb
|
45
|
+
- lib/slug_validator.rb
|
28
46
|
- sexy_validators.gemspec
|
29
47
|
homepage: ''
|
30
48
|
licenses: []
|