Skip to content

Commit

Permalink
All menus now should be instances, not classes anymore, this will avo…
Browse files Browse the repository at this point in the history
…id mistakes
  • Loading branch information
rafaelcanovas committed Mar 28, 2014
1 parent 89a104e commit 01892f9
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 16 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,9 @@ class BankMenu(alacarte.Menu):
group = 'main'
label = 'Bank'
submenus = (
BankTransactionsMenu,
BankBalanceMenu,
BankPremiumMenu,
BankTransactionsMenu(),
BankBalanceMenu(),
BankPremiumMenu(),
)

def shown(self):
Expand Down
7 changes: 5 additions & 2 deletions alacarte/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,15 @@


def register(menu_class):
# Instantiate the menu class before registering it
menu = menu_class()

try:
group = menu_class.group
group = menu.group
except AttributeError:
group = ''

MENU_REGISTRY[group].append(menu_class)
MENU_REGISTRY[group].append(menu)


def get_menus(group):
Expand Down
19 changes: 8 additions & 11 deletions alacarte/menus.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,36 +2,33 @@


class Menu:
@classmethod
def shown(cls):
def shown(self):
"""
All menus are shown by default.
Override this method to implement custom behavior.
"""
return True

@classmethod
def url(cls):
def url(self):
"""
Try to reverse `url_name`, fallback to '#' if not possible.
"""
try:
return reverse(cls.url_name)
return reverse(self.url_name)
except AttributeError:
return '#'
except NoReverseMatch:
raise

@classmethod
def is_active(cls):
def is_active(self):
"""
A menu is active either if its `url_name` is the current or if
any of its `submenus` are active.
"""
url = sub_urls = False
if hasattr(cls, 'url_name'):
url = reverse(cls.url_name) == cls.context['request'].path
if hasattr(cls, 'submenus'):
sub_urls = any([s.is_active() for s in cls.submenus])
if hasattr(self, 'url_name'):
url = reverse(self.url_name) == self.context['request'].path
if hasattr(self, 'submenus'):
sub_urls = any([s.is_active() for s in self.submenus])

return url or sub_urls

0 comments on commit 01892f9

Please sign in to comment.