sexy_validations 0.4.1 → 0.4.2

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/VERSION CHANGED
@@ -1 +1 @@
1
- 0.4.1
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
- real_value = case value.class.name
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
@@ -39,18 +39,22 @@ module SexyValidations
39
39
  end
40
40
 
41
41
  def validates(attribute = nil, validations = nil, &block)
42
- condition = nil
42
+ conditions = nil
43
+ conditions_if = nil
43
44
 
44
45
  if validations
45
46
  if validations[:if]
46
- condition = validations.delete(:if)
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 condition given" if condition
51
- ref = validations.delete(:unless)
52
- condition = lambda { |model| !ref.call(model) }
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
- :condition => condition,
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
- :condition => condition,
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
- :condition => condition,
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
- if validation[:condition]
88
- next unless validation[:condition].call(self)
89
- end
90
-
91
- case
92
- when validation[:validator]
93
- if errors[validation[:attribute]].empty?
94
- validation[:validator].validate(self, validation[:attribute], send(validation[:attribute]), validation[:options])
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
- when validation[:method]
97
- send(validation[:method])
98
- when validation[:block]
99
- validation[:block].call
100
- else
101
- raise ArgumentError, "invalid validation (internal error)"
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
@@ -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.1"
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-28}
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
- - 1
9
- version: 0.4.1
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-28 00:00:00 +01:00
17
+ date: 2010-09-29 00:00:00 +01:00
18
18
  default_executable:
19
19
  dependencies:
20
20
  - !ruby/object:Gem::Dependency