cybercog / laravel-eloquent-flag

Laravel Eloquent boolean & timestamp flagged attributes behavior.
https://komarev.com/sources/laravel-eloquent-flag
MIT License
133 stars 21 forks source link

Fire events #37

Closed jonagoldman closed 6 years ago

jonagoldman commented 6 years ago

Fire events on actions. Related to https://github.com/cybercog/laravel-eloquent-flag/issues/4 and https://github.com/cybercog/laravel-eloquent-flag/issues/33. No tests added.

antonkomarev commented 6 years ago

Thank you for the PR, Jona! It would be great to have a tests for this events too. And it would be great to have small description of the decision to have 2nd argument (boolean flag $halt) as false, what does it do?

jonagoldman commented 6 years ago

The 2nd argument is used to allow/disallow the event listener to stop the propagation. This will be used in events that are fired before the method execution. For example, if we had an accepting event, it will like like this:

/**
 * Mark entity as accepted.
 *
 * @return void
 */
public function accept()
{
    $this->fireModelEvent('accepting'); // $halt defaults to true
    $this->setAcceptedFlag()->save();
    $this->fireModelEvent('accepted', false);
}

In this case, the listener to the accepting event can return false and $this->setAcceptedFlag()->save(); will not be triggered. I don't think we need this functionality right now.

Regarding tests... I must confess I have not done much testing in my projects so I think you will need to help me on this one as I don't know how to test it correctly.

antonkomarev commented 6 years ago

Okay, I'll merge this changes, and we will return to tests a bit later this week. And release will be published only after tests will be ready.

antonkomarev commented 6 years ago

@jonagoldman thank you for researching topics about this feature and compiling all this stuff down!