dynamic_attribute_declaration 0.0.10 → 0.0.11
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.
- checksums.yaml +4 -4
- data/dynamic_attribute_declaration.gemspec +7 -7
- data/lib/dynamic_attribute_declaration.rb +4 -4
- data/lib/dynamic_attribute_declaration/version.rb +1 -1
- data/spec/lib/base_spec.rb +155 -0
- data/spec/lib/dynamic_attribute_declaration_spec.rb +253 -215
- data/spec/lib/multiple_spec.rb +23 -0
- data/spec/support/schema.rb +1 -0
- metadata +28 -24
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 5b76e3142d2eed5bc086805161e9bc55595999f3
|
4
|
+
data.tar.gz: da66b9d1a260a72a7ef735d1eb8453334a1df032
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 7be553e1972b3c823bc46e808f70474d88cddf96d86a421842577c0d47634db2307202ecdc6e3e0cfff0ef1003d183647abd660c6bd9d54e91c1b6b63db44c6d
|
7
|
+
data.tar.gz: 1c61921ef47b63b42135b87be4f54eb1d88cb30d1ac79f980df6812edb0470124723a5590610d04dc212e90f70ae62fdaade207b61a7d2a751574e9ccaf02467
|
@@ -9,8 +9,8 @@ Gem::Specification.new do |spec|
|
|
9
9
|
spec.authors = ["Mikkel Wied Frederiksen"]
|
10
10
|
spec.email = ["mikkel@wied.cc"]
|
11
11
|
spec.summary = %q{This gem lets you dynamically declare validations, that can function as partly applied validation, based on some kind of model instance state.}
|
12
|
-
|
13
|
-
spec.homepage = ""
|
12
|
+
spec.description = %q{DESCRIPTION ON THE WAY! :)}
|
13
|
+
spec.homepage = "https://github.com/mikkelwf/dynamic_attribute_declaration"
|
14
14
|
spec.license = "MIT"
|
15
15
|
|
16
16
|
spec.files = `git ls-files -z`.split("\x0")
|
@@ -19,11 +19,11 @@ Gem::Specification.new do |spec|
|
|
19
19
|
spec.require_paths = ["lib"]
|
20
20
|
|
21
21
|
spec.add_development_dependency "bundler", "~> 1.6"
|
22
|
-
spec.add_development_dependency "rake"
|
23
|
-
spec.add_development_dependency "rspec"
|
24
|
-
spec.add_development_dependency "rspec-its"
|
25
|
-
spec.add_development_dependency "sqlite3"
|
26
|
-
spec.add_development_dependency
|
22
|
+
spec.add_development_dependency "rake", "~> 10"
|
23
|
+
spec.add_development_dependency "rspec", "~> 3"
|
24
|
+
spec.add_development_dependency "rspec-its", "~> 1"
|
25
|
+
spec.add_development_dependency "sqlite3", "~> 1.3"
|
26
|
+
spec.add_development_dependency "database_cleaner", "~> 1.3"
|
27
27
|
|
28
28
|
spec.add_dependency "activerecord", "~> 4.0"
|
29
29
|
spec.add_dependency "activesupport", "~> 4.0"
|
@@ -65,21 +65,21 @@ module DynamicAttributeDeclaration
|
|
65
65
|
# throw "No validation state if defined" unless _rdynamic_attr_state_if
|
66
66
|
attrs.each do |key, val|
|
67
67
|
if val.key?(:validates) && !val[:validates].empty?
|
68
|
-
opts = val[:validates]
|
68
|
+
opts = val[:validates].deep_symbolize_keys
|
69
69
|
|
70
70
|
# Check if validation should only be used in specific state
|
71
71
|
if val.key?(:on) && _dynamic_attr_state_if && _dynamic_attr_state_if.class == Proc
|
72
72
|
validates_on = val[:on]
|
73
73
|
# If validates contains if statement, wrap that statement in state check
|
74
|
-
if
|
75
|
-
original_if =
|
74
|
+
if opts.key?(:if)
|
75
|
+
original_if = opts.delete(:if)
|
76
76
|
opts.merge! if: ->(model) { model.instance_exec(validates_on, &_dynamic_attr_state_if) && model.instance_eval(&original_if) }
|
77
77
|
else
|
78
78
|
opts.merge! if: ->(model) { model.instance_exec(validates_on, &_dynamic_attr_state_if) }
|
79
79
|
end
|
80
80
|
end
|
81
81
|
|
82
|
-
validates key.to_sym, opts
|
82
|
+
validates key.to_sym, opts
|
83
83
|
end
|
84
84
|
end
|
85
85
|
end
|
@@ -0,0 +1,155 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "BASE TESTS" do
|
4
|
+
|
5
|
+
let(:cls) { Phone }
|
6
|
+
|
7
|
+
describe "Basic gem tests" do
|
8
|
+
|
9
|
+
it "Accessor _dynamic_attrs should be Hash" do
|
10
|
+
expect(cls._dynamic_attrs.class).to eq Hash
|
11
|
+
end
|
12
|
+
|
13
|
+
%w(define_attrs attrs_for attrs_names_for build_validations_from_dynamic_attrs).each do |attr|
|
14
|
+
it "Should respond to #{attr}" do
|
15
|
+
expect(cls).to respond_to(attr.to_sym)
|
16
|
+
end
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
describe "Defining attrs" do
|
21
|
+
|
22
|
+
it "Clean Model should have no validators" do
|
23
|
+
expect(cls.validators).to eq []
|
24
|
+
end
|
25
|
+
|
26
|
+
describe "Simple definition of presence validator" do
|
27
|
+
let(:defition_array) { [{name:{validates:{presence: true}}}] }
|
28
|
+
let(:definition) { Hash[*defition_array.flatten] }
|
29
|
+
|
30
|
+
before do
|
31
|
+
cls.define_attrs definition
|
32
|
+
end
|
33
|
+
|
34
|
+
it "Should have at least one validator" do
|
35
|
+
expect(cls.validators).not_to eq []
|
36
|
+
expect(cls.validators.length).to be >= 1
|
37
|
+
end
|
38
|
+
|
39
|
+
it "Should have a specific validator" do
|
40
|
+
obj = definition
|
41
|
+
name = obj.keys.first
|
42
|
+
validators = obj[name][:validates]
|
43
|
+
validator_type = validators.keys.first
|
44
|
+
|
45
|
+
validator = cls.validators.first
|
46
|
+
expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
|
47
|
+
end
|
48
|
+
|
49
|
+
it "_dynamic_attrs should have configuration from define_attrs" do
|
50
|
+
expect(cls._dynamic_attrs).to eq definition
|
51
|
+
end
|
52
|
+
|
53
|
+
it "attrs_for with no state should return definition" do
|
54
|
+
expect(cls.attrs_for).to eq definition
|
55
|
+
end
|
56
|
+
|
57
|
+
it "attrs_names_for with no state should return definition" do
|
58
|
+
expect(cls.attrs_names_for).to eq definition.keys
|
59
|
+
end
|
60
|
+
end
|
61
|
+
|
62
|
+
describe "Simple definition of presence validator, with on parameter" do
|
63
|
+
let(:defition_array) { [{name:{validates:{presence: true}, on: :right}}] }
|
64
|
+
let(:definition) { Hash[*defition_array.flatten] }
|
65
|
+
let(:instance) { cls.new }
|
66
|
+
|
67
|
+
before do
|
68
|
+
cls.define_attrs definition
|
69
|
+
end
|
70
|
+
|
71
|
+
it "Should have at least one validator" do
|
72
|
+
expect(cls.validators).not_to eq []
|
73
|
+
expect(cls.validators.length).to be >= 1
|
74
|
+
end
|
75
|
+
|
76
|
+
it "Should have a specific validator" do
|
77
|
+
obj = definition
|
78
|
+
name = obj.keys.first
|
79
|
+
validators = obj[name][:validates]
|
80
|
+
validator_type = validators.keys.first
|
81
|
+
|
82
|
+
validator = cls.validators.first
|
83
|
+
expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
|
84
|
+
end
|
85
|
+
|
86
|
+
it "_dynamic_attrs should have configuration from define_attrs" do
|
87
|
+
expect(cls._dynamic_attrs).to eq definition
|
88
|
+
end
|
89
|
+
|
90
|
+
describe "attrs_for" do
|
91
|
+
it "with no state should return definition" do
|
92
|
+
expect(cls.attrs_for).to eq definition
|
93
|
+
end
|
94
|
+
describe "with right state" do
|
95
|
+
it "with symbol should return definition" do
|
96
|
+
expect(cls.attrs_for(:right)).to eq definition
|
97
|
+
end
|
98
|
+
it "with string should return definition" do
|
99
|
+
expect(cls.attrs_for("right")).to eq definition
|
100
|
+
end
|
101
|
+
end
|
102
|
+
describe "with wrong state" do
|
103
|
+
it "with symbol should return empty object" do
|
104
|
+
expect(cls.attrs_for(:wrong)).to eq({})
|
105
|
+
end
|
106
|
+
it "with string should return empty object" do
|
107
|
+
expect(cls.attrs_for("wrong")).to eq({})
|
108
|
+
end
|
109
|
+
end
|
110
|
+
end
|
111
|
+
|
112
|
+
describe "attrs_names_for" do
|
113
|
+
it "with no state should return definition keys" do
|
114
|
+
expect(cls.attrs_names_for).to eq definition.keys
|
115
|
+
end
|
116
|
+
describe "with right state" do
|
117
|
+
it "with symbol should return definition keys" do
|
118
|
+
expect(cls.attrs_names_for(:right)).to eq definition.keys
|
119
|
+
end
|
120
|
+
it "with string should return definition keys" do
|
121
|
+
expect(cls.attrs_names_for("right")).to eq definition.keys
|
122
|
+
end
|
123
|
+
end
|
124
|
+
describe "with wrong state" do
|
125
|
+
it "with symbol should return empty array" do
|
126
|
+
expect(cls.attrs_names_for(:wrong)).to eq []
|
127
|
+
end
|
128
|
+
it "with string should return empty array" do
|
129
|
+
expect(cls.attrs_names_for("wrong")).to eq []
|
130
|
+
end
|
131
|
+
end
|
132
|
+
end
|
133
|
+
|
134
|
+
describe "values_for" do
|
135
|
+
pending "TEST VALUES FOR"
|
136
|
+
end
|
137
|
+
end
|
138
|
+
end
|
139
|
+
|
140
|
+
describe "Defining define_attr_state_if" do
|
141
|
+
before do
|
142
|
+
cls.clear_dynamic_attrs!
|
143
|
+
end
|
144
|
+
|
145
|
+
it "Should have no _dynamic_attr_state_if as standard" do
|
146
|
+
expect(cls._dynamic_attr_state_if).to be_nil
|
147
|
+
end
|
148
|
+
|
149
|
+
it "lala" do
|
150
|
+
proc = Proc.new { true }
|
151
|
+
cls.define_attr_state_if proc
|
152
|
+
expect(cls._dynamic_attr_state_if).to eq proc
|
153
|
+
end
|
154
|
+
end
|
155
|
+
end
|
@@ -1,266 +1,304 @@
|
|
1
1
|
require 'spec_helper'
|
2
2
|
|
3
|
-
describe "
|
4
|
-
|
3
|
+
describe "MODEL INSTANCE TESTS" do
|
5
4
|
let(:cls) { Phone }
|
6
5
|
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
expect(cls._dynamic_attrs.class).to eq Hash
|
11
|
-
end
|
6
|
+
before do
|
7
|
+
cls.clear_validators!
|
8
|
+
cls.clear_dynamic_attrs!
|
12
9
|
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
end
|
17
|
-
end
|
10
|
+
cls.define_attr_state_if Proc.new { |value|
|
11
|
+
self.validator value
|
12
|
+
}
|
18
13
|
end
|
19
14
|
|
20
|
-
|
21
|
-
|
22
|
-
it "
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
let(:defition_array) { [{name:{validates:{presence: true}}}] }
|
28
|
-
let(:definition) { Hash[*defition_array.flatten] }
|
29
|
-
|
30
|
-
before do
|
31
|
-
cls.define_attrs definition
|
32
|
-
end
|
33
|
-
|
34
|
-
it "Should have at least one validator" do
|
35
|
-
expect(cls.validators).not_to eq []
|
36
|
-
expect(cls.validators.length).to be >= 1
|
37
|
-
end
|
38
|
-
|
39
|
-
it "Should have a specific validator" do
|
40
|
-
obj = definition
|
41
|
-
name = obj.keys.first
|
42
|
-
validators = obj[name][:validates]
|
43
|
-
validator_type = validators.keys.first
|
44
|
-
|
45
|
-
validator = cls.validators.first
|
46
|
-
expect(validator.class).to eq "ActiveRecord::Validations::#{validator_type.capitalize}Validator".constantize
|
47
|
-
end
|
48
|
-
|
49
|
-
it "_dynamic_attrs should have configuration from define_attrs" do
|
50
|
-
expect(cls._dynamic_attrs).to eq definition
|
51
|
-
end
|
52
|
-
|
53
|
-
it "attrs_for with no state should return definition" do
|
54
|
-
expect(cls.attrs_for).to eq definition
|
55
|
-
end
|
56
|
-
|
57
|
-
it "attrs_names_for with no state should return definition" do
|
58
|
-
expect(cls.attrs_names_for).to eq definition.keys
|
59
|
-
end
|
15
|
+
## WITH NO VALIDATOR ##
|
16
|
+
describe "With no validator" do
|
17
|
+
it "Should be valid" do
|
18
|
+
instance = cls.new
|
19
|
+
instance.valid?
|
20
|
+
expect(instance.valid?).to eq true
|
21
|
+
expect(instance.errors.full_messages).to eq []
|
60
22
|
end
|
23
|
+
end
|
24
|
+
## WITH NO VALIDATOR - END ##
|
61
25
|
|
62
|
-
|
63
|
-
let(:defition_array) { [{name:{validates:{presence: true}, on: :right}}] }
|
64
|
-
let(:definition) { Hash[*defition_array.flatten] }
|
65
|
-
let(:instance) { cls.new }
|
26
|
+
describe "With validator" do
|
66
27
|
|
67
|
-
|
68
|
-
|
69
|
-
|
28
|
+
describe "Without if" do
|
29
|
+
## WITH VALIDATOR - WITHOUT STATE ##
|
30
|
+
describe "Without state" do
|
31
|
+
before do
|
32
|
+
cls.define_attrs name: {
|
33
|
+
validates: { presence: true }
|
34
|
+
}
|
35
|
+
@instance = cls.new
|
36
|
+
end
|
70
37
|
|
71
|
-
|
72
|
-
|
73
|
-
|
38
|
+
it "Should not be valid when having no value" do
|
39
|
+
@instance.valid?
|
40
|
+
expect(@instance.valid?).to eq false
|
41
|
+
expect(@instance.errors.full_messages).not_to eq []
|
42
|
+
end
|
43
|
+
it "Should be valid when having a value" do
|
44
|
+
@instance.name = 'My Phone'
|
45
|
+
@instance.valid?
|
46
|
+
expect(@instance.valid?).to eq true
|
47
|
+
expect(@instance.errors.full_messages).to eq []
|
48
|
+
end
|
74
49
|
end
|
50
|
+
## WITH VALIDATOR - WITHOUT STATE - END ##
|
75
51
|
|
76
|
-
|
77
|
-
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
52
|
+
## WITH VALIDATOR - WITH STATE ##
|
53
|
+
describe "With state" do
|
54
|
+
before do
|
55
|
+
cls.define_attrs name: {
|
56
|
+
validates: { presence: true },
|
57
|
+
on: :right
|
58
|
+
}
|
59
|
+
@instance = cls.new
|
60
|
+
end
|
85
61
|
|
86
|
-
|
87
|
-
|
88
|
-
|
62
|
+
describe "With right validatable state" do
|
63
|
+
before do
|
64
|
+
@instance.state = :right
|
65
|
+
end
|
89
66
|
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
describe "with right state" do
|
95
|
-
it "with symbol should return definition" do
|
96
|
-
expect(cls.attrs_for(:right)).to eq definition
|
67
|
+
it "Should not be valid when having no value" do
|
68
|
+
@instance.valid?
|
69
|
+
expect(@instance.valid?).to eq false
|
70
|
+
expect(@instance.errors.full_messages).not_to eq []
|
97
71
|
end
|
98
|
-
it "
|
99
|
-
|
72
|
+
it "Should be valid when having a value" do
|
73
|
+
@instance.name = 'My Phone'
|
74
|
+
@instance.valid?
|
75
|
+
expect(@instance.valid?).to eq true
|
76
|
+
expect(@instance.errors.full_messages).to eq []
|
100
77
|
end
|
101
78
|
end
|
102
|
-
|
103
|
-
|
104
|
-
|
105
|
-
|
106
|
-
it "with string should return empty object" do
|
107
|
-
expect(cls.attrs_for("wrong")).to eq({})
|
79
|
+
|
80
|
+
describe "With wrong validatable state" do
|
81
|
+
before do
|
82
|
+
@instance.state = :wrong
|
108
83
|
end
|
109
|
-
end
|
110
|
-
end
|
111
84
|
|
112
|
-
|
113
|
-
|
114
|
-
|
115
|
-
|
116
|
-
describe "with right state" do
|
117
|
-
it "with symbol should return definition keys" do
|
118
|
-
expect(cls.attrs_names_for(:right)).to eq definition.keys
|
85
|
+
it "Should be valid when having no value" do
|
86
|
+
@instance.valid?
|
87
|
+
expect(@instance.valid?).to eq true
|
88
|
+
expect(@instance.errors.full_messages).to eq []
|
119
89
|
end
|
120
|
-
it "
|
121
|
-
|
90
|
+
it "Should be valid when having a value" do
|
91
|
+
@instance.name = 'My Phone'
|
92
|
+
@instance.valid?
|
93
|
+
expect(@instance.valid?).to eq true
|
94
|
+
expect(@instance.errors.full_messages).to eq []
|
122
95
|
end
|
123
96
|
end
|
124
|
-
|
125
|
-
|
126
|
-
|
97
|
+
|
98
|
+
describe "With no validatable state" do
|
99
|
+
it "Should be valid when having no value" do
|
100
|
+
@instance.valid?
|
101
|
+
expect(@instance.valid?).to eq true
|
102
|
+
expect(@instance.errors.full_messages).to eq []
|
127
103
|
end
|
128
|
-
it "
|
129
|
-
|
104
|
+
it "Should be valid when having a value" do
|
105
|
+
@instance.name = 'My Phone'
|
106
|
+
@instance.valid?
|
107
|
+
expect(@instance.valid?).to eq true
|
108
|
+
expect(@instance.errors.full_messages).to eq []
|
130
109
|
end
|
131
110
|
end
|
132
111
|
end
|
133
|
-
|
134
|
-
describe "values_for" do
|
135
|
-
pending "TEST VALUES FOR"
|
136
|
-
end
|
137
|
-
end
|
138
|
-
end
|
139
|
-
|
140
|
-
describe "Defining define_attr_state_if" do
|
141
|
-
before do
|
142
|
-
cls.clear_dynamic_attrs!
|
143
|
-
end
|
144
|
-
|
145
|
-
it "Should have no _dynamic_attr_state_if as standard" do
|
146
|
-
expect(cls._dynamic_attr_state_if).to be_nil
|
147
|
-
end
|
148
|
-
|
149
|
-
it "lala" do
|
150
|
-
proc = Proc.new { true }
|
151
|
-
cls.define_attr_state_if proc
|
152
|
-
expect(cls._dynamic_attr_state_if).to eq proc
|
153
|
-
end
|
154
|
-
end
|
155
|
-
|
156
|
-
describe "Model Instance" do
|
157
|
-
let(:cls) { Phone }
|
158
|
-
|
159
|
-
describe "With no validator" do
|
160
|
-
let(:instance) { cls.new }
|
161
|
-
|
162
|
-
before do
|
163
|
-
cls.clear_validators!
|
164
|
-
end
|
165
|
-
|
166
|
-
it "Should be valid" do
|
167
|
-
instance.valid?
|
168
|
-
expect(instance.valid?).to eq true
|
169
|
-
expect(instance.errors.full_messages).to eq []
|
170
|
-
end
|
112
|
+
## WITH VALIDATOR - WITH STATE ##
|
171
113
|
end
|
172
114
|
|
173
|
-
describe "With validator" do
|
174
|
-
let(:defition_array) { [{name:{validates:{presence: true}, on: :right}}] }
|
175
|
-
let(:definition) { Hash[*defition_array.flatten] }
|
176
|
-
|
177
|
-
before do
|
178
|
-
cls.clear_validators!
|
179
|
-
cls.clear_dynamic_attrs!
|
180
|
-
|
181
|
-
cls.define_attr_state_if Proc.new { |value|
|
182
|
-
self.validator value
|
183
|
-
}
|
184
|
-
cls.define_attrs definition
|
185
|
-
end
|
186
|
-
|
187
|
-
describe "With rigth validatable state" do
|
188
|
-
let(:instance) { cls.new }
|
189
115
|
|
116
|
+
describe "With if statement" do
|
117
|
+
## WITH VALIDATOR - WITHOUT STATE ##
|
118
|
+
describe "Without state" do
|
190
119
|
before do
|
191
|
-
|
120
|
+
cls.define_attrs name: {
|
121
|
+
validates: { presence: true }
|
122
|
+
},
|
123
|
+
model: {
|
124
|
+
validates: {
|
125
|
+
presence: true,
|
126
|
+
if: ->(state){ !!name }
|
127
|
+
},
|
128
|
+
}
|
129
|
+
@instance = cls.new
|
192
130
|
end
|
193
131
|
|
194
|
-
it "Should not be valid when having no
|
195
|
-
instance.valid?
|
196
|
-
expect(instance.
|
197
|
-
expect(instance.
|
132
|
+
it "Should not be valid when having no values" do
|
133
|
+
@instance.valid?
|
134
|
+
expect(@instance.class._validators).not_to eq({})
|
135
|
+
expect(@instance.valid?).to eq false
|
136
|
+
expect(@instance.errors.full_messages).not_to eq []
|
137
|
+
expect(@instance.errors.full_messages_for(:name)).to eq ["Name can't be blank"]
|
138
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
198
139
|
end
|
199
|
-
it "Should be valid when having
|
200
|
-
instance.name = 'My Phone'
|
201
|
-
instance.valid?
|
202
|
-
expect(instance.valid?).to eq
|
203
|
-
expect(instance.errors.full_messages).
|
140
|
+
it "Should not be valid when having set only the non-dependent attribute" do
|
141
|
+
@instance.name = 'My Phone'
|
142
|
+
@instance.valid?
|
143
|
+
expect(@instance.valid?).to eq false
|
144
|
+
expect(@instance.errors.full_messages).not_to eq []
|
145
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
146
|
+
expect(@instance.errors.full_messages_for(:model)).to eq ["Model can't be blank"]
|
147
|
+
end
|
148
|
+
it "Should not be valid when having set only the dependent attribute" do
|
149
|
+
@instance.model = 'The Awesome Phone'
|
150
|
+
@instance.valid?
|
151
|
+
expect(@instance.valid?).to eq false
|
152
|
+
expect(@instance.errors.full_messages).not_to eq []
|
153
|
+
expect(@instance.errors.full_messages_for(:name)).to eq ["Name can't be blank"]
|
154
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
155
|
+
end
|
156
|
+
it "Should be valid when having set both attributes" do
|
157
|
+
@instance.name = 'My Phone'
|
158
|
+
@instance.model = 'The Awesome Phone'
|
159
|
+
@instance.valid?
|
160
|
+
expect(@instance.valid?).to eq true
|
161
|
+
expect(@instance.errors.full_messages).to eq []
|
162
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
163
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
204
164
|
end
|
205
165
|
end
|
166
|
+
## WITH VALIDATOR - WITHOUT STATE - END ##
|
206
167
|
|
207
|
-
|
208
|
-
|
209
|
-
|
168
|
+
## WITH VALIDATOR - WITH STATE ##
|
169
|
+
describe "With state" do
|
210
170
|
before do
|
211
|
-
|
171
|
+
cls.define_attrs name: {
|
172
|
+
validates: { presence: true },
|
173
|
+
on: :right
|
174
|
+
},
|
175
|
+
model: {
|
176
|
+
validates: {
|
177
|
+
presence: true,
|
178
|
+
if: ->(state){ !!name }
|
179
|
+
},
|
180
|
+
on: :right
|
181
|
+
}
|
182
|
+
@instance = cls.new
|
212
183
|
end
|
213
184
|
|
214
|
-
|
215
|
-
instance.valid?
|
216
|
-
expect(instance.valid?).to eq true
|
217
|
-
expect(instance.errors.full_messages).to eq []
|
218
|
-
end
|
219
|
-
it "Should be valid when having a value" do
|
220
|
-
instance.name = 'My Phone'
|
221
|
-
instance.valid?
|
222
|
-
expect(instance.valid?).to eq true
|
223
|
-
expect(instance.errors.full_messages).to eq []
|
224
|
-
end
|
225
|
-
end
|
185
|
+
describe "With right validatable state" do
|
226
186
|
|
227
|
-
|
228
|
-
|
187
|
+
before do
|
188
|
+
@instance.state = :right
|
189
|
+
end
|
229
190
|
|
230
|
-
|
231
|
-
|
232
|
-
|
233
|
-
|
234
|
-
|
235
|
-
|
236
|
-
|
237
|
-
|
238
|
-
|
239
|
-
|
191
|
+
it "Should not be valid when having no values" do
|
192
|
+
@instance.valid?
|
193
|
+
expect(@instance.valid?).to eq false
|
194
|
+
expect(@instance.errors.full_messages).not_to eq []
|
195
|
+
expect(@instance.errors.full_messages_for(:name)).to eq ["Name can't be blank"]
|
196
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
197
|
+
end
|
198
|
+
it "Should not be valid when having set only the non-dependent attribute" do
|
199
|
+
@instance.name = 'My Phone'
|
200
|
+
@instance.valid?
|
201
|
+
expect(@instance.valid?).to eq false
|
202
|
+
expect(@instance.errors.full_messages).not_to eq []
|
203
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
204
|
+
expect(@instance.errors.full_messages_for(:model)).to eq ["Model can't be blank"]
|
205
|
+
end
|
206
|
+
it "Should not be valid when having set only the dependent attribute" do
|
207
|
+
@instance.model = 'The Awesome Phone'
|
208
|
+
@instance.valid?
|
209
|
+
expect(@instance.valid?).to eq false
|
210
|
+
expect(@instance.errors.full_messages).not_to eq []
|
211
|
+
expect(@instance.errors.full_messages_for(:name)).to eq ["Name can't be blank"]
|
212
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
213
|
+
end
|
214
|
+
it "Should be valid when having set both attributes" do
|
215
|
+
@instance.name = 'My Phone'
|
216
|
+
@instance.model = 'The Awesome Phone'
|
217
|
+
@instance.valid?
|
218
|
+
expect(@instance.valid?).to eq true
|
219
|
+
expect(@instance.errors.full_messages).to eq []
|
220
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
221
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
222
|
+
end
|
240
223
|
end
|
241
|
-
end
|
242
|
-
end
|
243
|
-
end
|
244
224
|
|
245
|
-
|
225
|
+
describe "With wrong validatable state" do
|
246
226
|
|
247
|
-
|
248
|
-
|
249
|
-
|
250
|
-
model.clear_dynamic_attrs!
|
251
|
-
end
|
252
|
-
end
|
227
|
+
before do
|
228
|
+
@instance.state = :wrong
|
229
|
+
end
|
253
230
|
|
254
|
-
|
255
|
-
|
256
|
-
|
231
|
+
it "Should be valid when having no values" do
|
232
|
+
@instance.valid?
|
233
|
+
expect(@instance.valid?).to eq true
|
234
|
+
expect(@instance.errors.full_messages).to eq []
|
235
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
236
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
237
|
+
end
|
238
|
+
it "Should be valid when having set only the non-dependent attribute" do
|
239
|
+
@instance.name = 'My Phone'
|
240
|
+
@instance.valid?
|
241
|
+
expect(@instance.valid?).to eq true
|
242
|
+
expect(@instance.errors.full_messages).to eq []
|
243
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
244
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
245
|
+
end
|
246
|
+
it "Should be valid when having set only the dependent attribute" do
|
247
|
+
@instance.model = 'The Awesome Phone'
|
248
|
+
@instance.valid?
|
249
|
+
expect(@instance.valid?).to eq true
|
250
|
+
expect(@instance.errors.full_messages).to eq []
|
251
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
252
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
253
|
+
end
|
254
|
+
it "Should be valid when having set both attributes" do
|
255
|
+
@instance.name = 'My Phone'
|
256
|
+
@instance.model = 'The Awesome Phone'
|
257
|
+
@instance.valid?
|
258
|
+
expect(@instance.valid?).to eq true
|
259
|
+
expect(@instance.errors.full_messages).to eq []
|
260
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
261
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
262
|
+
end
|
257
263
|
|
258
|
-
|
259
|
-
Car.define_attrs [{name:{validates:{presence: true}, on: :another}}]
|
260
|
-
expect(Phone._dynamic_attrs).not_to eq({})
|
261
|
-
expect(Car._dynamic_attrs).not_to eq({})
|
264
|
+
end
|
262
265
|
|
263
|
-
|
266
|
+
describe "With no validatable state" do
|
267
|
+
it "Should be valid when having no values" do
|
268
|
+
@instance.valid?
|
269
|
+
expect(@instance.valid?).to eq true
|
270
|
+
expect(@instance.errors.full_messages).to eq []
|
271
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
272
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
273
|
+
end
|
274
|
+
it "Should be valid when having set only the non-dependent attribute" do
|
275
|
+
@instance.name = 'My Phone'
|
276
|
+
@instance.valid?
|
277
|
+
expect(@instance.valid?).to eq true
|
278
|
+
expect(@instance.errors.full_messages).to eq []
|
279
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
280
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
281
|
+
end
|
282
|
+
it "Should be valid when having set only the dependent attribute" do
|
283
|
+
@instance.model = 'The Awesome Phone'
|
284
|
+
@instance.valid?
|
285
|
+
expect(@instance.valid?).to eq true
|
286
|
+
expect(@instance.errors.full_messages).to eq []
|
287
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
288
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
289
|
+
end
|
290
|
+
it "Should be valid when having set both attributes" do
|
291
|
+
@instance.name = 'My Phone'
|
292
|
+
@instance.model = 'The Awesome Phone'
|
293
|
+
@instance.valid?
|
294
|
+
expect(@instance.valid?).to eq true
|
295
|
+
expect(@instance.errors.full_messages).to eq []
|
296
|
+
expect(@instance.errors.full_messages_for(:name)).to eq []
|
297
|
+
expect(@instance.errors.full_messages_for(:model)).to eq []
|
298
|
+
end
|
299
|
+
end
|
300
|
+
end
|
301
|
+
## WITH VALIDATOR - WITH STATE ##
|
264
302
|
end
|
265
303
|
end
|
266
304
|
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe "MULTIPLE MODELS TESTS" do
|
4
|
+
|
5
|
+
before do
|
6
|
+
[Phone, Car].each do |model|
|
7
|
+
model.clear_validators!
|
8
|
+
model.clear_dynamic_attrs!
|
9
|
+
end
|
10
|
+
end
|
11
|
+
|
12
|
+
it "_dynamic_attrs should be different" do
|
13
|
+
expect(Phone._dynamic_attrs).to eq({})
|
14
|
+
expect(Car._dynamic_attrs).to eq({})
|
15
|
+
|
16
|
+
Phone.define_attrs [{name:{validates:{presence: true}, on: :right}}]
|
17
|
+
Car.define_attrs [{name:{validates:{presence: true}, on: :another}}]
|
18
|
+
expect(Phone._dynamic_attrs).not_to eq({})
|
19
|
+
expect(Car._dynamic_attrs).not_to eq({})
|
20
|
+
|
21
|
+
expect(Phone._dynamic_attrs).not_to eq Car._dynamic_attrs
|
22
|
+
end
|
23
|
+
end
|
data/spec/support/schema.rb
CHANGED
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: dynamic_attribute_declaration
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.0.
|
4
|
+
version: 0.0.11
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Mikkel Wied Frederiksen
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2014-10-
|
11
|
+
date: 2014-10-02 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: bundler
|
@@ -28,72 +28,72 @@ dependencies:
|
|
28
28
|
name: rake
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
|
-
- - "
|
31
|
+
- - "~>"
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version: '
|
33
|
+
version: '10'
|
34
34
|
type: :development
|
35
35
|
prerelease: false
|
36
36
|
version_requirements: !ruby/object:Gem::Requirement
|
37
37
|
requirements:
|
38
|
-
- - "
|
38
|
+
- - "~>"
|
39
39
|
- !ruby/object:Gem::Version
|
40
|
-
version: '
|
40
|
+
version: '10'
|
41
41
|
- !ruby/object:Gem::Dependency
|
42
42
|
name: rspec
|
43
43
|
requirement: !ruby/object:Gem::Requirement
|
44
44
|
requirements:
|
45
|
-
- - "
|
45
|
+
- - "~>"
|
46
46
|
- !ruby/object:Gem::Version
|
47
|
-
version: '
|
47
|
+
version: '3'
|
48
48
|
type: :development
|
49
49
|
prerelease: false
|
50
50
|
version_requirements: !ruby/object:Gem::Requirement
|
51
51
|
requirements:
|
52
|
-
- - "
|
52
|
+
- - "~>"
|
53
53
|
- !ruby/object:Gem::Version
|
54
|
-
version: '
|
54
|
+
version: '3'
|
55
55
|
- !ruby/object:Gem::Dependency
|
56
56
|
name: rspec-its
|
57
57
|
requirement: !ruby/object:Gem::Requirement
|
58
58
|
requirements:
|
59
|
-
- - "
|
59
|
+
- - "~>"
|
60
60
|
- !ruby/object:Gem::Version
|
61
|
-
version: '
|
61
|
+
version: '1'
|
62
62
|
type: :development
|
63
63
|
prerelease: false
|
64
64
|
version_requirements: !ruby/object:Gem::Requirement
|
65
65
|
requirements:
|
66
|
-
- - "
|
66
|
+
- - "~>"
|
67
67
|
- !ruby/object:Gem::Version
|
68
|
-
version: '
|
68
|
+
version: '1'
|
69
69
|
- !ruby/object:Gem::Dependency
|
70
70
|
name: sqlite3
|
71
71
|
requirement: !ruby/object:Gem::Requirement
|
72
72
|
requirements:
|
73
|
-
- - "
|
73
|
+
- - "~>"
|
74
74
|
- !ruby/object:Gem::Version
|
75
|
-
version: '
|
75
|
+
version: '1.3'
|
76
76
|
type: :development
|
77
77
|
prerelease: false
|
78
78
|
version_requirements: !ruby/object:Gem::Requirement
|
79
79
|
requirements:
|
80
|
-
- - "
|
80
|
+
- - "~>"
|
81
81
|
- !ruby/object:Gem::Version
|
82
|
-
version: '
|
82
|
+
version: '1.3'
|
83
83
|
- !ruby/object:Gem::Dependency
|
84
84
|
name: database_cleaner
|
85
85
|
requirement: !ruby/object:Gem::Requirement
|
86
86
|
requirements:
|
87
|
-
- - "
|
87
|
+
- - "~>"
|
88
88
|
- !ruby/object:Gem::Version
|
89
|
-
version: '
|
89
|
+
version: '1.3'
|
90
90
|
type: :development
|
91
91
|
prerelease: false
|
92
92
|
version_requirements: !ruby/object:Gem::Requirement
|
93
93
|
requirements:
|
94
|
-
- - "
|
94
|
+
- - "~>"
|
95
95
|
- !ruby/object:Gem::Version
|
96
|
-
version: '
|
96
|
+
version: '1.3'
|
97
97
|
- !ruby/object:Gem::Dependency
|
98
98
|
name: activerecord
|
99
99
|
requirement: !ruby/object:Gem::Requirement
|
@@ -122,7 +122,7 @@ dependencies:
|
|
122
122
|
- - "~>"
|
123
123
|
- !ruby/object:Gem::Version
|
124
124
|
version: '4.0'
|
125
|
-
description:
|
125
|
+
description: DESCRIPTION ON THE WAY! :)
|
126
126
|
email:
|
127
127
|
- mikkel@wied.cc
|
128
128
|
executables: []
|
@@ -139,11 +139,13 @@ files:
|
|
139
139
|
- dynamic_attribute_declaration.gemspec
|
140
140
|
- lib/dynamic_attribute_declaration.rb
|
141
141
|
- lib/dynamic_attribute_declaration/version.rb
|
142
|
+
- spec/lib/base_spec.rb
|
142
143
|
- spec/lib/dynamic_attribute_declaration_spec.rb
|
144
|
+
- spec/lib/multiple_spec.rb
|
143
145
|
- spec/spec_helper.rb
|
144
146
|
- spec/support/models.rb
|
145
147
|
- spec/support/schema.rb
|
146
|
-
homepage:
|
148
|
+
homepage: https://github.com/mikkelwf/dynamic_attribute_declaration
|
147
149
|
licenses:
|
148
150
|
- MIT
|
149
151
|
metadata: {}
|
@@ -169,7 +171,9 @@ specification_version: 4
|
|
169
171
|
summary: This gem lets you dynamically declare validations, that can function as partly
|
170
172
|
applied validation, based on some kind of model instance state.
|
171
173
|
test_files:
|
174
|
+
- spec/lib/base_spec.rb
|
172
175
|
- spec/lib/dynamic_attribute_declaration_spec.rb
|
176
|
+
- spec/lib/multiple_spec.rb
|
173
177
|
- spec/spec_helper.rb
|
174
178
|
- spec/support/models.rb
|
175
179
|
- spec/support/schema.rb
|