Skip to content

Commit

Permalink
allow easy override of Relation for a class
Browse files Browse the repository at this point in the history
  • Loading branch information
tpett committed Sep 8, 2011
1 parent d34d805 commit 877b530
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 1 deletion.
3 changes: 2 additions & 1 deletion pyperry/base.py
Expand Up @@ -386,6 +386,7 @@ class Article(pyperry.base.Base):
"""

__metaclass__ = BaseMeta
_relation_class = Relation

def __init__(self, fields=None, new_record=True, **kwargs):
"""
Expand Down Expand Up @@ -806,7 +807,7 @@ def relation(cls):
"""
if not hasattr(cls, '_relation') or cls._relation.klass != cls:
cls._relation = Relation(cls)
cls._relation = cls._relation_class(cls)
return cls._relation

@classmethod
Expand Down
12 changes: 12 additions & 0 deletions tests/base_test.py
Expand Up @@ -879,6 +879,18 @@ def test_fresh(self):
test.reload()
self.assertEqual(test.last_relation.query()['fresh'], True)

class RelationOverrideTestCase(BaseTestCase):

def test_uses_config_option(self):
class MyRelation(pyperry.relation.Relation): pass
class Test(pyperry.Base):
_relation_class = MyRelation

self.assertEqual(Test.relation().__class__, MyRelation)
self.assertEqual(
pyperry.base.Base.relation().__class__,
pyperry.relation.Relation)


##
# Freezing objects
Expand Down

0 comments on commit 877b530

Please sign in to comment.