trans_forms 0.2.1 → 0.2.2
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/lib/trans_forms/form_base.rb +18 -7
- data/lib/trans_forms/version.rb +1 -1
- data/spec/support/trans_forms/simple_forms.rb +19 -0
- data/spec/trans_forms/form_base_spec.rb +32 -7
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 1c5551ba7ce1dd381cff17fa241415a3ea065ff0
|
4
|
+
data.tar.gz: 48e1a777331d51938f1d94c4a762edfb005ab9a0
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 88612a51733c736445d9ea61ab58e92446c7fd23f5a4e4d886a6d54fd919e8c84f30a7a0cdf3dd3e8f5b9f8d382ef3cf923ea027ef9c70211d1f3aa02bcc7267
|
7
|
+
data.tar.gz: 66164e88117619b24f94fe90de7b8fd3f93d8c63a8e4c1fa4b00778148de9894487c003206832539d6fbda278883391c61b16bf97e3c06128818ba0af873c7b5
|
@@ -42,15 +42,26 @@ module TransForms
|
|
42
42
|
instance_eval &_transaction
|
43
43
|
true
|
44
44
|
end
|
45
|
-
|
46
|
-
|
47
|
-
|
48
|
-
|
49
|
-
|
50
|
-
|
51
|
-
|
45
|
+
|
46
|
+
rescue ActiveRecord::RecordInvalid, ActiveRecord::RecordNotSaved, ActiveRecord::RecordNotDestroyed => e
|
47
|
+
handle_transaction_error e do
|
48
|
+
if e.record.present? && errors != e.record.errors
|
49
|
+
e.record.errors.each do |attribute, message|
|
50
|
+
errors.add(attribute, message) unless Array(errors[attribute]).include? message
|
51
|
+
end
|
52
52
|
end
|
53
53
|
end
|
54
|
+
|
55
|
+
rescue ActiveRecord::ActiveRecordError => e
|
56
|
+
handle_transaction_error e
|
57
|
+
end
|
58
|
+
|
59
|
+
def handle_transaction_error(e)
|
60
|
+
self._last_error = e
|
61
|
+
|
62
|
+
yield if block_given?
|
63
|
+
|
64
|
+
after_save_on_error_callback e
|
54
65
|
false
|
55
66
|
end
|
56
67
|
|
data/lib/trans_forms/version.rb
CHANGED
@@ -11,3 +11,22 @@ class ErrorInTransactionForm < TransForms::FormBase
|
|
11
11
|
raise ActiveRecord::ActiveRecordError
|
12
12
|
end
|
13
13
|
end
|
14
|
+
|
15
|
+
class RecordNotFoundInTransactionForm < TransForms::FormBase
|
16
|
+
transaction do
|
17
|
+
User.find 9999999
|
18
|
+
end
|
19
|
+
end
|
20
|
+
|
21
|
+
class RecordNotSavedInTransactionForm < TransForms::FormBase
|
22
|
+
transaction do
|
23
|
+
user = User.new
|
24
|
+
raise ActiveRecord::RecordNotSaved, ['Could not save the record', user]
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
class RecordInvalidInTransactionForm < TransForms::FormBase
|
29
|
+
transaction do
|
30
|
+
User.new.save!
|
31
|
+
end
|
32
|
+
end
|
@@ -39,13 +39,20 @@ module TransForms
|
|
39
39
|
end
|
40
40
|
|
41
41
|
describe 'validations' do
|
42
|
-
|
43
|
-
attr = { name: 'John Doe', age: 30 }
|
44
|
-
form = UserCreator1.new(attr)
|
45
|
-
expect(form).to be_valid
|
42
|
+
subject { form.valid? }
|
46
43
|
|
47
|
-
|
48
|
-
|
44
|
+
context 'when validations pass' do
|
45
|
+
let(:form) { UserCreator1.new(attr) }
|
46
|
+
let(:attr) { { name: 'John Doe', age: 30 } }
|
47
|
+
|
48
|
+
it { is_expected.to be true }
|
49
|
+
end
|
50
|
+
|
51
|
+
context 'when validations fail' do
|
52
|
+
let(:form) { UserCreator1.new(attr) }
|
53
|
+
let(:attr) { { name: '', age: 30 } }
|
54
|
+
|
55
|
+
it { is_expected.to be false }
|
49
56
|
end
|
50
57
|
end
|
51
58
|
|
@@ -74,11 +81,29 @@ module TransForms
|
|
74
81
|
it { is_expected.to be true }
|
75
82
|
end
|
76
83
|
|
77
|
-
context 'when active errors do occur inside the +transaction+ block' do
|
84
|
+
context 'when active record errors do occur inside the +transaction+ block' do
|
78
85
|
let(:form) { ErrorInTransactionForm.new }
|
79
86
|
|
80
87
|
it { expect{ subject }.to raise_error(ActiveRecord::ActiveRecordError) }
|
81
88
|
end
|
89
|
+
|
90
|
+
context 'when RecordNotFound error occur inside the +transaction+ block' do
|
91
|
+
let(:form) { RecordNotFoundInTransactionForm.new }
|
92
|
+
|
93
|
+
it { expect{ subject }.to raise_error(ActiveRecord::RecordNotFound) }
|
94
|
+
end
|
95
|
+
|
96
|
+
context 'when RecordNotSaved error occur inside the +transaction+ block' do
|
97
|
+
let(:form) { RecordNotSavedInTransactionForm.new }
|
98
|
+
|
99
|
+
it { expect{ subject }.to raise_error(ActiveRecord::RecordNotSaved) }
|
100
|
+
end
|
101
|
+
|
102
|
+
context 'when RecordInvalid error occur inside the +transaction+ block' do
|
103
|
+
let(:form) { RecordInvalidInTransactionForm.new }
|
104
|
+
|
105
|
+
it { expect{ subject }.to raise_error(ActiveRecord::RecordInvalid) }
|
106
|
+
end
|
82
107
|
end
|
83
108
|
|
84
109
|
describe 'transaction' do
|