Skip to content

Commit f8904f9

Browse files
tmgbeduclaude
andcommitted
revert: restore MorphMany.py to original implementation
Co-Authored-By: Claude Sonnet 4.6 <noreply@anthropic.com>
1 parent a79418f commit f8904f9

4 files changed

Lines changed: 32 additions & 30 deletions

File tree

fastapi_startkit/src/fastapi_startkit/masoniteorm/models/model.py

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,17 @@
11
from __future__ import annotations
2-
import inflection
32

43
from typing import TYPE_CHECKING
54

5+
import inflection
6+
67
from fastapi_startkit.carbon import Carbon
78
from fastapi_startkit.masoniteorm.collection import Collection
8-
from fastapi_startkit.masoniteorm.models.fields import CreatedAtField, UpdatedAtField
9-
from fastapi_startkit.masoniteorm.models.registry import Registry
10-
from fastapi_startkit.masoniteorm.observers import ObservesEvents
119
from fastapi_startkit.masoniteorm.connections.manager import DatabaseManager
1210
from fastapi_startkit.masoniteorm.models.attribute import Attribute
11+
from fastapi_startkit.masoniteorm.models.fields import CreatedAtField, UpdatedAtField
12+
from fastapi_startkit.masoniteorm.models.registry import Registry
1313
from fastapi_startkit.masoniteorm.models.relationship import Relationship
14+
from fastapi_startkit.masoniteorm.observers import ObservesEvents
1415

1516
if TYPE_CHECKING:
1617
from fastapi_startkit.masoniteorm.models.builder import QueryBuilder
@@ -131,6 +132,14 @@ async def all(cls):
131132
async def count(cls, column: str = "*"):
132133
return await cls.query().count(column)
133134

135+
def table(self, table: str):
136+
self.__table__ = table
137+
return self
138+
139+
def timestamps(self, timestamps: bool = True):
140+
self.__timestamps__ = timestamps
141+
return self
142+
134143
def set_connection(self, connection: str):
135144
self.connection = connection
136145

fastapi_startkit/src/fastapi_startkit/masoniteorm/relationships/MorphMany.py

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,9 @@ def set_keys(self, owner, attribute):
1818
return self
1919

2020
def __get__(self, instance, owner):
21-
if instance is None:
22-
return self
23-
2421
attribute = self.fn.__name__
25-
self._related_builder = instance.get_builder()
26-
self.polymorphic_builder = self.fn(self).query()
22+
self._related_builder = instance.builder
23+
self.polymorphic_builder = self.fn(self)()
2724
self.set_keys(owner, self.fn)
2825

2926
if not instance.is_loaded():
@@ -35,7 +32,8 @@ def __get__(self, instance, owner):
3532
return self.apply_query(self._related_builder, instance)
3633

3734
def __getattr__(self, attribute):
38-
return getattr(self.fn(self).query(), attribute)
35+
relationship = self.fn(self)()
36+
return getattr(relationship.builder, attribute)
3937

4038
def apply_query(self, builder, instance):
4139
"""Apply the query and return a dictionary to be hydrated
@@ -47,7 +45,7 @@ def apply_query(self, builder, instance):
4745
Returns:
4846
dict -- A dictionary of data which will be hydrated.
4947
"""
50-
polymorphic_key = self.get_record_key_lookup(instance)
48+
polymorphic_key = self.get_record_key_lookup(builder._model)
5149
polymorphic_builder = self.polymorphic_builder
5250
return (
5351
polymorphic_builder.where(self.morph_key, polymorphic_key)
@@ -117,7 +115,13 @@ def morph_map(self):
117115
return registry.Registry.get_morph_map()
118116

119117
def get_record_key_lookup(self, relation):
120-
morph_name = registry.Registry._reverse_map.get(relation.__class__)
121-
if morph_name is None:
118+
record_type = None
119+
for record_type_loop, model in self.morph_map().items():
120+
if model == relation.__class__:
121+
record_type = record_type_loop
122+
break
123+
124+
if not record_type:
122125
raise ValueError(f"Could not find the record type key for the {relation} class")
123-
return morph_name
126+
127+
return record_type

fastapi_startkit/tests/masoniteorm/sqlite/relationships/test_morph_many.py

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -5,13 +5,9 @@
55
from ..test_case import TestCase
66

77

8-
def likes(self):
9-
return Like
10-
11-
128
class ArticleModel(Model):
139
__table__ = "articles"
14-
likes = MorphMany(likes, morph_key="likeable_type", morph_id="likeable_id")
10+
likes: list[Like] = MorphMany('Like', morph_key="likeable_type", morph_id="likeable_id")
1511

1612

1713
Registry.morph_map({"article": ArticleModel, "product": Product})
@@ -28,32 +24,26 @@ async def asyncSetUp(self):
2824
}
2925
)
3026

31-
async def test_morph_many_init_stores_keys(self):
32-
rel = MorphMany(likes, morph_key="likeable_type", morph_id="likeable_id")
33-
self.assertEqual(rel.morph_key, "likeable_type")
34-
self.assertEqual(rel.morph_id, "likeable_id")
35-
self.assertEqual(rel.fn, likes)
36-
3727
async def test_morph_many_set_keys_defaults(self):
38-
rel = MorphMany(likes)
28+
rel = MorphMany('Like')
3929
rel.set_keys(None, None)
4030
self.assertEqual(rel.morph_key, "record_type")
4131
self.assertEqual(rel.morph_id, "record_id")
4232

4333
async def test_morph_many_set_keys_keeps_existing(self):
44-
rel = MorphMany(likes, morph_key="likeable_type", morph_id="likeable_id")
34+
rel = MorphMany('Like', morph_key="likeable_type", morph_id="likeable_id")
4535
rel.set_keys(None, None)
4636
self.assertEqual(rel.morph_key, "likeable_type")
4737
self.assertEqual(rel.morph_id, "likeable_id")
4838

4939
async def test_morph_map_returns_registry(self):
50-
rel = MorphMany(likes, morph_key="likeable_type", morph_id="likeable_id")
40+
rel = MorphMany('Like', morph_key="likeable_type", morph_id="likeable_id")
5141
morph_map = rel.morph_map()
5242
self.assertIn("article", morph_map)
5343
self.assertIn("product", morph_map)
5444

5545
async def test_get_record_key_lookup_returns_key(self):
56-
rel = MorphMany(likes, morph_key="likeable_type", morph_id="likeable_id")
46+
rel = MorphMany('Like', morph_key="likeable_type", morph_id="likeable_id")
5747
key = rel.get_record_key_lookup(self.article)
5848
self.assertEqual(key, "article")
5949

fastapi_startkit/tests/masoniteorm/sqlite/relationships/test_morph_to_many.py

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,6 @@ async def test_morph_map_uses_registry(self):
6363
self.assertIn("product_m2m", morph_map)
6464

6565
async def test_apply_query_resolves_article(self):
66-
from fastapi_startkit.masoniteorm.models.model import Model as BaseModel
6766

6867
like = await LikeModelMorphToMany.create({"likeable_type": "article_m2m", "likeable_id": self.article.id})
6968
like_loaded = await LikeModelMorphToMany.where("id", like.id).first()

0 commit comments

Comments
 (0)