trans_forms 0.2.0 → 0.2.1
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:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a6480ab685b89f6d61fcd74be97c3d231ba01785
|
4
|
+
data.tar.gz: 3ffb76ca12799e46cf585c2249539b06e31f1b7d
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9d0bf53811ba08874c7404e5b9ab36feb777db31caf3164b8ad679c51165c4ac3bf42e222bfd81cf3ab75fac903d2f0ffecef0144e5be712c3c4cb70e3e0843a
|
7
|
+
data.tar.gz: baa5afca6873b7fe52be6889ee79a7e7caad30c54495d84844cfc4170efab7a56ef118d6e46ff716a50a80ef350bbe5fa00ff36edf812518b6d8c7a1449e969d
|
@@ -46,9 +46,9 @@ module TransForms
|
|
46
46
|
# Triggers callback
|
47
47
|
after_save_on_error_callback e
|
48
48
|
self._last_error = e
|
49
|
-
if e.respond_to?(:record)
|
49
|
+
if e.respond_to?(:record) && errors != e.record.errors
|
50
50
|
e.record.errors.each do |attribute, message|
|
51
|
-
errors.add(attribute, message)
|
51
|
+
errors.add(attribute, message) unless Array(errors[attribute]).include? message
|
52
52
|
end
|
53
53
|
end
|
54
54
|
false
|
data/lib/trans_forms/version.rb
CHANGED
@@ -4,13 +4,37 @@ module TransForms
|
|
4
4
|
describe FormBase do
|
5
5
|
|
6
6
|
describe '#initialize' do
|
7
|
-
|
8
|
-
|
9
|
-
|
7
|
+
subject { UserCreator1.new(attr) }
|
8
|
+
|
9
|
+
context 'when assigning attributes defined by +attribute+' do
|
10
|
+
let(:attr) { { name: 'John Doe', age: 30 } }
|
11
|
+
|
12
|
+
it { is_expected.to be_a UserCreator1 }
|
13
|
+
|
14
|
+
it 'assigns attributes and responds to method calls' do
|
15
|
+
expect(subject.name).to eq attr[:name]
|
16
|
+
expect(subject.age).to eq attr[:age]
|
17
|
+
end
|
18
|
+
end
|
19
|
+
|
20
|
+
context 'when assigning attributes defined by +attr_accessor+' do
|
21
|
+
let(:attr) { { extra_attribute: 'value' } }
|
22
|
+
|
23
|
+
it { is_expected.to be_a UserCreator1 }
|
10
24
|
|
11
|
-
|
12
|
-
|
13
|
-
|
25
|
+
it 'assigns and responds to method calls' do
|
26
|
+
expect(subject.extra_attribute).to eq attr[:extra_attribute]
|
27
|
+
end
|
28
|
+
end
|
29
|
+
|
30
|
+
context 'when assigning attributes that are not defined in any way' do
|
31
|
+
let(:attr) { { foo: 'bar' } }
|
32
|
+
|
33
|
+
it { is_expected.to be_a UserCreator1 }
|
34
|
+
|
35
|
+
it 'does not assign or respond to method call' do
|
36
|
+
expect { subject.foo }.to raise_error(NoMethodError)
|
37
|
+
end
|
14
38
|
end
|
15
39
|
end
|
16
40
|
|
@@ -26,54 +50,88 @@ module TransForms
|
|
26
50
|
end
|
27
51
|
|
28
52
|
describe '#save' do
|
53
|
+
subject { form.save }
|
54
|
+
|
29
55
|
context 'when no errors occur inside +transaction+ block' do
|
30
56
|
let(:form) { NoErrorInTransactionForm.new }
|
31
|
-
|
57
|
+
|
58
|
+
it { is_expected.to be true }
|
32
59
|
end
|
33
60
|
|
34
61
|
context 'when errors do occur inside the +transaction+ block' do
|
35
62
|
let(:form) { ErrorInTransactionForm.new }
|
36
|
-
|
63
|
+
|
64
|
+
it { is_expected.to be false }
|
37
65
|
end
|
38
66
|
end
|
39
67
|
|
40
68
|
describe '#save!' do
|
69
|
+
subject { form.save! }
|
70
|
+
|
41
71
|
context 'when no errors occur inside +transaction+ block' do
|
42
72
|
let(:form) { NoErrorInTransactionForm.new }
|
43
|
-
|
73
|
+
|
74
|
+
it { is_expected.to be true }
|
44
75
|
end
|
45
76
|
|
46
77
|
context 'when active errors do occur inside the +transaction+ block' do
|
47
78
|
let(:form) { ErrorInTransactionForm.new }
|
48
|
-
|
79
|
+
|
80
|
+
it { expect{ subject }.to raise_error(ActiveRecord::ActiveRecordError) }
|
49
81
|
end
|
50
82
|
end
|
51
83
|
|
52
84
|
describe 'transaction' do
|
53
|
-
|
54
|
-
|
55
|
-
|
85
|
+
subject { form.save }
|
86
|
+
|
87
|
+
context 'when trying to create multiple records inside a transaction without errors' do
|
88
|
+
let(:form) { MultipleRecordsCreator.new(attr) }
|
89
|
+
let(:attr) { { name1: 'John', name2: 'Peter' } }
|
56
90
|
|
57
|
-
expect(
|
58
|
-
|
91
|
+
it { expect(subject).to be true }
|
92
|
+
|
93
|
+
it 'saves multiple records' do
|
94
|
+
expect{ subject }.to change{ User.count }.by 2
|
95
|
+
end
|
59
96
|
end
|
60
97
|
|
61
|
-
|
62
|
-
|
63
|
-
|
64
|
-
|
65
|
-
|
98
|
+
context 'when trying to create multiple records but second record fails to create because of validation error' do
|
99
|
+
let(:form) { MultipleRecordsCreator.new(attr) }
|
100
|
+
let(:attr) { { name1: 'John', name2: 'John' } }
|
101
|
+
|
102
|
+
it { expect(subject).to be false }
|
66
103
|
|
67
|
-
|
68
|
-
|
104
|
+
it 'rollbacks both saves' do
|
105
|
+
expect{ subject }.not_to change{ User.count }
|
106
|
+
end
|
69
107
|
end
|
70
108
|
|
71
|
-
|
72
|
-
|
73
|
-
|
109
|
+
context 'when trying to create multiple records and second record fails to create because of validations, but failed save did not raise error' do
|
110
|
+
let(:form) { MultipleRecordsCreatorNoBang.new(attr) }
|
111
|
+
let(:attr) { { name1: 'John', name2: 'John' } }
|
112
|
+
|
113
|
+
it { expect(subject).to be true }
|
114
|
+
|
115
|
+
it 'does not cause rollback and successfully saved the first record' do
|
116
|
+
expect{ subject }.to change{ User.count }.by 1
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
context 'when +set_main_model+ is used and main instance is raising an error inside the transaction' do
|
121
|
+
let(:user) { User.new }
|
122
|
+
let(:form) { UserProxyModel.new({ model: user, name: '' }) }
|
123
|
+
|
124
|
+
it { expect(subject).to be false }
|
125
|
+
|
126
|
+
it 'does not create a new User' do
|
127
|
+
expect{ subject }.not_to change{ User.count }
|
128
|
+
end
|
129
|
+
|
130
|
+
it 'does not create a new User' do
|
131
|
+
form.save
|
74
132
|
|
75
|
-
|
76
|
-
|
133
|
+
expect(form.errors).to eq user.errors
|
134
|
+
end
|
77
135
|
end
|
78
136
|
end
|
79
137
|
|
metadata
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: trans_forms
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.2.
|
4
|
+
version: 0.2.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Daniel Viklund
|
@@ -14,44 +14,56 @@ dependencies:
|
|
14
14
|
name: virtus
|
15
15
|
requirement: !ruby/object:Gem::Requirement
|
16
16
|
requirements:
|
17
|
-
- -
|
17
|
+
- - '='
|
18
18
|
- !ruby/object:Gem::Version
|
19
|
-
version:
|
19
|
+
version: 1.0.5
|
20
20
|
type: :runtime
|
21
21
|
prerelease: false
|
22
22
|
version_requirements: !ruby/object:Gem::Requirement
|
23
23
|
requirements:
|
24
|
-
- -
|
24
|
+
- - '='
|
25
25
|
- !ruby/object:Gem::Version
|
26
|
-
version:
|
26
|
+
version: 1.0.5
|
27
27
|
- !ruby/object:Gem::Dependency
|
28
28
|
name: activemodel
|
29
29
|
requirement: !ruby/object:Gem::Requirement
|
30
30
|
requirements:
|
31
31
|
- - ">="
|
32
32
|
- !ruby/object:Gem::Version
|
33
|
-
version:
|
33
|
+
version: 4.2.0
|
34
|
+
- - "<"
|
35
|
+
- !ruby/object:Gem::Version
|
36
|
+
version: '6'
|
34
37
|
type: :runtime
|
35
38
|
prerelease: false
|
36
39
|
version_requirements: !ruby/object:Gem::Requirement
|
37
40
|
requirements:
|
38
41
|
- - ">="
|
39
42
|
- !ruby/object:Gem::Version
|
40
|
-
version:
|
43
|
+
version: 4.2.0
|
44
|
+
- - "<"
|
45
|
+
- !ruby/object:Gem::Version
|
46
|
+
version: '6'
|
41
47
|
- !ruby/object:Gem::Dependency
|
42
48
|
name: activesupport
|
43
49
|
requirement: !ruby/object:Gem::Requirement
|
44
50
|
requirements:
|
45
51
|
- - ">="
|
46
52
|
- !ruby/object:Gem::Version
|
47
|
-
version:
|
53
|
+
version: 4.2.0
|
54
|
+
- - "<"
|
55
|
+
- !ruby/object:Gem::Version
|
56
|
+
version: '6'
|
48
57
|
type: :runtime
|
49
58
|
prerelease: false
|
50
59
|
version_requirements: !ruby/object:Gem::Requirement
|
51
60
|
requirements:
|
52
61
|
- - ">="
|
53
62
|
- !ruby/object:Gem::Version
|
54
|
-
version:
|
63
|
+
version: 4.2.0
|
64
|
+
- - "<"
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: '6'
|
55
67
|
description: Gem to create Form records that handles multiple changes wrapped in a
|
56
68
|
transaction
|
57
69
|
email:
|
@@ -104,7 +116,7 @@ required_ruby_version: !ruby/object:Gem::Requirement
|
|
104
116
|
requirements:
|
105
117
|
- - ">="
|
106
118
|
- !ruby/object:Gem::Version
|
107
|
-
version:
|
119
|
+
version: 2.3.6
|
108
120
|
required_rubygems_version: !ruby/object:Gem::Requirement
|
109
121
|
requirements:
|
110
122
|
- - ">="
|