optional_validations 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml ADDED
@@ -0,0 +1,7 @@
1
+ ---
2
+ SHA1:
3
+ metadata.gz: 35d13a5d2248f8840226fe6d2dc91c8779f424ad
4
+ data.tar.gz: ec08971b0a2a26653da3225e84fbb3d117c5adbb
5
+ SHA512:
6
+ metadata.gz: 9ac28392dda3e57513f0874111060299cc5ebb44028fafeba34633f214a936ec89fdcd9418cb8242badeb830515870527458fcc78c0b6fc49654ac5a9d46f8a0
7
+ data.tar.gz: 7e8804e4e6709a5fce67c414b9c36cec972c1bedc7236bc4d941cde47684399fc19da5168218ccd863aed8175dcf9707134a22fcba383d43e0f597f21d28fecb
data/Rakefile ADDED
@@ -0,0 +1,8 @@
1
+ require 'rake/testtask'
2
+
3
+ Rake::TestTask.new do |t|
4
+ t.libs << 'test'
5
+ end
6
+
7
+ desc 'Run tests'
8
+ task :default => :test
@@ -0,0 +1,51 @@
1
+ require 'active_support/all'
2
+ require 'active_model'
3
+
4
+
5
+ module ActiveModel
6
+ module Validations
7
+ module ClassMethods
8
+ alias_method :__validates_with, :validates_with
9
+ def validates_with(*args, &block)
10
+
11
+ _if = Array(args[1][:if])
12
+
13
+ # if multiple attribute names are supplied we need to split them into separate validates_with calls
14
+ # in order to be able to choose only relevant validations to certain fields
15
+ args[1][:attributes].each do |attr_name|
16
+ args[1][:attributes] = [attr_name]
17
+ args[1][:if] = _if.clone
18
+ args[1][:if].unshift -> do
19
+ if @__validate_only.present?
20
+ @__validate_only.include?(attr_name)
21
+ elsif @__validate_except.present?
22
+ ! @__validate_except.include?(attr_name)
23
+ else
24
+ true
25
+ end
26
+ end
27
+ __validates_with(*args, &block)
28
+ end
29
+ end
30
+ end
31
+
32
+ def validate_only(*fields)
33
+ @__validate_except = nil
34
+ @__validate_only = fields.map &:to_sym
35
+ true
36
+ end
37
+
38
+ def validate_except(*fields)
39
+ @__validate_only = nil
40
+ @__validate_except = fields.map &:to_sym
41
+ true
42
+ end
43
+
44
+ def validate_all
45
+ @__validate_only = nil
46
+ @__validate_except = nil
47
+ true
48
+ end
49
+
50
+ end
51
+ end
@@ -0,0 +1,171 @@
1
+ require 'minitest/autorun'
2
+ require 'optional_validations'
3
+
4
+ class ModelBase
5
+ include ActiveModel::Validations
6
+
7
+ attr_accessor :attr1, :attr2, :attr3
8
+
9
+ def test1?
10
+ true
11
+ end
12
+
13
+ def test2?
14
+ true
15
+ end
16
+
17
+ def self.name
18
+ 'ModelExample'
19
+ end
20
+
21
+ end
22
+
23
+
24
+ class OptionalValidationsTest < Minitest::Test
25
+ def test_valid_by_default
26
+ model = Class.new(ModelBase) do
27
+ # validates_presence_of :attr1
28
+ end
29
+ m = model.new
30
+ assert m.valid?
31
+ end
32
+
33
+
34
+ def test_basic_validation
35
+ model = Class.new(ModelBase) do
36
+ validates_presence_of :attr1
37
+ end
38
+ m = model.new
39
+ assert !m.valid?
40
+ end
41
+
42
+ def test_basic_validate_only
43
+ model = Class.new(ModelBase) do
44
+ validates_presence_of :attr1
45
+ end
46
+ m = model.new
47
+
48
+ m.validate_only :attr2
49
+ assert m.valid?
50
+
51
+ m.validate_only :attr1
52
+ assert !m.valid?
53
+ end
54
+
55
+ def test_basic_validate_except
56
+ model = Class.new(ModelBase) do
57
+ validates_presence_of :attr1
58
+ end
59
+ m = model.new
60
+
61
+ m.validate_except :attr2
62
+ assert !m.valid?
63
+
64
+ m.validate_except :attr1
65
+ assert m.valid?
66
+ end
67
+
68
+ def test_basic_validate_all
69
+ model = Class.new(ModelBase) do
70
+ validates_presence_of :attr1
71
+ end
72
+ m = model.new
73
+
74
+ m.validate_except :attr2
75
+ assert !m.valid?
76
+
77
+ m.validate_except :attr1
78
+ assert m.valid?
79
+
80
+ m.validate_all
81
+ assert !m.valid?
82
+ end
83
+
84
+
85
+ def test_multiple_fields
86
+ model = Class.new(ModelBase) do
87
+ validates_presence_of :attr1, :attr2
88
+ end
89
+ m = model.new
90
+
91
+ m.validate_only :attr1
92
+ assert !m.valid?
93
+
94
+ m.validate_only :attr2
95
+ assert !m.valid?
96
+
97
+ m.validate_except :attr1, :attr2
98
+ assert m.valid?
99
+
100
+ m.validate_only :attr3
101
+ assert m.valid?
102
+
103
+ m.attr1 = 1
104
+ m.validate_only :attr1
105
+ assert m.valid?
106
+
107
+ m.validate_only :attr2
108
+ assert !m.valid?
109
+ end
110
+
111
+ def test_conditional_validation
112
+ model = Class.new(ModelBase) do
113
+ validates_presence_of :attr1, :attr2, if: :test1?
114
+ end
115
+ m = model.new
116
+ assert !m.valid?
117
+
118
+ m.validate_except :attr1, :attr2
119
+ assert m.valid?
120
+
121
+ m.attr1 = 1
122
+ m.validate_only :attr1
123
+ assert m.valid?
124
+
125
+ m.validate_only :attr2
126
+ assert !m.valid?
127
+ end
128
+
129
+ def test_multi_conditional_validation
130
+ model = Class.new(ModelBase) do
131
+ validates_presence_of :attr1, :attr2, if: [:test1?, :test2?, -> { true }]
132
+ end
133
+ m = model.new
134
+ assert !m.valid?
135
+
136
+ m.validate_except :attr1, :attr2
137
+ assert m.valid?
138
+
139
+ m.attr1 = 1
140
+ m.validate_only :attr1
141
+ assert m.valid?
142
+
143
+ m.validate_only :attr2
144
+ assert !m.valid?
145
+ end
146
+
147
+ def test_multiple_validations
148
+ model = Class.new(ModelBase) do
149
+ validates_presence_of :attr1, :attr2
150
+ validates_numericality_of :attr2, :attr3
151
+ end
152
+ m = model.new
153
+ assert !m.valid?
154
+
155
+ m.attr1 = 'a'
156
+ m.attr2 = 1
157
+ m.validate_only :attr1, :attr2
158
+ assert m.valid?
159
+
160
+ m.validate_all
161
+ assert !m.valid?
162
+
163
+ m.validate_only :attr2
164
+ assert m.valid?
165
+ m.attr2 = 'a'
166
+ assert !m.valid?
167
+
168
+ end
169
+
170
+
171
+ end
metadata ADDED
@@ -0,0 +1,88 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: optional_validations
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ platform: ruby
6
+ authors:
7
+ - Ivan Brykov
8
+ autorequire:
9
+ bindir: bin
10
+ cert_chain: []
11
+ date: 2015-04-16 00:00:00.000000000 Z
12
+ dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: minitest
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '5'
20
+ type: :development
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '5'
27
+ - !ruby/object:Gem::Dependency
28
+ name: activesupport
29
+ requirement: !ruby/object:Gem::Requirement
30
+ requirements:
31
+ - - "~>"
32
+ - !ruby/object:Gem::Version
33
+ version: '4'
34
+ type: :runtime
35
+ prerelease: false
36
+ version_requirements: !ruby/object:Gem::Requirement
37
+ requirements:
38
+ - - "~>"
39
+ - !ruby/object:Gem::Version
40
+ version: '4'
41
+ - !ruby/object:Gem::Dependency
42
+ name: activemodel
43
+ requirement: !ruby/object:Gem::Requirement
44
+ requirements:
45
+ - - "~>"
46
+ - !ruby/object:Gem::Version
47
+ version: '4'
48
+ type: :runtime
49
+ prerelease: false
50
+ version_requirements: !ruby/object:Gem::Requirement
51
+ requirements:
52
+ - - "~>"
53
+ - !ruby/object:Gem::Version
54
+ version: '4'
55
+ description: Introduces new ActiveModel::Validations methods — validate_only and validate_except
56
+ email:
57
+ executables: []
58
+ extensions: []
59
+ extra_rdoc_files: []
60
+ files:
61
+ - Rakefile
62
+ - lib/optional_validations.rb
63
+ - test/test_optional_validations.rb
64
+ homepage: https://github.org/brykov/optional_validations
65
+ licenses:
66
+ - MIT
67
+ metadata: {}
68
+ post_install_message:
69
+ rdoc_options: []
70
+ require_paths:
71
+ - lib
72
+ required_ruby_version: !ruby/object:Gem::Requirement
73
+ requirements:
74
+ - - ">="
75
+ - !ruby/object:Gem::Version
76
+ version: '0'
77
+ required_rubygems_version: !ruby/object:Gem::Requirement
78
+ requirements:
79
+ - - ">="
80
+ - !ruby/object:Gem::Version
81
+ version: '0'
82
+ requirements: []
83
+ rubyforge_project:
84
+ rubygems_version: 2.4.5
85
+ signing_key:
86
+ specification_version: 4
87
+ summary: Provides ability to choose ActiveModel fields to be validated
88
+ test_files: []