sexy_validations 0.4.0 → 0.4.1

Sign up to get free protection for your applications and to get access to all the features.
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.0
1
+ 0.4.1
@@ -39,18 +39,21 @@ module SexyValidations
39
39
  end
40
40
 
41
41
  def validates(attribute = nil, validations = nil, &block)
42
- if validations
43
- condition = nil
42
+ condition = nil
44
43
 
44
+ if validations
45
45
  if validations[:if]
46
46
  condition = validations.delete(:if)
47
47
  end
48
48
 
49
49
  if validations[:unless]
50
+ raise ArgumentError, "if and unless condition given" if condition
50
51
  ref = validations.delete(:unless)
51
52
  condition = lambda { |model| !ref.call(model) }
52
53
  end
54
+ end
53
55
 
56
+ unless validations.blank?
54
57
  validations.each_pair do |validator, options|
55
58
  klass = load_validator(validator)
56
59
  self.validations << {
@@ -64,10 +67,13 @@ module SexyValidations
64
67
  if attribute
65
68
  self.validations << {
66
69
  :method => "#{attribute}_validation",
70
+ :condition => condition,
67
71
  }
68
72
  else
73
+ raise ArgumentError, "at least a block has to be given" unless block_given?
69
74
  self.validations << {
70
75
  :block => block,
76
+ :condition => condition,
71
77
  }
72
78
  end
73
79
  end
@@ -78,11 +84,12 @@ module SexyValidations
78
84
  def validate!
79
85
  errors.clear
80
86
  validations.each do |validation|
87
+ if validation[:condition]
88
+ next unless validation[:condition].call(self)
89
+ end
90
+
81
91
  case
82
92
  when validation[:validator]
83
- if validation[:condition]
84
- next unless validation[:condition].call(self)
85
- end
86
93
  if errors[validation[:attribute]].empty?
87
94
  validation[:validator].validate(self, validation[:attribute], send(validation[:attribute]), validation[:options])
88
95
  end
@@ -5,7 +5,7 @@
5
5
 
6
6
  Gem::Specification.new do |s|
7
7
  s.name = %q{sexy_validations}
8
- s.version = "0.4.0"
8
+ s.version = "0.4.1"
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"]
metadata CHANGED
@@ -5,8 +5,8 @@ version: !ruby/object:Gem::Version
5
5
  segments:
6
6
  - 0
7
7
  - 4
8
- - 0
9
- version: 0.4.0
8
+ - 1
9
+ version: 0.4.1
10
10
  platform: ruby
11
11
  authors:
12
12
  - Corin Langosch