sexy_validations 0.4.1 → 0.4.2
Sign up to get free protection for your applications and to get access to all the features.
- data/VERSION +1 -1
- data/lib/sexy_validations/validators/presence.rb +1 -8
- data/lib/sexy_validations.rb +38 -22
- data/sexy_validations.gemspec +2 -2
- metadata +3 -3
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.4.
|
1
|
+
0.4.2
|
@@ -3,14 +3,7 @@ module SexyValidations
|
|
3
3
|
module Validators
|
4
4
|
class Presence
|
5
5
|
def self.validate(model, attribute, value, options)
|
6
|
-
|
7
|
-
when "Sequel::Plugins::Paperclip::Attachment"
|
8
|
-
value.queued_file
|
9
|
-
else
|
10
|
-
value
|
11
|
-
end
|
12
|
-
|
13
|
-
return unless real_value.blank?
|
6
|
+
return unless value.blank?
|
14
7
|
|
15
8
|
model.errors.add(attribute, "muss ausgefüllt werden")
|
16
9
|
end
|
data/lib/sexy_validations.rb
CHANGED
@@ -39,18 +39,22 @@ module SexyValidations
|
|
39
39
|
end
|
40
40
|
|
41
41
|
def validates(attribute = nil, validations = nil, &block)
|
42
|
-
|
42
|
+
conditions = nil
|
43
|
+
conditions_if = nil
|
43
44
|
|
44
45
|
if validations
|
45
46
|
if validations[:if]
|
46
|
-
|
47
|
+
conditions = validations.delete(:if)
|
48
|
+
conditions_if = true
|
47
49
|
end
|
48
50
|
|
49
51
|
if validations[:unless]
|
50
|
-
raise ArgumentError, "if and unless
|
51
|
-
|
52
|
-
|
52
|
+
raise ArgumentError, "if and unless conditions given" if condition
|
53
|
+
conditions = validations.delete(:unless)
|
54
|
+
conditions_if = false
|
53
55
|
end
|
56
|
+
|
57
|
+
conditions = [conditions] if conditions && !conditions.is_a?(Array)
|
54
58
|
end
|
55
59
|
|
56
60
|
unless validations.blank?
|
@@ -60,20 +64,23 @@ module SexyValidations
|
|
60
64
|
:attribute => attribute,
|
61
65
|
:validator => klass,
|
62
66
|
:options => options,
|
63
|
-
:
|
67
|
+
:conditions => conditions,
|
68
|
+
:conditions_if => conditions_if,
|
64
69
|
}
|
65
70
|
end
|
66
71
|
else
|
67
72
|
if attribute
|
68
73
|
self.validations << {
|
69
74
|
:method => "#{attribute}_validation",
|
70
|
-
:
|
75
|
+
:conditions => conditions,
|
76
|
+
:conditions_if => conditions_if,
|
71
77
|
}
|
72
78
|
else
|
73
79
|
raise ArgumentError, "at least a block has to be given" unless block_given?
|
74
80
|
self.validations << {
|
75
81
|
:block => block,
|
76
|
-
:
|
82
|
+
:conditions => conditions,
|
83
|
+
:conditions_if => conditions_if,
|
77
84
|
}
|
78
85
|
end
|
79
86
|
end
|
@@ -84,21 +91,30 @@ module SexyValidations
|
|
84
91
|
def validate!
|
85
92
|
errors.clear
|
86
93
|
validations.each do |validation|
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
94
|
+
catch (:skip_validation) do
|
95
|
+
if validation[:conditions]
|
96
|
+
validation[:conditions].each do |condition|
|
97
|
+
success = condition.call(self, validation[:attribute])
|
98
|
+
if validation[:conditions_if]
|
99
|
+
throw :skip_validation unless success
|
100
|
+
else
|
101
|
+
throw :skip_validation if success
|
102
|
+
end
|
95
103
|
end
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
validation[:
|
100
|
-
|
101
|
-
|
104
|
+
end
|
105
|
+
|
106
|
+
case
|
107
|
+
when validation[:validator]
|
108
|
+
if errors[validation[:attribute]].empty?
|
109
|
+
validation[:validator].validate(self, validation[:attribute], send(validation[:attribute]), validation[:options])
|
110
|
+
end
|
111
|
+
when validation[:method]
|
112
|
+
send(validation[:method])
|
113
|
+
when validation[:block]
|
114
|
+
validation[:block].call
|
115
|
+
else
|
116
|
+
raise ArgumentError, "invalid validation (internal error)"
|
117
|
+
end
|
102
118
|
end
|
103
119
|
end
|
104
120
|
end
|
data/sexy_validations.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = %q{sexy_validations}
|
8
|
-
s.version = "0.4.
|
8
|
+
s.version = "0.4.2"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Corin Langosch"]
|
12
|
-
s.date = %q{2010-09-
|
12
|
+
s.date = %q{2010-09-29}
|
13
13
|
s.description = %q{Module which provides sexy validations for models.}
|
14
14
|
s.email = %q{info@netskin.com}
|
15
15
|
s.extra_rdoc_files = [
|
metadata
CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
|
|
5
5
|
segments:
|
6
6
|
- 0
|
7
7
|
- 4
|
8
|
-
-
|
9
|
-
version: 0.4.
|
8
|
+
- 2
|
9
|
+
version: 0.4.2
|
10
10
|
platform: ruby
|
11
11
|
authors:
|
12
12
|
- Corin Langosch
|
@@ -14,7 +14,7 @@ autorequire:
|
|
14
14
|
bindir: bin
|
15
15
|
cert_chain: []
|
16
16
|
|
17
|
-
date: 2010-09-
|
17
|
+
date: 2010-09-29 00:00:00 +01:00
|
18
18
|
default_executable:
|
19
19
|
dependencies:
|
20
20
|
- !ruby/object:Gem::Dependency
|