application_service 0.2.0 → 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: a2ba3938b2707398961940cb759ff62513c3e979
4
- data.tar.gz: 3701a47aba069f130b9ae0b263123bc8f7c7ed47
3
+ metadata.gz: 5098843852847896293de30d2f5c4fecad1b3102
4
+ data.tar.gz: e0155d8b8a81b6b603446550cdc6828cbddc8d2a
5
5
  SHA512:
6
- metadata.gz: 484eb1e80e2af5cbbcefc1c8bc0a60047664d8abf4b1a36f077ea5f92cccaec698d297551d8a663c3cc3ffb1666e034078df829aad2fb206a2269e78fac0435c
7
- data.tar.gz: 8b1295095f81155e29ffe0bb7d63b1819af83907af787b7f9df77b341311a03186edb2d91866588acf973fb6bbece74f17abad85279e11503449ce6e9ca6421b
6
+ metadata.gz: 2f7e81a4f71400cc1439cd684b2332cc847afec95e351fe8b35e050e3424b0d7206a3b3b5581fe5e6637b1f5b4cae287f9337376df79b9fe2564fe36fa393312
7
+ data.tar.gz: cef7d3d2b4cfc15fe1fe8d396b5921be18a4fc75f1d5330e496594d14ff14c41eb60ca4e8c82540615ed952f7f20ba47cba50df72046becdf9468e22d0522cc7
data/VERSION CHANGED
@@ -1 +1 @@
1
- 0.2.0
1
+ 0.2.2
@@ -2,16 +2,16 @@
2
2
  # DO NOT EDIT THIS FILE DIRECTLY
3
3
  # Instead, edit Jeweler::Tasks in Rakefile, and run 'rake gemspec'
4
4
  # -*- encoding: utf-8 -*-
5
- # stub: application_service 0.2.0 ruby lib
5
+ # stub: application_service 0.2.2 ruby lib
6
6
 
7
7
  Gem::Specification.new do |s|
8
8
  s.name = "application_service"
9
- s.version = "0.2.0"
9
+ s.version = "0.2.2"
10
10
 
11
11
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
12
12
  s.require_paths = ["lib"]
13
13
  s.authors = ["Nathan Woodhull"]
14
- s.date = "2014-08-20"
14
+ s.date = "2014-11-18"
15
15
  s.description = "A service layer scaffold for rails apps extracted from Agra"
16
16
  s.email = "woodhull@gmail.com"
17
17
  s.extra_rdoc_files = [
@@ -15,6 +15,9 @@ class ApplicationService
15
15
 
16
16
  args.each do |arg|
17
17
  set_callback callback, :before, arg, options
18
+ if options.fetch(:skip_on_admin_save, false)
19
+ skip_callback callback, :before, arg, if: -> { @on_admin_save }
20
+ end
18
21
  end
19
22
  end
20
23
 
@@ -23,6 +26,9 @@ class ApplicationService
23
26
 
24
27
  args.each do |arg|
25
28
  set_callback callback, :after, arg, options
29
+ if options.fetch(:skip_on_admin_save, false)
30
+ skip_callback callback, :after, arg, if: -> { @on_admin_save }
31
+ end
26
32
  end
27
33
  end
28
34
 
@@ -78,29 +84,46 @@ class ApplicationService
78
84
  end
79
85
  end
80
86
 
87
+ def admin_save(obj)
88
+ @obj = obj
89
+
90
+ @on_admin_save = true
91
+ save_method = @obj.respond_to?(:admin_save) ? :admin_save : :save
92
+ run_callbacks :save do
93
+ if @obj.new_record?
94
+ result = create(save_method)
95
+ else
96
+ result = update(save_method)
97
+ end
98
+ result
99
+ end
100
+ ensure
101
+ @on_admin_save = false
102
+ end
103
+
81
104
  private
82
105
 
83
- def create!
106
+ def create!(save_method=:save!)
84
107
  run_callbacks :create do
85
- @obj.save!
108
+ @obj.send(save_method)
86
109
  end
87
110
  end
88
111
 
89
- def create
112
+ def create(save_method=:save)
90
113
  run_callbacks :create do
91
- @obj.save
114
+ @obj.send(save_method)
92
115
  end
93
116
  end
94
117
 
95
- def update
118
+ def update(save_method=:save)
96
119
  run_callbacks :update do
97
- @obj.save
120
+ @obj.send(save_method)
98
121
  end
99
122
  end
100
123
 
101
- def update!
124
+ def update!(save_method=:save!)
102
125
  run_callbacks :update do
103
- @obj.save!
126
+ @obj.send(save_method)
104
127
  end
105
128
  end
106
129
 
@@ -89,6 +89,114 @@ describe ApplicationService do
89
89
  end
90
90
  end
91
91
 
92
+ describe '#admin_save' do
93
+ it "should trigger admin_save if defined on object" do
94
+ obj = double(new_record?: true)
95
+ expect(obj).to receive(:admin_save)
96
+
97
+ subject.admin_save(obj)
98
+ end
99
+
100
+ it "should trigger regular save if admin_save not defined on object" do
101
+ obj = double(new_record?: true)
102
+ expect(obj).to receive(:save)
103
+
104
+ subject.admin_save(obj)
105
+ end
106
+
107
+ describe 'callbacks' do
108
+ let(:object) { double(new_record?: false, save: true) }
109
+
110
+ context 'invoke' do
111
+ it "should invoke callbacks if skip_on_admin_save is false" do
112
+ service_klass = Class.new(ApplicationService) do
113
+ before :save, :before_save_callback, skip_on_admin_save: false
114
+ after :save, :after_save_callback, skip_on_admin_save: false
115
+ end
116
+
117
+ service = service_klass.new
118
+ expect(service).to receive(:before_save_callback)
119
+ expect(service).to receive(:after_save_callback)
120
+
121
+ service.admin_save(object)
122
+ end
123
+
124
+ it "should invoke callbacks if skip_on_admin_save option missing" do
125
+ service_klass = Class.new(ApplicationService) do
126
+ before :save, :before_save_callback
127
+ after :save, :after_save_callback
128
+ end
129
+
130
+ service = service_klass.new
131
+ expect(service).to receive(:before_save_callback)
132
+ expect(service).to receive(:after_save_callback)
133
+
134
+ service.admin_save(object)
135
+ end
136
+
137
+ it "should invoke callbacks on save even if skip_on_admin_save is true" do
138
+ service_klass = Class.new(ApplicationService) do
139
+ before :save, :before_save_callback, skip_on_admin_save: true
140
+ after :save, :after_save_callback, skip_on_admin_save: true
141
+ end
142
+
143
+ service = service_klass.new
144
+ expect(service).to receive(:before_save_callback)
145
+ expect(service).to receive(:after_save_callback)
146
+
147
+ service.save(object)
148
+ end
149
+
150
+ it "should invoke callbacks on save even if admin_save raises exception" do
151
+ service_klass = Class.new(ApplicationService) do
152
+ before :save, :before_save_callback, skip_on_admin_save: true
153
+ after :save, :after_save_callback, skip_on_admin_save: true
154
+ end
155
+
156
+ expect(object).to receive(:save).and_raise(StandardError)
157
+
158
+ service = service_klass.new
159
+ expect(service).to receive(:before_save_callback).once
160
+ expect(service).to receive(:after_save_callback).once
161
+
162
+ expect { service.admin_save(object) }.to raise_error
163
+
164
+ expect(object).to receive(:save).and_return(true)
165
+
166
+ service.save(object)
167
+ end
168
+ end
169
+
170
+ context 'skip' do
171
+ it "should skip callbacks if skip_on_admin_save for create" do
172
+ service_klass = Class.new(ApplicationService) do
173
+ before :create, :before_create_callback, skip_on_admin_save: true
174
+ after :create, :after_create_callback, skip_on_admin_save: true
175
+ end
176
+
177
+ service = service_klass.new
178
+ expect(service).not_to receive(:before_create_callback)
179
+ expect(service).not_to receive(:after_create_callback)
180
+
181
+ service.admin_save(object)
182
+ end
183
+
184
+ it "should skip callbacks if skip_on_admin_save for update" do
185
+ service_klass = Class.new(ApplicationService) do
186
+ before :save, :before_save_callback, skip_on_admin_save: true
187
+ after :save, :after_save_callback, skip_on_admin_save: true
188
+ end
189
+
190
+ service = service_klass.new
191
+ expect(service).not_to receive(:before_save_callback)
192
+ expect(service).not_to receive(:after_save_callback)
193
+
194
+ service.admin_save(object)
195
+ end
196
+ end
197
+ end
198
+ end
199
+
92
200
  describe 'callbacks' do
93
201
  let(:object) { double(new_record?: false, save: true) }
94
202
 
metadata CHANGED
@@ -1,14 +1,14 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: application_service
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.2.0
4
+ version: 0.2.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Nathan Woodhull
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2014-08-20 00:00:00.000000000 Z
11
+ date: 2014-11-18 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activesupport