specstar-models 0.0.8 → 0.0.9
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/specstar/models.rb +41 -7
- metadata +2 -2
data/lib/specstar/models.rb
CHANGED
@@ -7,6 +7,30 @@ module Specstar
|
|
7
7
|
" of " + hash.map { |key, value| "#{key} #{value}" }.to_sentence if hash.present?
|
8
8
|
end
|
9
9
|
|
10
|
+
def validate_presence_of_methods_in_options(model, options)
|
11
|
+
if options[:if] && options[:if].is_a?(Symbol)
|
12
|
+
return false unless model.respond_to? options[:if]
|
13
|
+
end
|
14
|
+
|
15
|
+
if options[:unless] && options[:unless].is_a?(Symbol)
|
16
|
+
return false unless model.respond_to? options[:unless]
|
17
|
+
end
|
18
|
+
|
19
|
+
true
|
20
|
+
end
|
21
|
+
|
22
|
+
def undefined_method_in_options(model, options)
|
23
|
+
if options[:if] && options[:if].is_a?(Symbol)
|
24
|
+
return options[:if] unless model.respond_to? options[:if]
|
25
|
+
end
|
26
|
+
|
27
|
+
if options[:unless] && options[:unless].is_a?(Symbol)
|
28
|
+
return options[:unless] unless model.respond_to? options[:unless]
|
29
|
+
end
|
30
|
+
|
31
|
+
nil
|
32
|
+
end
|
33
|
+
|
10
34
|
RSpec::Matchers.define :have_attribute do |attr|
|
11
35
|
attr = attr.to_s
|
12
36
|
|
@@ -56,15 +80,21 @@ module Specstar
|
|
56
80
|
model.class.reflect_on_all_associations.map { |a| a.name }.include? association.to_sym
|
57
81
|
end
|
58
82
|
|
59
|
-
RSpec::Matchers.define :validate_presence_of do |attr|
|
83
|
+
RSpec::Matchers.define :validate_presence_of do |attr, options|
|
60
84
|
match do |model|
|
61
85
|
(has_attribute?(model, attr) || has_association?(model, attr)) &&
|
62
|
-
model._validators[attr].select
|
86
|
+
model._validators[attr].select do |validator|
|
87
|
+
validator.instance_of?(ActiveModel::Validations::PresenceValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
|
88
|
+
end.size > 0
|
63
89
|
end
|
64
90
|
|
65
91
|
failure_message_for_should do |model|
|
66
92
|
if has_attribute?(model, attr) || has_association?(model, attr)
|
67
|
-
|
93
|
+
if options.nil? || validate_presence_of_methods_in_options(model, options)
|
94
|
+
"expected #{model.class} to validate presence of #{attr}."
|
95
|
+
else
|
96
|
+
"expected #{model.class} to define #{undefined_method_in_options(model, options)}."
|
97
|
+
end
|
68
98
|
else
|
69
99
|
"expected #{model.class} to have an attribute or association #{attr}."
|
70
100
|
end
|
@@ -73,14 +103,18 @@ module Specstar
|
|
73
103
|
|
74
104
|
RSpec::Matchers.define :validate_uniqueness_of do |attr, options|
|
75
105
|
match do |model|
|
76
|
-
(has_attribute?(model, attr) || has_association?(model, attr)) && model._validators[attr].select
|
77
|
-
|
78
|
-
|
106
|
+
(has_attribute?(model, attr) || has_association?(model, attr)) && model._validators[attr].select do |validator|
|
107
|
+
validator.instance_of?(ActiveRecord::Validations::UniquenessValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
|
108
|
+
end.size > 0
|
79
109
|
end
|
80
110
|
|
81
111
|
failure_message_for_should do |model|
|
82
112
|
if has_attribute?(model, attr) || has_association?(model, attr)
|
83
|
-
|
113
|
+
if options.nil? || validate_presence_of_methods_in_options(model, options)
|
114
|
+
"expected #{model.class} to validate uniqueness of #{attr}."
|
115
|
+
else
|
116
|
+
"expected #{model.class} to define #{undefined_method_in_options(model, options)}."
|
117
|
+
end
|
84
118
|
else
|
85
119
|
"expected #{model.class} to have an attribute or association #{attr}."
|
86
120
|
end
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: specstar-models
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.9
|
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-04-07 00:00:00.000000000 Z
|
13
13
|
dependencies:
|
14
14
|
- !ruby/object:Gem::Dependency
|
15
15
|
name: rspec-core
|