activemenu 0.3.0 → 0.4.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +34 -1
- data/lib/active_menu/menu.rb +7 -6
- data/lib/active_menu/registry.rb +17 -2
- data/lib/active_menu/version.rb +1 -1
- data/lib/active_menu.rb +6 -2
- data/spec/lib/active_menu/menu_spec.rb +12 -0
- data/spec/lib/active_menu/registry_spec.rb +12 -0
- data/spec/lib/active_menu_spec.rb +7 -1
- metadata +1 -1
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA1:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: 2f5aa010d8d62f74c8feaeae635875b296ed144f
|
4
|
+
data.tar.gz: 247873f2e994ba74026223b77763f6fe595f18e2
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 176346f8915333413ec023f744680092bfedfa7707934a682c1f7c20e96f66aa9c02332ced6d3433c8bf8f374362e6d02dd2ad75f3c8ff522ace72ece19e18ad
|
7
|
+
data.tar.gz: 75856397839244d9dc73fc64ebc0048fbdd9c4e07be4ddead72c9ff5e5204c45d81449bf7e4b67b6108c736f4f11fb1d17f5263a4d10d9ff33c645311bf5cbc5
|
data/README.md
CHANGED
@@ -20,11 +20,44 @@ Or install it yourself as:
|
|
20
20
|
|
21
21
|
## Usage
|
22
22
|
|
23
|
+
### Creating menus objects
|
24
|
+
|
25
|
+
```ruby
|
26
|
+
require 'active_menu'
|
27
|
+
ActiveMenu::create(:mymenu)
|
28
|
+
#....
|
29
|
+
# In another gem you can use
|
30
|
+
@menu = ActiveMenu::get(:mymenu).submenu do |sub|
|
31
|
+
sub.content == 'My content'
|
32
|
+
end
|
33
|
+
```
|
34
|
+
### exists?
|
35
|
+
```ruby
|
36
|
+
ActiveMenu::create(:someid)
|
37
|
+
ActiveMenu::exists?(:someid) # == true
|
38
|
+
ActiveMenu::exists?(:this_id_doesnt_exists) # == false
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
### Set options to the menu
|
43
|
+
These options are write to a hash, that you can use with other gem to render it.
|
23
44
|
```ruby
|
24
|
-
@menu = ActiveMenu::
|
45
|
+
@menu = ActiveMenu::get(:someid)
|
46
|
+
@menu.options[:tag] = :div
|
47
|
+
@menu.options[:myoptions] = 'myvalue'
|
48
|
+
```
|
49
|
+
|
50
|
+
### Get the menu
|
51
|
+
```ruby
|
52
|
+
# You can retrieve the menu instance with the method get but you can use a block too.
|
53
|
+
@menu = ActiveMenu::get(:someid)
|
54
|
+
ActiveMenu::get(:someid) do |menu|
|
55
|
+
menu.id # The menu id
|
56
|
+
end
|
25
57
|
```
|
26
58
|
|
27
59
|
|
60
|
+
### Nested menus
|
28
61
|
```ruby
|
29
62
|
@menu = ActiveMenu::Menu.new(:mainmenu, 'http://example.com')
|
30
63
|
# def initialize(id, href = nil, content=nil, submenus=[], parent=nil, &block) .... yield(self) if block_given?
|
data/lib/active_menu/menu.rb
CHANGED
@@ -1,22 +1,23 @@
|
|
1
1
|
class ActiveMenu::Menu
|
2
2
|
|
3
|
-
attr_accessor :id, :href, :content, :submenus, :parent
|
3
|
+
attr_accessor :id, :href, :content, :submenus, :parent, :options
|
4
4
|
|
5
|
-
def initialize(id, href = nil, content=nil,
|
5
|
+
def initialize(id, href = nil, content=nil, options={}, &block)
|
6
6
|
@id = id.to_sym
|
7
7
|
@href = href
|
8
|
-
@submenus = []
|
9
8
|
@content = content
|
9
|
+
@submenus = []
|
10
|
+
@options = options
|
10
11
|
|
11
12
|
#sets the parent
|
12
13
|
submenus.each {|s| s.parent = self }
|
13
14
|
@submenus = submenus
|
14
|
-
@parent = parent
|
15
15
|
yield(self) if block_given?
|
16
16
|
end
|
17
17
|
|
18
|
-
def submenu(id, href, content=nil,
|
19
|
-
sm = self.class.new(id, href, content,
|
18
|
+
def submenu(id, href, content=nil, options={}, &block)
|
19
|
+
sm = self.class.new(id, href, content, options, &block)
|
20
|
+
sm.parent = self
|
20
21
|
@submenus << sm
|
21
22
|
sm
|
22
23
|
end
|
data/lib/active_menu/registry.rb
CHANGED
@@ -18,9 +18,24 @@ class ActiveMenu::Registry
|
|
18
18
|
@menus = []
|
19
19
|
end
|
20
20
|
|
21
|
-
def get(id)
|
21
|
+
def get(id, &block)
|
22
22
|
id = id.to_sym
|
23
|
-
@menus.select {|m| m.id == id}
|
23
|
+
selected = @menus.select {|m| m.id == id}
|
24
|
+
if selected.length >= 1
|
25
|
+
yield(selected.first) if block_given?
|
26
|
+
selected.first
|
27
|
+
else
|
28
|
+
false
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
def exists?(id)
|
33
|
+
id = id.to_sym
|
34
|
+
if self.get(id)
|
35
|
+
true
|
36
|
+
else
|
37
|
+
false
|
38
|
+
end
|
24
39
|
end
|
25
40
|
|
26
41
|
end
|
data/lib/active_menu/version.rb
CHANGED
data/lib/active_menu.rb
CHANGED
@@ -9,6 +9,8 @@ describe ActiveMenu::Menu do
|
|
9
9
|
it {should respond_to(:content)}
|
10
10
|
it {should respond_to(:submenus)}
|
11
11
|
it {should respond_to(:parent)}
|
12
|
+
it {should respond_to(:options)}
|
13
|
+
|
12
14
|
|
13
15
|
its(:submenus) {should be_a(Array)}
|
14
16
|
|
@@ -17,6 +19,8 @@ describe ActiveMenu::Menu do
|
|
17
19
|
@menu = ActiveMenu::Menu.new(:idtest, "http://example.com", "My menu")
|
18
20
|
end
|
19
21
|
|
22
|
+
#it 'can spec'
|
23
|
+
|
20
24
|
it 'has the right attributes values' do
|
21
25
|
@menu.content.should == 'My menu'
|
22
26
|
@menu.id.should == :idtest
|
@@ -53,4 +57,12 @@ describe ActiveMenu::Menu do
|
|
53
57
|
|
54
58
|
end
|
55
59
|
|
60
|
+
|
61
|
+
it 'can add options to a hash' do
|
62
|
+
@menu.options[:tag] = :div
|
63
|
+
@menu.options[:tag].should == :div
|
64
|
+
@menu.options[:tag] = :ul
|
65
|
+
@menu.options[:tag].should == :ul
|
66
|
+
end
|
67
|
+
|
56
68
|
end
|
@@ -5,6 +5,11 @@ describe 'ActiveMenu::Registry' do
|
|
5
5
|
it {should respond_to(:menus)}
|
6
6
|
it {should be_a_kind_of(Singleton)}
|
7
7
|
|
8
|
+
|
9
|
+
before :each do
|
10
|
+
ActiveMenu::Registry.instance.reset
|
11
|
+
end
|
12
|
+
|
8
13
|
it 'can reset the menus to zero' do
|
9
14
|
ActiveMenu::Registry.instance.create(:idmenu, 'http://example.com')
|
10
15
|
ActiveMenu::Registry.instance.menus.length == 1
|
@@ -12,4 +17,11 @@ describe 'ActiveMenu::Registry' do
|
|
12
17
|
ActiveMenu::Registry.instance.menus.length == 0
|
13
18
|
end
|
14
19
|
|
20
|
+
it 'can get a menu and yield it to a block' do
|
21
|
+
ActiveMenu::Registry.instance.create(:idmenu, 'http://example.com')
|
22
|
+
ActiveMenu::get(:idmenu) do |m|
|
23
|
+
m.id.should == :idmenu
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
15
27
|
end
|
@@ -24,5 +24,11 @@ describe ActiveMenu do
|
|
24
24
|
m2 = ActiveMenu::get('admin-nav')
|
25
25
|
m1.should == m2
|
26
26
|
end
|
27
|
-
|
27
|
+
|
28
|
+
it 'can check if a menu exists.' do
|
29
|
+
ActiveMenu::create(:someid)
|
30
|
+
ActiveMenu::exists?(:someid).should == true
|
31
|
+
ActiveMenu::exists?(:this_id_doesnt_exists).should == false
|
32
|
+
end
|
33
|
+
|
28
34
|
end
|