badges in the navbar!

This commit is contained in:
nilsding 2014-11-10 23:46:51 +01:00
parent f8c81f679d
commit 4d35d96d1c
2 changed files with 39 additions and 6 deletions

View file

@ -1,6 +1,23 @@
module ApplicationHelper
def nav_entry(body, path)
content_tag(:li, link_to(body, path), class: ('active' if current_page? path))
def nav_entry(body, path, options = {})
options = {
badge: nil,
badge_color: nil,
icon: nil
}.merge(options)
unless options[:icon].nil?
body = "#{content_tag(:i, '', class: "mdi-#{options[:icon]}")} #{body}"
end
unless options[:badge].nil?
# TODO: make this prettier?
body << " #{
content_tag(:span, options[:badge], class: ("badge#{
" badge-#{options[:badge_color]}" unless options[:badge_color].nil?
}"))}"
end
content_tag(:li, link_to(body.html_safe, path), class: ('active' if current_page? path))
end
##
@ -15,4 +32,17 @@ module ApplicationHelper
c
end
end
def inbox_count
return 0 unless user_signed_in?
puts current_user.id
count = Inbox.select("COUNT(id) AS count")
.where(new: true)
.where(user_id: current_user.id)
.group(:user_id)
.order(:count)
.first.count
return nil unless count > 0
count
end
end

View file

@ -9,9 +9,11 @@
%a.navbar-brand{href: "/"}= APP_CONFIG['site_name']
#j2-main-navbar-collapse.collapse.navbar-collapse
%ul.nav.navbar-nav
= nav_entry "Front", "/"
%ul.nav.navbar-nav.navbar-right
- if user_signed_in?
= nav_entry "Front", root_path
- if user_signed_in?
%ul.nav.navbar-nav
= nav_entry "Inbox", "/inbox", badge: inbox_count, badge_color: 'danger'
%ul.nav.navbar-nav.navbar-right
%li.dropdown
%a.dropdown-toggle{href: "#", "data-toggle" => "dropdown"}
= current_user.screen_name
@ -21,6 +23,7 @@
%li= link_to "Settings", edit_user_registration_path
%li.divider
%li= link_to "Logout", destroy_user_session_path, method: :delete
- else
- else
%ul.nav.navbar-nav.navbar-right
= nav_entry "Sign in", new_user_session_path
= nav_entry "Sign up", new_user_registration_path