📜 ⬆️ ⬇️

Recipes: Custom Event Handler Sets

I think most Ruby users worked with Rails, and used their event handlers.

validate :validate_humanity_conflicts validates_presence_of :radius #    ,   

The benefits of them are obvious:
We will most likely want to use declarations for the convenience of describing our own events.

And I will show how to do it:
')
 class AnyClass include ActiveSupport::Callbacks # already included by ActiveRecord::Base define_callbacks :after_something def after_something run_callbacks(:after_something) end end 

 class ChildClass < AnyClass after_something :eliminate_frags def eliminate_frags # do it true # to stop callback chain return false end end 

For this functionality to work, you need gem active_support (included in rails, but can also be used separately).

I almost forgot, the chain of event handlers in this case will be called when the instance method is called

 after_something 

Source: https://habr.com/ru/post/67696/


All Articles