rails

https://github.com/rails/rails

Ruby

Ruby on Rails

ActiveModel::Callbacks#define_model_callbacks

define_model_callbacks accepts the same options +define_callbacks+ does,
in case you want to overwrite a default. Besides that, it also accepts an
<tt>:only</tt> option, where you can choose if you want all types (before,
around or after) or just some.

  define_model_callbacks :initializer, only: :after

Note, the <tt>only: <type></tt> hash will apply to all callbacks defined
on that method call. To get around this you can call the define_model_callbacks
method as many times as you need.

  define_model_callbacks :create,  only: :after
  define_model_callbacks :update,  only: :before
  define_model_callbacks :destroy, only: :around

Would create +after_create+, +before_update+ and +around_destroy+ methods
only.

You can pass in a class to before_<type>, after_<type> and around_<type>,
in which case the callback will call that class's <action>_<type> method
passing the object that the callback is being called on.

  class MyModel
    extend ActiveModel::Callbacks
    define_model_callbacks :create

    before_create AnotherClass
  end

  class AnotherClass
    def self.before_create( obj )
      # obj is the MyModel instance that the callback is being called on
    end
  end

NOTE: +method_name+ passed to `define_model_callbacks` must not end with
`!`, `?` or `=`.

Source | Google | Stack overflow

Edit

git clone [email protected]:rails/rails.git

cd rails

open activemodel/lib/active_model/callbacks.rb

Contribute

# Make a new branch

git checkout -b -your-name--update-docs-ActiveModel--Callbacks-define_model_callbacks-for-pr


# Commit to git

git add activemodel/lib/active_model/callbacks.rbgit commit -m "better docs for ActiveModel::Callbacks#define_model_callbacks"


# Open pull request

gem install hub # on a mac you can `brew install hub`

hub fork

git push <your name> -your-name--update-docs-ActiveModel--Callbacks-define_model_callbacks-for-pr

hub pull-request


# Celebrate!