Rails 4: Skip Callback

Rails 4: Skip callback

skip_callback is a complicated and not granular option.

I prefer to use an attr_accessor:

attr_accessor :skip_my_method, :skip_my_method_2
after_save{ my_method unless skip_my_method }
after_save{ my_method_2 unless skip_my_method_2 }

That way you can be declarative when skipping a callback:

model.create skip_my_method: true # skips my_method
model.create skip_my_method_2: true # skips my_method_2

How I can skip callback in action

This should work:

class BarsController < ApplicationController

after_action :some_method, only: [:index], unless: :skip_action?

def index
get_cache = $redis.get('some_key')
if get_cache.present?
@skip_action = true
# i want to skip after_action callback in here
else
# other stuff
end
end

private

def skip_action?
@skip_action
end
end

You can also use attr_accessor :skip_action instead of private method because controller is just object.

How to skip ActiveRecord callbacks?

For Rails 2, but not Rails 3 you can use these:

object.send(:create_without_callbacks)
object.send(:update_without_callbacks)

Skip callback on create Rails

Based on the referenced answer:

FactoryGirl.define do
factory :withdrawal_request, class: 'WithdrawalRequest' do
...
after(:build) { WithdrawalRequest.skip_callback(:create, :before, :callback_to_be_skipped) }
#you were getting the errors here initially because you were calling the method on Class, the superclass of WithdrawalRequest

#OR
after(:build) {|withdrawal_request| withdrawal_request.class.skip_callback(:create, :before, :callback_to_be_skipped)}

end
end

Is it possible to skip role removing using callbacks with rolify gem for Rails

It looks like you are hoping to use these callbacks like an ActiveRecord validation. They are different things. The callback is simply a way to run code before and after the role record is created/destroyed, it is not used to control whether the mutation code within is run.

Some options:

  1. Add logic to your model to test if it can_remove_role?(:role) and then test that in your controller before making the change.
  2. If you are saving from a form, you can make a validation on the model to check your condition. ie. The User model would be invalid if that was what changed. Check out the methods on Dirty for inspiration on how to see what did/will change https://api.rubyonrails.org/classes/ActiveModel/Dirty.html
  3. Use a policy in something like the Pundit gem or similar authorization package (which your program should have if it is to be used in a real environment) to wrap changing of roles. ie. in the Pundit example your UserPolicy would have a change_role? or similar policy where you define what is a legal role change for the @record and then call the authorize on it.

Option 3 is nice, because it provides a DRY location to make more rules regarding what can and cannot be done with your Users. Perhaps do some searches for tutorials on how Pundit and Rolify can be used together.



Related Topics



Leave a reply



Submit