2016-01-12 18:43:00 -06:00
|
|
|
module ApplicationHelper
|
2016-01-18 15:10:25 -06:00
|
|
|
|
2016-04-04 14:28:23 -05:00
|
|
|
# Given a model or collection of models, returns them with the given decorator. If a block is passed, yields the
|
|
|
|
# decorated objects as well
|
|
|
|
#
|
|
|
|
# Useful in this context:
|
|
|
|
# <% decorate(@model_obj, ModelObjDecorator) do |model_obj| %>
|
|
|
|
# <%= model_obj.decorator_method %>
|
|
|
|
# <% end %>
|
|
|
|
def decorate(obj, decorator_class)
|
|
|
|
decorated = decorator_class.decorate(obj, self)
|
|
|
|
yield(decorated) if block_given?
|
|
|
|
decorated
|
|
|
|
end
|
|
|
|
|
2016-01-18 15:10:25 -06:00
|
|
|
def timestamp(time)
|
|
|
|
time ? time.strftime('%D %R') : ''
|
|
|
|
end
|
|
|
|
|
2016-01-12 18:43:00 -06:00
|
|
|
def nav_items
|
2016-03-03 13:12:42 -06:00
|
|
|
nav = [
|
2016-01-12 18:43:00 -06:00
|
|
|
nav_item('Recipes', recipes_path, 'recipes'),
|
2016-01-30 21:20:15 -06:00
|
|
|
nav_item('Ingredients', ingredients_path, 'ingredients'),
|
2016-08-15 17:43:02 -05:00
|
|
|
nav_item('Logs', logs_path, 'logs'),
|
2016-01-31 00:00:32 -06:00
|
|
|
nav_item('Calculator', calculator_path, 'calculator'),
|
2016-01-30 21:20:15 -06:00
|
|
|
nav_item('About', about_path, 'home')
|
2016-01-12 18:43:00 -06:00
|
|
|
]
|
2016-03-03 13:12:42 -06:00
|
|
|
|
|
|
|
if current_user && current_user.admin?
|
|
|
|
nav << nav_item('Admin', admin_users_path, 'admin/users')
|
|
|
|
end
|
2016-03-03 13:30:34 -06:00
|
|
|
|
|
|
|
nav
|
2016-01-12 18:43:00 -06:00
|
|
|
end
|
|
|
|
|
2016-01-19 16:50:39 -06:00
|
|
|
def profile_nav_items
|
|
|
|
if current_user
|
|
|
|
[content_tag('li', class: 'dropdown') do
|
|
|
|
li_cnt = ''.html_safe
|
|
|
|
|
|
|
|
li_cnt << link_to('#', class: 'dropdown-toggle', data: {toggle: 'dropdown'}, role: 'button') do
|
|
|
|
''.html_safe << "#{current_user.display_name}" << content_tag('span', '', class: 'caret')
|
|
|
|
end
|
|
|
|
|
|
|
|
li_cnt << content_tag('ul', class: 'dropdown-menu') do
|
|
|
|
''.html_safe << nav_item('Profile', edit_user_path) << nav_item('Logout', logout_path)
|
|
|
|
end
|
|
|
|
|
|
|
|
li_cnt
|
|
|
|
end]
|
|
|
|
else
|
|
|
|
[nav_item('Login', login_path)]
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def nav_item(name, url, controller = nil)
|
2016-01-12 18:43:00 -06:00
|
|
|
content_tag('li', link_to(name, url), class: active_for_controller(controller))
|
|
|
|
end
|
|
|
|
|
|
|
|
def active_for_controller(controller)
|
|
|
|
if current_controller == controller
|
|
|
|
'active'
|
|
|
|
else
|
|
|
|
''
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def current_controller
|
|
|
|
params[:controller]
|
|
|
|
end
|
|
|
|
end
|