Skip to content

Commit

Permalink
Allows changing of push behavior for different operation types
Browse files Browse the repository at this point in the history
  • Loading branch information
benedikt committed Dec 14, 2023
1 parent f2dcb32 commit bf1b9d0
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 3 deletions.
4 changes: 3 additions & 1 deletion lib/userlist/rails.rb
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,13 @@ def self.setup_callbacks(model, scope)
model.instance_variable_set(:@userlist_callbacks_registered, true)
end

def self.setup_callback(type, model, scope, method)
def self.setup_callback(type, model, scope, default_method)
return unless callback_method = [:after_commit, :"after_#{type}"].find { |m| model.respond_to?(m) }

callback = lambda do
begin
method = try(:"userlist_#{type}_behavior") || default_method

relation = Userlist::Push.public_send(scope)
relation.public_send(method, self)
rescue Userlist::Error => e
Expand Down
17 changes: 15 additions & 2 deletions spec/userlist/rails_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,19 +26,32 @@
end

context 'when the user is updated' do
let(:model) { model_class.create }

it 'should push the user' do
model = model_class.create
expect(Userlist::Push.users).to receive(:push).with(model)
model.save
end
end

context 'when the user is destroyed' do
let(:model) { model_class.create }

it 'should delete the user' do
model = model_class.create
expect(Userlist::Push.users).to receive(:delete).with(model)
model.destroy
end

context 'when the destroy behavior is push' do
before do
model.define_singleton_method(:userlist_destroy_behavior) { :push }
end

it 'should push the user' do
expect(Userlist::Push.users).to receive(:push).with(model)
model.save
end
end
end

context 'when the callbacks are set up multiple times' do
Expand Down

0 comments on commit bf1b9d0

Please sign in to comment.