diff options
author | Azul <azul@leap.se> | 2014-07-05 11:41:43 +0200 |
---|---|---|
committer | Azul <azul@leap.se> | 2014-07-05 11:54:11 +0200 |
commit | bdb4b0e275c205b0b44bbe3cc4ec4c162b309b37 (patch) | |
tree | 1b92aadd34f890a6332812a01a39769e24f4a733 | |
parent | 87e9ccbcdf4f99dd898b0715750092a27fff7e94 (diff) |
make link_to_navigation more generic and reuse it
Use link_to_navigation for all important navigation items. It creates a link in a list item for use with bootstrap. It supports an :active flag and an :icon option in the html_options now. It also translates the label. This way it can be used in a lot of places as the generic navigation link.
-rw-r--r-- | app/helpers/navigation_helper.rb | 35 | ||||
-rw-r--r-- | app/views/common/_navigation_item.html.haml | 10 | ||||
-rw-r--r-- | app/views/layouts/_header.html.haml | 6 | ||||
-rw-r--r-- | app/views/layouts/_navigation.html.haml | 15 | ||||
-rw-r--r-- | engines/support/app/helpers/tickets_helper.rb | 42 |
5 files changed, 67 insertions, 41 deletions
diff --git a/app/helpers/navigation_helper.rb b/app/helpers/navigation_helper.rb index 19cb934..779ce58 100644 --- a/app/helpers/navigation_helper.rb +++ b/app/helpers/navigation_helper.rb @@ -1,19 +1,19 @@ module NavigationHelper # - # used to create a side navigation link. + # Create a navigation link. # - # Signature is the same as link_to, except it accepts an :active value in the html_options + # Signature is the same as link_to, except... + # * it accepts an :active flag in the html_options + # * it accepts an :icon string in the html_options + # * the label (first arg) will be translated # def link_to_navigation(*args) - if args.last.is_a? Hash - html_options = args.pop.dup - active_class = html_options.delete(:active) ? 'active' : nil - html_options[:class] = [html_options[:class], active_class].join(' ') - args << html_options - else - active_class = nil - end + html_options = args.extract_options! || {} + active_class = extract_active_class!(html_options) + icon = extract_icon!(html_options) + args[0] = icon + translate(args[0], cascade: true) + args << html_options if html_options.present? content_tag :li, :class => active_class do link_to(*args) end @@ -59,6 +59,21 @@ module NavigationHelper private + def extract_active_class!(options) + active_class = options.delete(:active) ? 'active' : nil + options[:class] = [options[:class], active_class].compact.join(' ') + active_class + end + + def extract_icon!(options) + icon = options.delete(:icon) + if icon.present? + content_tag(:i, '', class: 'icon-'+ icon) + else + "" + end + end + def controller_string @controller_string ||= params[:controller].to_s.gsub(/^\//, '') end diff --git a/app/views/common/_navigation_item.html.haml b/app/views/common/_navigation_item.html.haml index 02c54c8..39b20d7 100644 --- a/app/views/common/_navigation_item.html.haml +++ b/app/views/common/_navigation_item.html.haml @@ -1,3 +1,9 @@ +-# +-# A very simple navigation link. It takes a symbol, uses it for the +-# translation, path and determining if the link is active. +-# +-# For something more complex use link_to_navigation directly instead. +-# - item = navigation_item.to_s -%li{:class => ("active" if controller?(item))} - = link_to t(".#{item}", cascade: true), polymorphic_url(item) += link_to_navigation ".#{item}", polymorphic_url(item), + active: controller?(item) diff --git a/app/views/layouts/_header.html.haml b/app/views/layouts/_header.html.haml index ff14af9..7b7999a 100644 --- a/app/views/layouts/_header.html.haml +++ b/app/views/layouts/_header.html.haml @@ -1,8 +1,8 @@ - if admin? %ul.nav.nav-tabs - = render partial: 'common/navigation_item', collection: [:users, :identities, :tickets] - %li - = link_to t(:logout), logout_path, :method => :delete + = render partial: 'common/navigation_item', + collection: [:users, :identities, :tickets] + = link_to_navigation :logout, logout_path, :method => :delete - if @user && @show_navigation .lead = @user.email_address diff --git a/app/views/layouts/_navigation.html.haml b/app/views/layouts/_navigation.html.haml index 94f71f7..dccba0c 100644 --- a/app/views/layouts/_navigation.html.haml +++ b/app/views/layouts/_navigation.html.haml @@ -1,7 +1,12 @@ %ul.nav.sidenav - = link_to_navigation t(:overview), @user, :active => (controller?(:users) && action?(:show)) - = link_to_navigation t(:account_settings), edit_user_path(@user), :active => (controller?(:users) && !action?(:show)) + = link_to_navigation :overview, @user, + active: (controller?(:users) && action?(:show)) + = link_to_navigation :account_settings, edit_user_path(@user), + active: (controller?(:users) && !action?(:show)) - # will want link for identity settings - = link_to_navigation t(".tickets"), auto_tickets_path, :active => controller?(:tickets) - = link_to_navigation t(:billing_settings), billing_top_link(@user), :active => controller?(:customer, :payments, :subscriptions, :credit_card_info) if APP_CONFIG[:billing] - = link_to_navigation t(:logout), logout_path, :method => :delete + = link_to_navigation ".tickets", auto_tickets_path, + active: controller?(:tickets) + - if APP_CONFIG[:billing] + = link_to_navigation :billing_settings, billing_top_link(@user), + active: controller?(:customer, :payments, :subscriptions, :credit_card_info) + = link_to_navigation :logout, logout_path, method: :delete diff --git a/engines/support/app/helpers/tickets_helper.rb b/engines/support/app/helpers/tickets_helper.rb index 11b02e4..7db31dc 100644 --- a/engines/support/app/helpers/tickets_helper.rb +++ b/engines/support/app/helpers/tickets_helper.rb @@ -35,32 +35,32 @@ module TicketsHelper # def link_to_status(new_status) - label = t(:".#{new_status}", cascade: true) - link_to label, auto_tickets_path(:open_status => new_status, :sort_order => search_order) + label = ".#{new_status}" + link_to_navigation label, auto_tickets_path(open_status: new_status, sort_order: search_order) end def link_to_order(order_field) - if search_order.start_with?(order_field) - # link for currently-filtered field. Link to other direction of this field. - if search_order.end_with? 'asc' - direction = 'desc' - icon_direction = 'up' - else - direction = 'asc' - icon_direction = 'down' - end - arrow = content_tag(:i, '', class: 'icon-arrow-'+ icon_direction) - else - # for not-currently-filtered field, don't display an arrow, and link to descending direction - arrow = '' - direction = 'desc' - end + direction = new_direction_for_order(order_field) + icon = icon_for_direction(direction) + # for not-currently-filtered field link to descending direction + direction ||= 'desc' + label = ".#{order_field}" + link_to_navigation label, auto_tickets_path(sort_order: order_field + '_at_' + direction, open_status: search_status), + icon: icon + end - label = t(:".#{order_field}", cascade: true) - link_to auto_tickets_path(:sort_order => order_field + '_at_' + direction, :open_status => search_status) do - arrow + label - end + def new_direction_for_order(order_field) + # return if we're not filtering by this field + return unless search_order.start_with?(order_field) + # Link to the other direction for the filtered field. + search_order.end_with?('asc') ? 'desc' : 'asc' + end + + def icon_for_direction(direction) + # Don't display an icon if we do not filter this field + return if direction.blank? + direction == 'asc' ? 'arrow-down' : 'arrow-up' end end |