Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added without_events method to ObserveEvents class. This feature upda… #693

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,6 +101,7 @@ class Model(TimeStampsMixin, ObservesEvents, metaclass=ModelMeta):
__selects__ = []

__observers__ = {}
__has_events__ = True

_booted = False
_scopes = {}
Expand Down
25 changes: 20 additions & 5 deletions src/masoniteorm/observers/ObservesEvents.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,29 @@
class ObservesEvents:
def observe_events(self, model, event):
for observer in model.__observers__.get(model.__class__, []):
try:
getattr(observer, event)(model)
except AttributeError:
pass
if model.__has_events__ == True:
for observer in model.__observers__.get(model.__class__, []):
try:
getattr(observer, event)(model)
except AttributeError:
pass

@classmethod
def observe(cls, observer):
if cls in cls.__observers__:
cls.__observers__[cls].append(observer)
else:
cls.__observers__.update({cls: [observer]})

@classmethod
def without_events(cls):
"""Sets __has_events__ attribute on model to false.
"""
cls.__has_events__ = False
return cls

@classmethod
def with_events(cls):
"""Sets __has_events__ attribute on model to True.
"""
cls.__has_events__ = False
return cls