Skip to content

Commit

Permalink
fixing issue with updating scope
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Dec 17, 2020
1 parent ef4c5c8 commit b35d210
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 5 deletions.
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
# Versions should comply with PEP440. For a discussion on single-sourcing
# the version across setup.py and the project code, see
# https://packaging.python.org/en/latest/single_source_version.html
version='0.9.1',
version='0.9.2',
package_dir={'': 'src'},

description='The Official Masonite ORM',
Expand Down
2 changes: 1 addition & 1 deletion src/masoniteorm/models/Model.py
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,7 @@ def find(cls, record_id, query=False):
if query:
return builder.to_sql()
else:
return builder.get()
return builder.first()

def first_or_new(self):
pass
Expand Down
6 changes: 3 additions & 3 deletions src/masoniteorm/query/grammars/BaseGrammar.py
Original file line number Diff line number Diff line change
Expand Up @@ -214,12 +214,12 @@ def _compile_key_value_equals(self, qmark=False):
sql += sql_string.format(
column=self._table_column_string(column),
value=value if not qmark else "?",
separator=",",
separator=", ",
)
if qmark:
self._bindings += (value,)

sql = sql.rstrip(", ")
sql = sql.rstrip(", ")

return sql

Expand Down Expand Up @@ -394,7 +394,7 @@ def process_wheres(self, qmark=False, strip_first_where=False):

if not isinstance(where.bindings, (list, tuple)):
raise ValueError(
f"Binings must be tuple or list. Received {type(where.bindings)}"
f"Bindings must be tuple or list. Received {type(where.bindings)}"
)

if where.bindings:
Expand Down
15 changes: 15 additions & 0 deletions src/masoniteorm/scopes/TimeStampsScope.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
from .BaseScope import BaseScope

from ..expressions.expressions import (
UpdateQueryExpression,
)


class TimeStampsScope(BaseScope):
"""Global scope class to add soft deleting to models."""
Expand All @@ -9,6 +13,10 @@ def on_boot(self, builder):
"_timestamps", self.set_timestamp_create, action="insert"
)

builder.set_global_scope(
"_timestamp_update", self.set_timestamp_update, action="update"
)

def on_remove(self, builder):
pass

Expand All @@ -18,9 +26,16 @@ def set_timestamp(owner_cls, query):
def set_timestamp_create(self, builder):
if not builder._model.__timestamps__:
return builder

builder._creates.update(
{
"updated_at": builder._model.get_new_date().to_datetime_string(),
"created_at": builder._model.get_new_date().to_datetime_string(),
}
)

def set_timestamp_update(self, builder):
if not builder._model.__timestamps__:
return builder

builder._updates += (UpdateQueryExpression({"updated_at": builder._model.get_new_date().to_datetime_string()}),)

0 comments on commit b35d210

Please sign in to comment.