Module: Alchemy::Admin::NavigationHelper

Included in:
BaseHelper
Defined in:
app/helpers/alchemy/admin/navigation_helper.rb

Overview

This module contains helper methods for rendering the admin navigation.

Instance Method Summary collapse

Instance Method Details

#admin_subnavigationObject

Renders the subnavigation from current module

We find the module from current controller and index action.



23
24
25
26
27
28
29
30
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 23

def admin_subnavigation
  if current_alchemy_module.present?
    render(
      'alchemy/admin/partials/sub_navigation',
      entries: current_sub_navigation
    )
  end
end

#alchemy_main_navigation_entry(alchemy_module) ⇒ Object

Renders one admin main navigation entry

Parameters:

  • alchemy_module (Hash)

    The Hash representing a Alchemy module



11
12
13
14
15
16
17
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 11

def alchemy_main_navigation_entry(alchemy_module)
  render(
    'alchemy/admin/partials/main_navigation_entry',
    alchemy_module: alchemy_module.stringify_keys,
    navigation: module_main_navigation(alchemy_module)
  )
end

#entry_active?(entry) ⇒ Boolean

Returns true if given navi entry is in params controller and action

Example

<%= entry_active?({controller: 'admin/users', action: 'index'}) %>

Parameters:

  • A (Hash)

    Alchemy module definition navigation entry

Returns:

  • (Boolean)


73
74
75
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 73

def entry_active?(entry)
  is_entry_controller_active?(entry) && is_entry_action_active?(entry)
end

CSS classes for main navigation entry.



58
59
60
61
62
63
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 58

def main_navigation_css_classes(navigation)
  [
    'main_navi_entry',
    admin_mainnavi_active?(navigation) ? 'active' : nil
  ].compact.join(' ')
end

Used for checking the main navi permissions

To let your module be navigatable by the user you have to provide an Ability for it.

Example:

# module.yml
name: 'my_module'
navigation: {
  controller: 'my/admin/posts'
  action: 'index'
}

# ability.rb
can :index, :my_admin_posts


48
49
50
51
52
53
54
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 48

def navigate_module(navigation)
  navigation.stringify_keys!
  [
    navigation['action'].to_sym,
    navigation['controller'].to_s.gsub(/\A\//, '').gsub(/\//, '_').to_sym
  ]
end

#sorted_alchemy_modulesObject

Alchemy modules for main navigation.

Sorted by position attribute, if given.



115
116
117
118
119
120
121
122
123
124
125
126
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 115

def sorted_alchemy_modules
  sorted = []
  not_sorted = []
  alchemy_modules.map do |m|
    if m['position'].blank?
      not_sorted << m
    else
      sorted << m
    end
  end
  sorted.sort_by { |m| m['position'] } + not_sorted
end

#url_for_module(alchemy_module) ⇒ Object

Returns url for given Alchemy module.

If the module is inside an engine it calls the url_for helper on the engines routing proxy.

If the module is inside the host rails app it calls the url_for helper on the main_app routing proxy.

Parameters:

  • A (Hash)

    Alchemy module definition



86
87
88
89
90
91
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 86

def url_for_module(alchemy_module)
  route_from_engine_or_main_app(
    alchemy_module['engine_name'],
    url_options_for_module(alchemy_module)
  )
end

#url_for_module_sub_navigation(navigation) ⇒ Object

Returns url for given Alchemy module sub navigation entry.

If the module is inside an engine it calls the url_for helper on the engines routing proxy.

If the module is inside the host rails app it calls the url_for helper on the main_app routing proxy.

Parameters:

  • A (Hash)

    Alchemy module sub navigation definition



102
103
104
105
106
107
108
109
# File 'app/helpers/alchemy/admin/navigation_helper.rb', line 102

def url_for_module_sub_navigation(navigation)
  alchemy_module = module_definition_for(navigation)
  return if alchemy_module.nil?
  route_from_engine_or_main_app(
    alchemy_module['engine_name'],
    url_options_for_navigation_entry(navigation)
  )
end