partial_menu 0.3.0 → 1.0.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 497fcb02fc093221933584484fc1bee9994a6117
4
- data.tar.gz: 7ac39d10ef5207c92b7bc049619bcee9748abca9
3
+ metadata.gz: 51707929a107b5a59d3fc4ccce357f1a8833ec79
4
+ data.tar.gz: cc8327e327f9ab7fcc716003d320dd2f5133408b
5
5
  SHA512:
6
- metadata.gz: 7462b4e8009ec5acbbc8e94f36ef1d4e6b2bcda277eca8d92fd548e7668086db0c8230a85a7339a8879aaa9d3bfbdad12893513a3f115ca49d750d20d7813e35
7
- data.tar.gz: 419e533d62c76d7d4fd87ca9964883fd0c65f91fce0cd6e6eeb8fc87e00bfbfcca1af7d0949ae87ecb4ac3ae4e0119a2b8b571d604de600d065517e8785ee716
6
+ metadata.gz: e811b68b27e208262c1d56a3928b33cae54492b2fc0d6b66dbb878e8d82e5c075b467141b99cd12375da9dbd3eacbf470af04e5d0c4adc30ccef8f1d20c97824
7
+ data.tar.gz: 32c9fd3d571ac174e7f63c5e8dd3b1e3824dd0e08fcb97085eaed7c961b7dde4d1dbad6c75a40653d89fbf26cc025a826a16be11c813e200078ad2835f359c7e
@@ -1,12 +1,12 @@
1
- <li <%= 'class="active"' if current_menu?(item.uri)%>>
2
- <% if item.try(:icon).present? %>
3
- <i class="fa fa-fw <%= item.icon %>"></i>
4
- <% end %>
5
- <% if item.uri.present? %>
1
+ <li class="<%= 'active' if is_active_link?(url_for(item.uri), :inclusive) %>">
2
+ <% if item.try(:icon).present? -%>
3
+ <i class="<%= item.try(:icontype) || 'fa' %> <%= item.try(:icontype) || 'fa'%>-fw <%= item.icon %>"></i>
4
+ <% end -%>
5
+ <% if item.uri.present? -%>
6
6
  <%= link_to item.uri do -%>
7
7
  <%= item.title %>
8
8
  <% end -%>
9
- <% else %>
9
+ <% else -%>
10
10
  <%= item.title %>
11
11
  <% end -%>
12
12
  </li>
@@ -1,9 +1,11 @@
1
1
  <ul>
2
- <% menu.items.each do |item| %>
3
- <% if item.has_submenu? %>
4
- <%= render "#{menu.type}_menu/submenu", menu: item.submenu %>
5
- <% else %>
6
- <%= render "#{menu.type}_menu/item", item: item %>
7
- <% end %>
8
- <% end %>
2
+ <% menu.items.each do |item| -%>
3
+ <% if item.submenu? -%>
4
+ <%= render "#{menu.layout}_menu/submenu", menu: item.submenu -%>
5
+ <% elsif item.separator?%>
6
+ <%= render "#{menu.layout}_menu/separator", item: item -%>
7
+ <% else -%>
8
+ <%= render "#{menu.layout}_menu/item", item: item -%>
9
+ <% end -%>
10
+ <% end -%>
9
11
  </ul>
@@ -0,0 +1 @@
1
+ <li><hr/></li>
@@ -1,12 +1,20 @@
1
1
  <li class="nav-item" title="<%= menu.parent.title %>" data-original-title="<%= menu.parent.title %>">
2
2
  <a class="nav-link nav-link-collapse collapsed" data-toggle="collapse"
3
- href="#collapse<%= menu.id %>" data-parent="#<%= menu.type%>">
3
+ href="#collapse<%= menu.id %>" data-parent="#<%= menu.layout %>">
4
4
  <% if menu.parent.try(:icon).present? %>
5
- <i class="fa fa-fw <%= menu.parent.icon %>"></i>
5
+ <i class="<%= menu.parent.try(:icontype) || 'fa'%> <%= menu.parent.try(:icontype) || 'fa'%>-fw <%= menu.parent.icon %>"></i>
6
6
  <% end -%>
7
7
  <span class="nav-link-text"><%= menu.parent.title %></span>
8
8
  </a>
9
+ <ul id="#<%= menu.layout %>">
9
10
  <% menu.items.each do |item| %>
10
- <%= render "#{menu.type}_menu/submenu_item", item: item %>
11
+ <% if item.submenu? -%>
12
+ <%= render "#{menu.layout}_menu/submenu", menu: item.submenu -%>
13
+ <% elsif item.separator?%>
14
+ <%= render "#{menu.layout}_menu/separator", item: item -%>
15
+ <% else -%>
16
+ <%= render "#{menu.layout}_menu/submenu_item", item: item -%>
17
+ <% end -%>
11
18
  <% end %>
12
- </li>
19
+ </ul>
20
+ </li>
@@ -1,12 +1,12 @@
1
- <li <%= 'class="active"' if current_menu?(item.uri)%>>
2
- <% if item.try(:icon).present? %>
1
+ <li class="<%= 'active' if is_active_link?(item.uri, :inclusive) %>">
2
+ <% if item.try(:icon).present? -%>
3
3
  <i class="fa fa-fw <%= item.icon %>"></i>
4
- <% end %>
5
- <% if item.uri.present? %>
4
+ <% end -%>
5
+ <% if item.uri.present? -%>
6
6
  <%= link_to item.uri do -%>
7
7
  <%= item.title %>
8
8
  <% end -%>
9
- <% else %>
9
+ <% else -%>
10
10
  <%= item.title %>
11
11
  <% end -%>
12
12
  </li>
@@ -6,6 +6,7 @@ menu:
6
6
  icon: test
7
7
  - second_item: separator
8
8
  - third_item:
9
+ icontype: glyph
9
10
  menu:
10
11
  - first_submenu: link
11
12
  - second_submenu:
@@ -3,20 +3,30 @@ require 'partial_menu/menu_item'
3
3
  module PartialMenu
4
4
  # Represents a menu with menu items
5
5
  class Menu
6
- attr_reader :type
6
+ attr_reader :layout
7
7
  attr_reader :items
8
8
  attr_reader :parent
9
9
  attr_reader :id
10
10
 
11
- def initialize(props, type = 'main', parent = nil)
12
- raise "Expected a array but got #{props.class}" unless props.is_a? Array
13
- raise "Expected a string but got #{type.class}" unless type.is_a? String
14
- @type = type
11
+ # rubocop:disable Metrics/MethodLength
12
+ def initialize(props, layout = 'main', parent = nil)
13
+ unless props.is_a? Array
14
+ raise ::ArgumentError, "Expected an Array, got #{props.class}"
15
+ end
16
+ unless layout.is_a? String
17
+ raise ::ArgumentError, "Expected a String, got #{layout.class}"
18
+ end
19
+ unless parent.nil? || (parent.is_a? PartialMenu::MenuItem)
20
+ raise ::ArgumentError, "Expected MenuItem or nil, got #{parent.class}"
21
+ end
22
+ @props = props
23
+ @layout = layout
15
24
  @items = []
16
25
  @parent = parent
17
26
  set_id
18
27
  add_items(props)
19
28
  end
29
+ # rubocop:enable Metrics/MethodLength
20
30
 
21
31
  private
22
32
 
@@ -30,7 +40,7 @@ module PartialMenu
30
40
  @id = if @parent.try(:class) == 'PartialMenu::MenuItem'
31
41
  @parent.id + '_submenu'
32
42
  else
33
- @type + '_menu'
43
+ @layout + '_menu'
34
44
  end
35
45
  end
36
46
  end
@@ -1,80 +1,144 @@
1
+ require 'active_link_to'
2
+
1
3
  module PartialMenu
2
4
  # Represents menu item in a menu
3
5
  class MenuItem
6
+ include ActiveLinkTo
7
+
4
8
  attr_reader :type
5
9
  attr_reader :parent
6
10
  attr_reader :uri
7
11
  attr_reader :title
8
12
  attr_reader :submenu
9
13
  attr_reader :id
14
+ attr_reader :options
10
15
 
16
+ ##
17
+ # Contructor which check parameter types and start setup
18
+ #
19
+ # @param [Hash] props The menu list propertiess from the yaml file.
20
+ # @param [PartialMenu::Menu] parent The parent Menu of the item
21
+ #
22
+ # Throws ArgumentError if parameters are not expected type
23
+ #
11
24
  def initialize(props, parent)
12
- raise 'Expected a hash as props' unless props.is_a?(Hash)
13
- raise 'Expected a PartialMenu::Menu as parent' unless parent.is_a?(
14
- PartialMenu::Menu
15
- )
25
+ unless props.is_a? Hash
26
+ raise ::ArgumentError, "Expected Hash, got #{prop.class}"
27
+ end
28
+ unless parent.is_a? PartialMenu::Menu
29
+ raise ::ArgumentError, "Expected PartialMenu::Menu, got #{parent.class}"
30
+ end
16
31
  @parent = parent
17
32
  @props = props
18
33
  setup
19
34
  end
20
35
 
21
- def setup
22
- set_type
23
- set_title
24
- set_id
25
- set_uri
26
- set_submenu
27
- set_other_attr
28
- end
29
-
36
+ ##
37
+ # True if menu item has submenu
38
+ #
39
+ # @return [boolean]
40
+ #
30
41
  def submenu?
31
42
  @submenu != nil
32
43
  end
33
44
 
45
+ ##
46
+ # True if menu item is a separator
47
+ #
48
+ # @ return [boolean]
49
+ #
34
50
  def separator?
35
51
  @type == :separator
36
52
  end
37
53
 
38
- def active?
39
- false
40
- end
41
-
42
54
  private
43
55
 
56
+ ##
57
+ # Manage parsing order of yaml properties
58
+ #
59
+ def setup
60
+ set_first_entry
61
+ set_type
62
+ set_id
63
+ set_title
64
+ set_uri
65
+ set_submenu
66
+ set_other_attr
67
+ end
68
+
69
+ ##
70
+ # Check if there is a +:menu+ property for the menu item and intantiate a
71
+ # new <tt>PartialMenu::Menu</tt> to parse it's content.
72
+ #
44
73
  def set_submenu
45
74
  @submenu = nil
46
- return if @props.key?(:menu)
75
+ return unless @props.key?(:menu)
47
76
  @submenu = PartialMenu::Menu.new(
48
77
  @props[:menu],
49
- @parent.type,
78
+ @parent.layout,
50
79
  self
51
80
  )
52
81
  end
53
82
 
83
+ ##
84
+ # Sets type property
85
+ # * +:separator+ - if menu item is has +separator+ entry only
86
+ # * +:link+ - any other occasion
87
+ #
54
88
  def set_type
55
- @_first_entry = @props.shift
56
89
  @type = :item
57
90
  @type = @_first_entry[1].to_sym if @_first_entry[1] == 'separator'
58
91
  end
59
92
 
93
+ ##
94
+ # Gets and first entry in the yaml file for this particular menu item as it
95
+ # could hold esential information about the entry
96
+ #
97
+ def set_first_entry
98
+ @_first_entry = @props.shift
99
+ end
100
+
101
+ ##
102
+ # Set the title of the menu item based ont the +title+ property from the
103
+ # yaml file or if that not exists, use the yaml entry's name titlelized. If
104
+ # menu item is a separator, :title will be an empty string.
105
+ #
60
106
  def set_title
61
107
  @title = ''
62
- return unless @type != :separator
108
+ return if @type == :separator
63
109
  @title = @props[:title] if @props.key?(:title)
64
- @title = @_first_entry[0].to_s.titleize
110
+ @title = @id.titleize if @title.blank?
65
111
  end
66
112
 
113
+ ##
114
+ # Sets the menu item id to the entry's name in the yaml
115
+ #
67
116
  def set_id
68
117
  @id = @_first_entry[0].to_s
69
118
  end
70
119
 
120
+ ##
121
+ # Set the URL of the menu item if it is not a separator.
122
+ #
71
123
  def set_uri
72
124
  @uri = nil
73
125
  @uri = @props[:uri] if @type != :separator
74
126
  end
75
127
 
128
+ ##
129
+ # Parse additional attributes from yaml
130
+ #
131
+ # If a menu entry defines any addtional attributes above the list below,
132
+ # it will be parsed to an instance variable of the MenuItem.
133
+ #
134
+ # Built-in properties:
135
+ # * +:id+
136
+ # * +:uri+
137
+ # * +:title+
138
+ # * +:menu+
139
+ #
76
140
  def set_other_attr
77
- @props.except(@id, :uri, :title, :menu).each do |name, value|
141
+ @props.except(:id, :uri, :title, :menu).each do |name, value|
78
142
  instance_variable_set('@' + name.to_s, value)
79
143
  define_singleton_method(name) { instance_variable_get("@#{name}") }
80
144
  end
@@ -4,8 +4,8 @@ module PartialMenu
4
4
  # Binding to Rails
5
5
  class Railtie < ::Rails::Railtie
6
6
  initializer 'partial_menu.view_helpers' do
7
- ActiveSupport.on_load(:action_view) do
8
- PartialMenu::ViewHelpers
7
+ ActiveSupport.on_load :action_view do
8
+ include PartialMenu::ViewHelpers
9
9
  end
10
10
  end
11
11
 
@@ -1,3 +1,3 @@
1
1
  module PartialMenu
2
- VERSION = '0.3.0'.freeze
2
+ VERSION = '1.0.0'.freeze
3
3
  end
@@ -6,51 +6,70 @@ module PartialMenu
6
6
  # This module serves for availability in ActionView templates. It also adds a
7
7
  # new view helper: +partial_menu+.
8
8
  #
9
- # == Using the helper without arguments
10
- # If the helper is called without passing in the type, it will
11
- # render the default menu using the default view partials.
12
- #
13
- # Example:
14
- #
15
- # <%= partial_menu %>
16
- #
17
- # ... will result in <tt>menu.yaml</tt> getting displayed using partials
18
- # from +app/views/partial_menu+:
19
- #
20
- # <ul>
21
- # <li><a href="/">First item</a></li>
22
- # ...
23
- # </ul>
24
- #
25
- # Any options defined in the call will be send to +render+ as local variables
26
- #
27
- # Example:
28
- #
29
- # <%= partial_menu 'menu', {menu_id:'sidemenu'} %>
30
- #
31
- # You can leave out menu type parameter:
32
- #
33
- # Example:
34
- #
35
- # <%= partial_menu {menu_id:'sidemenu'} %>
36
- #
37
9
  module ViewHelpers
38
- def partial_menu(type = 'main', options = {}) #:nodoc:
10
+ # rubocop:disable Metrics/MethodLength
11
+ # == Using the helper
12
+ # If the helper is called without passing in the type, it will
13
+ # render the default menu using the default view partials.
14
+ #
15
+ # Example:
16
+ #
17
+ # <%= partial_menu %>
18
+ #
19
+ # ... will result in <tt>menu.yaml</tt> getting displayed using partials
20
+ # from +app/views/partial_menu+:
21
+ #
22
+ # <ul>
23
+ # <li><a href="/">First item</a></li>
24
+ # ...
25
+ # </ul>
26
+ #
27
+ # Any options defined in the call will be send to +render+ as
28
+ # local variables
29
+ #
30
+ # Example:
31
+ #
32
+ # <%= partial_menu 'menu', {menu_id:'sidemenu'} %>
33
+ #
34
+ # You can leave out menu type parameter:
35
+ #
36
+ # Example:
37
+ #
38
+ # <%= partial_menu {menu_id:'sidemenu'} %>
39
+ #
40
+ def partial_menu(type = 'main', options = {})
41
+ unless (type.is_a? String) || (type.is_a? Hash)
42
+ raise ::ArgumentError, "Expected a String or Hash, got #{type.class}"
43
+ end
44
+ unless options.is_a? Hash
45
+ raise ::ArgumentError, "Expected a Hash, got #{options.class}"
46
+ end
39
47
  if type.is_a? Hash
40
48
  options = type
41
49
  type = 'main'
42
50
  end
43
- options[:menu] = PartialMenu::Menu.new(load_menu_from_yaml(type))
44
- options.symbolize_keys!
51
+ options[:menu] = PartialMenu::Menu.new(load_menu_from_yaml(type), type)
52
+ options.deep_symbolize_keys!
45
53
  render partial: "#{type}_menu/menu", locals: options
46
54
  end
47
-
48
- def current_menu?(path)
49
- request.path == URI(path).path unless path.blank?
50
- end
55
+ # rubocop:enable Metrics/MethodLength
51
56
 
52
57
  private
53
58
 
59
+ ##
60
+ # Load yaml file from config
61
+ #
62
+ # The +type+ parameter is used to create te actual file name. It is
63
+ # expected to:
64
+ # * be in the Rails apps config/ folder
65
+ # * have a name like +type>_menu.yml+
66
+ #
67
+ # All keys are symbolized after loading hte file.
68
+ #
69
+ # @param [String] type The menu type, which identifies the file too
70
+ #
71
+ # @return [Hash] The parsed YAML as hash of objects
72
+ #
54
73
  def load_menu_from_yaml(type)
55
74
  Psych.load_file(
56
75
  Rails.root.join("config/#{type}_menu.yml")
data/lib/partial_menu.rb CHANGED
@@ -1,9 +1,6 @@
1
+ require 'partial_menu/version'
2
+
1
3
  # PartailMenu implementation class
2
4
  module PartialMenu
3
- end
4
-
5
- if defined?(Rails::Railtie)
6
- require 'partial_menu/railtie'
7
- elsif defined?(Rails::Initializer)
8
- raise 'partial_menu is not compatible with Rails 2.3 or older'
5
+ require 'partial_menu/railtie' if defined?(Rails)
9
6
  end
metadata CHANGED
@@ -1,15 +1,29 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: partial_menu
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.0
4
+ version: 1.0.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Peter Nagy
8
8
  autorequire:
9
9
  bindir: bin
10
10
  cert_chain: []
11
- date: 2018-08-08 00:00:00.000000000 Z
11
+ date: 2018-08-14 00:00:00.000000000 Z
12
12
  dependencies:
13
+ - !ruby/object:Gem::Dependency
14
+ name: active_link_to
15
+ requirement: !ruby/object:Gem::Requirement
16
+ requirements:
17
+ - - "~>"
18
+ - !ruby/object:Gem::Version
19
+ version: '1.0'
20
+ type: :runtime
21
+ prerelease: false
22
+ version_requirements: !ruby/object:Gem::Requirement
23
+ requirements:
24
+ - - "~>"
25
+ - !ruby/object:Gem::Version
26
+ version: '1.0'
13
27
  - !ruby/object:Gem::Dependency
14
28
  name: rails
15
29
  requirement: !ruby/object:Gem::Requirement
@@ -94,6 +108,20 @@ dependencies:
94
108
  - - "~>"
95
109
  - !ruby/object:Gem::Version
96
110
  version: '0.58'
111
+ - !ruby/object:Gem::Dependency
112
+ name: simplecov
113
+ requirement: !ruby/object:Gem::Requirement
114
+ requirements:
115
+ - - "~>"
116
+ - !ruby/object:Gem::Version
117
+ version: '0'
118
+ type: :development
119
+ prerelease: false
120
+ version_requirements: !ruby/object:Gem::Requirement
121
+ requirements:
122
+ - - "~>"
123
+ - !ruby/object:Gem::Version
124
+ version: '0'
97
125
  - !ruby/object:Gem::Dependency
98
126
  name: sqlite3
99
127
  requirement: !ruby/object:Gem::Requirement
@@ -124,6 +152,7 @@ files:
124
152
  - lib/generators/views/USAGE
125
153
  - lib/generators/views/templates/_item.html.erb
126
154
  - lib/generators/views/templates/_menu.html.erb
155
+ - lib/generators/views/templates/_separator.html.erb
127
156
  - lib/generators/views/templates/_submenu.html.erb
128
157
  - lib/generators/views/templates/_submenu_item.html.erb
129
158
  - lib/generators/views/views_generator.rb