specstar-models 0.2.9 → 0.2.10

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 0ecb4a4e5725085fc2189a9ae104e721c281c7d7
4
- data.tar.gz: 036857076591e0b6fda5434f152e7821a890342a
3
+ metadata.gz: fb5d80b65b01397c8136543df44ab5cdfd5837a7
4
+ data.tar.gz: 467969881bad160ded787df276a9fe94891d7b53
5
5
  SHA512:
6
- metadata.gz: 1bddfa50bc7d0ef05e325904eb01c64187f47d0297d1e7c6a8c3f6737b1f84125d7691b075d4f4e625a5037f4cf108a286580e14addf4886498c3f93bef6c2c3
7
- data.tar.gz: 2e4f77b556da6d2ce4fbd9893bf3f76ffd33bd7a8c7c4fd522091703b31f069efb382803247da7d0354258e9902532ab33b61f965dc8dec286885ba2626e74c7
6
+ metadata.gz: 8a933d6e82dc514ec4e750dcc78b44ebc39629a5bcd29048c6c00c0d62527a28154fb45cb0cfe867448c59d6581f2bfc97d6f4f87c2c3e269c2dbc18589979a2
7
+ data.tar.gz: cf819995ff2f9a87094b94afdf58d9c2cb2699f4047643510c77af70dc17af2f2a463778dca1ac759d05778e6393a9063b5aa2a608abb2a3f5dd04f4775bac8f
@@ -111,7 +111,7 @@ module Specstar
111
111
  match do |model|
112
112
  (has_attribute?(model, attr) || has_association?(model, attr)) &&
113
113
  model._validators[attr].select do |validator|
114
- validator.instance_of?(ActiveRecord::Validations::NumericalityValidator) && validator.options.merge(options) == validator.options
114
+ validator.instance_of?(ActiveModel::Validations::NumericalityValidator) && validator.options.merge(options) == validator.options
115
115
  end.size > 0
116
116
  end
117
117
 
@@ -0,0 +1,128 @@
1
+ require 'rspec/core'
2
+ require 'rspec/expectations'
3
+
4
+ module Specstar
5
+ module Models
6
+ module Matchers
7
+ def validate_presence_of_methods_in_options(model, options)
8
+ if options[:if] && options[:if].is_a?(Symbol)
9
+ return false unless model.respond_to? options[:if]
10
+ end
11
+
12
+ if options[:unless] && options[:unless].is_a?(Symbol)
13
+ return false unless model.respond_to? options[:unless]
14
+ end
15
+
16
+ true
17
+ end
18
+
19
+ def undefined_method_in_options(model, options)
20
+ if options[:if] && options[:if].is_a?(Symbol)
21
+ return options[:if] unless model.respond_to? options[:if]
22
+ end
23
+
24
+ if options[:unless] && options[:unless].is_a?(Symbol)
25
+ return options[:unless] unless model.respond_to? options[:unless]
26
+ end
27
+
28
+ nil
29
+ end
30
+
31
+ def has_attribute?(model, attr, extras={})
32
+ attr = attr.to_s
33
+
34
+ result = model.attributes.include? attr
35
+
36
+ if result && extras.any?
37
+ properties = model.class.columns_hash[attr]
38
+ extras.each_pair do |property, value|
39
+ result = false && break unless properties.send(property).to_s == value.to_s
40
+ end
41
+ end
42
+
43
+ result
44
+ end
45
+
46
+ def has_association?(model, association)
47
+ model.class.reflect_on_all_associations.map { |a| a.name.to_s }.include? association.to_s
48
+ end
49
+
50
+ RSpec::Matchers.define :validate_presence_of do |attr, options|
51
+ match do |model|
52
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
53
+ model._validators[attr.to_sym].select do |validator|
54
+ validator.instance_of?(ActiveRecord::Validations::PresenceValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
55
+ end.size > 0
56
+ end
57
+
58
+ failure_message do |model|
59
+ if has_attribute?(model, attr) || has_association?(model, attr)
60
+ if options.nil? || validate_presence_of_methods_in_options(model, options)
61
+ "expected #{model.class} to validate presence of #{attr}."
62
+ else
63
+ "expected #{model.class} to define #{undefined_method_in_options(model, options)}."
64
+ end
65
+ else
66
+ "expected #{model.class} to have an attribute or association #{attr}."
67
+ end
68
+ end
69
+ end
70
+
71
+ RSpec::Matchers.define :validate_uniqueness_of do |attr, options|
72
+ match do |model|
73
+ (has_attribute?(model, attr) || has_association?(model, attr)) && model._validators[attr].select do |validator|
74
+ validator.instance_of?(ActiveRecord::Validations::UniquenessValidator) && (options.nil? || validate_presence_of_methods_in_options(model, options) && (options.to_a - validator.options.to_a).empty?)
75
+ end.size > 0
76
+ end
77
+
78
+ failure_message do |model|
79
+ if has_attribute?(model, attr) || has_association?(model, attr)
80
+ if options.nil? || validate_presence_of_methods_in_options(model, options)
81
+ "expected #{model.class} to validate uniqueness of #{attr}."
82
+ else
83
+ "expected #{model.class} to define #{undefined_method_in_options(model, options)}."
84
+ end
85
+ else
86
+ "expected #{model.class} to have an attribute or association #{attr}."
87
+ end
88
+ end
89
+ end
90
+
91
+ RSpec::Matchers.define :validate_inclusion_of do |attr, options|
92
+ match do |model|
93
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
94
+ model._validators[attr].select do |validator|
95
+ validator.instance_of?(ActiveModel::Validations::InclusionValidator) && validator.options.merge(options) == validator.options
96
+ end.size > 0
97
+ end
98
+
99
+ failure_message do |model|
100
+ if has_attribute?(model, attr) || has_association?(model, attr)
101
+ "expected #{model.class} to validate inclusion of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
102
+ else
103
+ "expected #{model.class} to have an attribute or association #{attr}."
104
+ end
105
+ end
106
+ end
107
+
108
+ RSpec::Matchers.define :validate_numericality_of do |attr, options|
109
+ options = options || {}
110
+
111
+ match do |model|
112
+ (has_attribute?(model, attr) || has_association?(model, attr)) &&
113
+ model._validators[attr].select do |validator|
114
+ validator.instance_of?(ActiveRecord::Validations::NumericalityValidator) && validator.options.merge(options) == validator.options
115
+ end.size > 0
116
+ end
117
+
118
+ failure_message do |model|
119
+ if has_attribute?(model, attr) || has_association?(model, attr)
120
+ "expected #{model.class} to validate numericality of #{attr} in [#{options.delete(:in).map(&:to_s).join(', ')}]."
121
+ else
122
+ "expected #{model.class} to have an attribute or association #{attr}."
123
+ end
124
+ end
125
+ end
126
+ end
127
+ end
128
+ end
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: specstar-models
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.9
4
+ version: 0.2.10
5
5
  platform: ruby
6
6
  authors:
7
7
  - Sujoy Gupta
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2015-09-29 00:00:00.000000000 Z
11
+ date: 2016-01-11 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: rspec-core
@@ -49,6 +49,7 @@ files:
49
49
  - lib/specstar/models/associations.rb
50
50
  - lib/specstar/models/attributes.rb
51
51
  - lib/specstar/models/validations.rb
52
+ - lib/specstar/models/validations.rb~
52
53
  homepage: http://github.com/sujoyg/specstar-models
53
54
  licenses:
54
55
  - MIT
@@ -69,7 +70,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
69
70
  version: '0'
70
71
  requirements: []
71
72
  rubyforge_project:
72
- rubygems_version: 2.4.6
73
+ rubygems_version: 2.4.8
73
74
  signing_key:
74
75
  specification_version: 4
75
76
  summary: RSpec helpers for models.