trans_forms 0.2.1 → 0.2.2

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: a6480ab685b89f6d61fcd74be97c3d231ba01785
4
- data.tar.gz: 3ffb76ca12799e46cf585c2249539b06e31f1b7d
3
+ metadata.gz: 1c5551ba7ce1dd381cff17fa241415a3ea065ff0
4
+ data.tar.gz: 48e1a777331d51938f1d94c4a762edfb005ab9a0
5
5
  SHA512:
6
- metadata.gz: 9d0bf53811ba08874c7404e5b9ab36feb777db31caf3164b8ad679c51165c4ac3bf42e222bfd81cf3ab75fac903d2f0ffecef0144e5be712c3c4cb70e3e0843a
7
- data.tar.gz: baa5afca6873b7fe52be6889ee79a7e7caad30c54495d84844cfc4170efab7a56ef118d6e46ff716a50a80ef350bbe5fa00ff36edf812518b6d8c7a1449e969d
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
- rescue ActiveRecord::ActiveRecordError => e
46
- # Triggers callback
47
- after_save_on_error_callback e
48
- self._last_error = e
49
- if e.respond_to?(:record) && errors != e.record.errors
50
- e.record.errors.each do |attribute, message|
51
- errors.add(attribute, message) unless Array(errors[attribute]).include? message
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
 
@@ -1,3 +1,3 @@
1
1
  module TransForms
2
- VERSION = '0.2.1'
2
+ VERSION = '0.2.2'
3
3
  end
@@ -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
- it 'is considered valid when validations pass' do
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
- form.name = ''
48
- expect(form).not_to be_valid
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
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.1
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Daniel Viklund