simple_menu 0.1.1
Sign up to get free protection for your applications and to get access to all the features.
- data/lib/simple_menu.rb +76 -0
- data/lib/simple_menu/railtie.rb +9 -0
- data/lib/simple_menu/version.rb +3 -0
- data/lib/simple_menu/view_helpers.rb +7 -0
- metadata +115 -0
data/lib/simple_menu.rb
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
require 'active_support/core_ext'
|
2
|
+
require 'nokogiri'
|
3
|
+
|
4
|
+
require 'simple_menu/railtie' if defined?(Rails)
|
5
|
+
require 'simple_menu/version'
|
6
|
+
|
7
|
+
class SimpleMenu
|
8
|
+
def initialize(template, &block)
|
9
|
+
self.template = template
|
10
|
+
self.menu = []
|
11
|
+
self.options = {
|
12
|
+
save_with: Nokogiri::XML::Node::SaveOptions::FORMAT |
|
13
|
+
Nokogiri::XML::Node::SaveOptions::NO_DECLARATION |
|
14
|
+
Nokogiri::XML::Node::SaveOptions::NO_EMPTY_TAGS,
|
15
|
+
indent: 2
|
16
|
+
}
|
17
|
+
|
18
|
+
add(&block) if block_given?
|
19
|
+
end
|
20
|
+
|
21
|
+
def add(index = menu.length, &block)
|
22
|
+
menu.insert(index, instance_eval(&block))
|
23
|
+
end
|
24
|
+
|
25
|
+
def before(href_or_name, &block)
|
26
|
+
add(index_of(href_or_name), &block)
|
27
|
+
end
|
28
|
+
|
29
|
+
def after(href_or_name, &block)
|
30
|
+
add(index_of(href_or_name) + 1, &block)
|
31
|
+
end
|
32
|
+
|
33
|
+
def method_missing(name, *args, &block)
|
34
|
+
template.send(name, *args, &block)
|
35
|
+
end
|
36
|
+
|
37
|
+
def to_s
|
38
|
+
Nokogiri::XML::Builder.new do |xml|
|
39
|
+
xml.ul do
|
40
|
+
each do |item|
|
41
|
+
xml.li do
|
42
|
+
xml << highlight_current(item)
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end.to_xml(options).html_safe
|
47
|
+
end
|
48
|
+
|
49
|
+
private
|
50
|
+
attr_accessor :menu, :template, :options
|
51
|
+
delegate :each, to: :menu
|
52
|
+
|
53
|
+
def element(item)
|
54
|
+
Nokogiri::HTML(item).children.search('body > *')
|
55
|
+
end
|
56
|
+
|
57
|
+
def href(element)
|
58
|
+
href = element.attribute('href')
|
59
|
+
href.present? ? href.value : ''
|
60
|
+
end
|
61
|
+
|
62
|
+
def highlight_current(item)
|
63
|
+
element(item).tap do |element|
|
64
|
+
element.add_class(:active) if element.present? && template.url_for == href(element)
|
65
|
+
end.to_html
|
66
|
+
end
|
67
|
+
|
68
|
+
def index_of(href_or_name)
|
69
|
+
each.with_index do |item, index|
|
70
|
+
element = element(item)
|
71
|
+
next unless element.present?
|
72
|
+
|
73
|
+
return index if element.text.match(href_or_name) || href(element).match(href_or_name)
|
74
|
+
end.length
|
75
|
+
end
|
76
|
+
end
|
metadata
ADDED
@@ -0,0 +1,115 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: simple_menu
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
prerelease:
|
6
|
+
platform: ruby
|
7
|
+
authors:
|
8
|
+
- Mario Uher
|
9
|
+
autorequire:
|
10
|
+
bindir: bin
|
11
|
+
cert_chain: []
|
12
|
+
date: 2012-02-23 00:00:00.000000000 Z
|
13
|
+
dependencies:
|
14
|
+
- !ruby/object:Gem::Dependency
|
15
|
+
name: actionpack
|
16
|
+
requirement: &70327159113800 !ruby/object:Gem::Requirement
|
17
|
+
none: false
|
18
|
+
requirements:
|
19
|
+
- - ~>
|
20
|
+
- !ruby/object:Gem::Version
|
21
|
+
version: '3.0'
|
22
|
+
type: :runtime
|
23
|
+
prerelease: false
|
24
|
+
version_requirements: *70327159113800
|
25
|
+
- !ruby/object:Gem::Dependency
|
26
|
+
name: activesupport
|
27
|
+
requirement: &70327159113300 !ruby/object:Gem::Requirement
|
28
|
+
none: false
|
29
|
+
requirements:
|
30
|
+
- - ~>
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: '3.0'
|
33
|
+
type: :runtime
|
34
|
+
prerelease: false
|
35
|
+
version_requirements: *70327159113300
|
36
|
+
- !ruby/object:Gem::Dependency
|
37
|
+
name: nokogiri
|
38
|
+
requirement: &70327159112920 !ruby/object:Gem::Requirement
|
39
|
+
none: false
|
40
|
+
requirements:
|
41
|
+
- - ! '>='
|
42
|
+
- !ruby/object:Gem::Version
|
43
|
+
version: '0'
|
44
|
+
type: :runtime
|
45
|
+
prerelease: false
|
46
|
+
version_requirements: *70327159112920
|
47
|
+
- !ruby/object:Gem::Dependency
|
48
|
+
name: minitest
|
49
|
+
requirement: &70327159112360 !ruby/object:Gem::Requirement
|
50
|
+
none: false
|
51
|
+
requirements:
|
52
|
+
- - ! '>='
|
53
|
+
- !ruby/object:Gem::Version
|
54
|
+
version: '0'
|
55
|
+
type: :development
|
56
|
+
prerelease: false
|
57
|
+
version_requirements: *70327159112360
|
58
|
+
- !ruby/object:Gem::Dependency
|
59
|
+
name: purdytest
|
60
|
+
requirement: &70327159111780 !ruby/object:Gem::Requirement
|
61
|
+
none: false
|
62
|
+
requirements:
|
63
|
+
- - ! '>='
|
64
|
+
- !ruby/object:Gem::Version
|
65
|
+
version: '0'
|
66
|
+
type: :development
|
67
|
+
prerelease: false
|
68
|
+
version_requirements: *70327159111780
|
69
|
+
- !ruby/object:Gem::Dependency
|
70
|
+
name: rails
|
71
|
+
requirement: &70327159127380 !ruby/object:Gem::Requirement
|
72
|
+
none: false
|
73
|
+
requirements:
|
74
|
+
- - ~>
|
75
|
+
- !ruby/object:Gem::Version
|
76
|
+
version: '3.0'
|
77
|
+
type: :development
|
78
|
+
prerelease: false
|
79
|
+
version_requirements: *70327159127380
|
80
|
+
description: Super simple and bulletproof menus for your rails app.
|
81
|
+
email: uher.mario@gmail.com
|
82
|
+
executables: []
|
83
|
+
extensions: []
|
84
|
+
extra_rdoc_files: []
|
85
|
+
files:
|
86
|
+
- lib/simple_menu/railtie.rb
|
87
|
+
- lib/simple_menu/version.rb
|
88
|
+
- lib/simple_menu/view_helpers.rb
|
89
|
+
- lib/simple_menu.rb
|
90
|
+
homepage: https://www.github.com/haihappen/simple_menu
|
91
|
+
licenses: []
|
92
|
+
post_install_message:
|
93
|
+
rdoc_options: []
|
94
|
+
require_paths:
|
95
|
+
- lib
|
96
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
97
|
+
none: false
|
98
|
+
requirements:
|
99
|
+
- - ! '>='
|
100
|
+
- !ruby/object:Gem::Version
|
101
|
+
version: '0'
|
102
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
103
|
+
none: false
|
104
|
+
requirements:
|
105
|
+
- - ! '>='
|
106
|
+
- !ruby/object:Gem::Version
|
107
|
+
version: '0'
|
108
|
+
requirements: []
|
109
|
+
rubyforge_project:
|
110
|
+
rubygems_version: 1.8.11
|
111
|
+
signing_key:
|
112
|
+
specification_version: 3
|
113
|
+
summary: Super simple and bulletproof menus for your rails app.
|
114
|
+
test_files: []
|
115
|
+
has_rdoc:
|