Skip to content

Commit

Permalink
Merge 580f0dd into 9fb2bc3
Browse files Browse the repository at this point in the history
  • Loading branch information
josephmancuso committed Sep 9, 2018
2 parents 9fb2bc3 + 580f0dd commit 787c681
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 0 deletions.
8 changes: 8 additions & 0 deletions masonite/view.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ def __init__(self, container):
self.environments = []
self.extension = '.html'
self._filters = {}
self._tests = {}

def render(self, template, dictionary={}):
"""Get the string contents of the view.
Expand All @@ -71,6 +72,9 @@ def render(self, template, dictionary={}):
# Check if composers are even set for a speed improvement
if self.composers:
self._update_from_composers()

if self._tests:
self.env.tests.update(self._tests)

self.rendered_template = self._render()

Expand Down Expand Up @@ -218,6 +222,10 @@ def filter(self, name, function):
"""

self._filters.update({name: function})

def test(self, key, obj):
self._tests.update({key: obj})
return self

def __load_environment(self, template):
"""Private method for loading all the environments.
Expand Down
1 change: 1 addition & 0 deletions resources/templates/admin_test.html
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{% if user is admin %}True{% else %}False{% endif %}
22 changes: 22 additions & 0 deletions tests/test_view.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,3 +208,25 @@ def test_cache_throws_exception_with_incorrect_cache_type(self):
view(
'test_exception', {'test': 'test'}
).cache_for(1, 'monthss')

def test_can_add_tests_to_view(self):
view = self.container.make('ViewClass')

view.test('admin', self._is_admin)

assert view._tests == {'admin': self._is_admin}

user = MockAdminUser
assert view.render(
'admin_test', {'user': user}).rendered_template == 'True'

user.admin = 0

assert view.render(
'admin_test', {'user': user}).rendered_template == 'False'

def _is_admin(self, obj):
return obj.admin == 1

class MockAdminUser:
admin = 1

0 comments on commit 787c681

Please sign in to comment.