Skip to content

Commit 2b30bda

Browse files
authored
Merge pull request graphiti-api#53 from richmolj/master
Add around_persistence
2 parents be3af1a + cb6f71d commit 2b30bda

File tree

2 files changed

+105
-16
lines changed

2 files changed

+105
-16
lines changed

lib/graphiti/resource/persistence.rb

Lines changed: 28 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,14 @@ def around_save(method = nil, only: [:create, :update], &blk)
4444
end
4545
end
4646

47+
def around_persistence(method = nil, only: [:create, :update], &blk)
48+
if blk
49+
raise Errors::AroundCallbackProc.new(self, 'around_persistence')
50+
else
51+
add_callback(:persistence, :around, method, only, &blk)
52+
end
53+
end
54+
4755
def around_destroy(method = nil, &blk)
4856
if blk
4957
raise Errors::AroundCallbackProc.new(self, 'around_destroy')
@@ -64,31 +72,35 @@ def add_callback(kind, lifecycle, method = nil, only, &blk)
6472
def create(create_params)
6573
model_instance = nil
6674

67-
run_callbacks :attributes, :create, create_params do |params|
68-
model_instance = build(model)
69-
assign_attributes(model_instance, params)
70-
model_instance
71-
end
75+
run_callbacks :persistence, :create, create_params do
76+
run_callbacks :attributes, :create, create_params do |params|
77+
model_instance = build(model)
78+
assign_attributes(model_instance, params)
79+
model_instance
80+
end
7281

73-
run_callbacks :save, :create, model_instance do
74-
model_instance = save(model_instance)
75-
end
82+
run_callbacks :save, :create, model_instance do
83+
model_instance = save(model_instance)
84+
end
7685

77-
model_instance
86+
model_instance
87+
end
7888
end
7989

8090
def update(update_params)
8191
model_instance = nil
8292
id = update_params.delete(:id)
8393

84-
run_callbacks :attributes, :update, update_params do |params|
85-
model_instance = self.class._find(params.merge(id: id)).data
86-
assign_attributes(model_instance, params)
87-
model_instance
88-
end
94+
run_callbacks :persistence, :update, update_params do
95+
run_callbacks :attributes, :update, update_params do |params|
96+
model_instance = self.class._find(params.merge(id: id)).data
97+
assign_attributes(model_instance, params)
98+
model_instance
99+
end
89100

90-
run_callbacks :save, :update, model_instance do
91-
model_instance = save(model_instance)
101+
run_callbacks :save, :update, model_instance do
102+
model_instance = save(model_instance)
103+
end
92104
end
93105

94106
model_instance

spec/persistence_spec.rb

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -532,6 +532,83 @@ def do_after_save(model)
532532
end
533533
end
534534

535+
describe '.around_persistence' do
536+
RSpec.shared_examples 'around persistence' do |opts|
537+
opts ||= {}
538+
539+
before do
540+
klass.class_eval do
541+
before_attributes do |attrs|
542+
attrs[:first_name] = "#{attrs[:first_name]}mid"
543+
end
544+
545+
def do_around_persistence(attributes)
546+
attributes[:first_name] = 'b4'
547+
model = yield
548+
model.update_attributes(first_name: "#{model.first_name}after")
549+
model
550+
1 # return value shouldnt matter
551+
end
552+
end
553+
end
554+
555+
if opts[:only_update]
556+
context 'when creating' do
557+
include_context 'create hooks'
558+
559+
it 'does not fire' do
560+
employee
561+
expect(klass.calls.length).to eq(0)
562+
end
563+
end
564+
else
565+
context 'when creating' do
566+
include_context 'create hooks'
567+
568+
it 'can modify attributes and the saved model' do
569+
reloaded = PORO::Employee.find(employee.id)
570+
expect(reloaded.first_name).to eq('b4midafter')
571+
end
572+
end
573+
end
574+
575+
context 'when updating' do
576+
include_context 'update hooks'
577+
578+
it 'can modify attributes and the saved model' do
579+
reloaded = PORO::Employee.find(employee.id)
580+
expect(reloaded.first_name).to eq('b4midafter')
581+
end
582+
end
583+
end
584+
585+
context 'when registering via method' do
586+
before do
587+
klass.around_persistence :do_around_persistence
588+
end
589+
590+
include_examples 'around persistence'
591+
end
592+
593+
context 'when registering via proc' do
594+
it 'raises error' do
595+
expect {
596+
klass.around_persistence do
597+
'dontgethere'
598+
end
599+
}.to raise_error(Graphiti::Errors::AroundCallbackProc, /around_persistence/)
600+
end
601+
end
602+
603+
context 'when limiting actions' do
604+
before do
605+
klass.around_persistence :do_around_persistence, only: [:update]
606+
end
607+
608+
include_examples 'around persistence', only_update: true
609+
end
610+
end
611+
535612
describe '.around_save' do
536613
RSpec.shared_examples 'around save' do |opts|
537614
opts ||= {}

0 commit comments

Comments
 (0)