alacarte 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/.gitignore +4 -0
- data/Gemfile +6 -0
- data/README.textile +104 -0
- data/Rakefile +2 -0
- data/alacarte.gemspec +21 -0
- data/app/helpers/alacarte_helper.rb +37 -0
- data/lib/alacarte.rb +9 -0
- data/lib/alacarte/engine.rb +9 -0
- data/lib/alacarte/menu.rb +64 -0
- data/lib/alacarte/menus.rb +14 -0
- data/lib/alacarte/rails.rb +9 -0
- data/lib/alacarte/version.rb +3 -0
- metadata +79 -0
data/.gitignore
ADDED
data/Gemfile
ADDED
data/README.textile
ADDED
@@ -0,0 +1,104 @@
|
|
1
|
+
h1. Alacarte
|
2
|
+
|
3
|
+
Alacarte allows you to setup menu's and conditions in your Rails app in a router-like DSL way.
|
4
|
+
|
5
|
+
You can easily;
|
6
|
+
|
7
|
+
* group menu's by name
|
8
|
+
* set conditions on menu items as a group
|
9
|
+
* set conditions on menu items with @:if@ and @:unless@
|
10
|
+
* nest your menu's
|
11
|
+
|
12
|
+
h2. Installation
|
13
|
+
|
14
|
+
Alacarte is being developed against Rails 3.0.3. No other versions have been tested.
|
15
|
+
|
16
|
+
To install, add Alacarte to your @Gemfile@ and run `bundle install`:
|
17
|
+
|
18
|
+
<pre>
|
19
|
+
gem 'alacarte'
|
20
|
+
</pre>
|
21
|
+
|
22
|
+
After alacarte is installed, you will need to setup a rails initialiser to define your menu.
|
23
|
+
|
24
|
+
h3. Initialiser
|
25
|
+
|
26
|
+
Add an initialiser file to your rails application; e.g. @config/initialisers/alacarte.rb@ and add the menus in that file using the draw method.
|
27
|
+
|
28
|
+
<pre>
|
29
|
+
YourApplication::Application.menus.draw do
|
30
|
+
menu :main do
|
31
|
+
# menu information goes here...
|
32
|
+
end
|
33
|
+
end
|
34
|
+
</pre>
|
35
|
+
|
36
|
+
h2. How it works
|
37
|
+
|
38
|
+
h3. Helper environment
|
39
|
+
|
40
|
+
Alacarte is linked to your helper environment, therefor you can call any helper method that is available to your rails app within your menu definition file;
|
41
|
+
|
42
|
+
* format helpers
|
43
|
+
* url_for, generated paths based on your routes file, ...
|
44
|
+
* I18n
|
45
|
+
* authentication related calls (e.g. current_user for Devise or Authlogic)
|
46
|
+
|
47
|
+
h3. options
|
48
|
+
|
49
|
+
You can pass in options to any level of your menu, including the top level. Here are some examples of options;
|
50
|
+
|
51
|
+
* @type@ specify what type it is, currently @menu@, @link@ and @span@ are supported
|
52
|
+
* @name@ allows you to specify the name of the menu item, a @deep_name@ is derived, based on the parents of that item.
|
53
|
+
* @path@ allows you to set the path of that link
|
54
|
+
* @label@ sets the label of the menu. By default Alacarte tries to translate the @deep_name@ (with an alacarte.menus namespace)
|
55
|
+
* @html@ are options passed in as html options to the given element (e.g. class or id attributes)
|
56
|
+
* @group@ are options passed in as html options, when the current element apears to be a group of other elements
|
57
|
+
* @if@ allows you to pass in a proc to test if the menu item is valid
|
58
|
+
* @unless@ allows you to pass in a proc to test if the menu item is valid, but tests for a false
|
59
|
+
|
60
|
+
h3. Menu example
|
61
|
+
|
62
|
+
<pre>
|
63
|
+
YourApplication::Application.menus.draw do
|
64
|
+
|
65
|
+
menu :language do
|
66
|
+
link :nl, root_path(:locale => :nl), :html => { :class => 'nl' }
|
67
|
+
link :fr, root_path(:locale => :fr), :html => { :class => 'fr' }
|
68
|
+
end
|
69
|
+
|
70
|
+
menu :main, :group => { :class => 'main' } do
|
71
|
+
link :home, root_path
|
72
|
+
link :recent,
|
73
|
+
link :other_site, 'http://someurl.com'
|
74
|
+
# ...
|
75
|
+
|
76
|
+
link :my_account, account_path(:current), :if => { current_user }
|
77
|
+
|
78
|
+
end
|
79
|
+
|
80
|
+
end
|
81
|
+
</pre>
|
82
|
+
|
83
|
+
h3. Render the menu
|
84
|
+
|
85
|
+
Render the menu where you like in your view and pass an optional "current element" selector (in this case the current locale);
|
86
|
+
|
87
|
+
<pre>
|
88
|
+
...
|
89
|
+
<div id="language_selection">
|
90
|
+
<%= navigation_for(:language, I18n.locale) %>
|
91
|
+
</div>
|
92
|
+
...
|
93
|
+
</pre>
|
94
|
+
|
95
|
+
h3. Output example
|
96
|
+
|
97
|
+
The output is generated as a ul-li list with anchor elements. If there is an element that can be matched, it will get a class active;
|
98
|
+
|
99
|
+
<pre>
|
100
|
+
<ul>
|
101
|
+
<li><a href="/" class="nl active">nl</a></li>
|
102
|
+
<li><a href="/" class="nl">fr</a></li>
|
103
|
+
</ul>
|
104
|
+
</pre>
|
data/Rakefile
ADDED
data/alacarte.gemspec
ADDED
@@ -0,0 +1,21 @@
|
|
1
|
+
# -*- encoding: utf-8 -*-
|
2
|
+
$:.push File.expand_path("../lib", __FILE__)
|
3
|
+
require "alacarte/version"
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = "alacarte"
|
7
|
+
s.version = Alacarte::VERSION
|
8
|
+
s.platform = Gem::Platform::RUBY
|
9
|
+
s.authors = ["Stijn Mathysen"]
|
10
|
+
s.email = ["stijn@skylight.be"]
|
11
|
+
s.homepage = "http://github.com/stijnster/alacarte"
|
12
|
+
s.summary = %q{Provides a generic menu system for Rails}
|
13
|
+
s.description = %q{This Rails plugin allows you to create a menu system, using a dsl (similar to routes).}
|
14
|
+
|
15
|
+
s.rubyforge_project = "alacarte"
|
16
|
+
|
17
|
+
s.files = `git ls-files`.split("\n")
|
18
|
+
s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
|
19
|
+
s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
|
20
|
+
s.require_paths = ["lib"]
|
21
|
+
end
|
@@ -0,0 +1,37 @@
|
|
1
|
+
module AlacarteHelper
|
2
|
+
|
3
|
+
def navigation_for(menu, current_element = nil)
|
4
|
+
current_element = controller_name if current_element.blank?
|
5
|
+
|
6
|
+
if current_menu = Alacarte.menus[menu]
|
7
|
+
current_menu.build(self)
|
8
|
+
|
9
|
+
return elements_for(current_menu, current_element.to_sym)
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
def elements_for(menu, current_element)
|
14
|
+
result = ''
|
15
|
+
|
16
|
+
menu.items.each do |item|
|
17
|
+
if item.valid?
|
18
|
+
_html_options = item.html_options || {}
|
19
|
+
_html_options[:class] = _html_options[:class].to_s.insert(0, 'active ').strip if (current_element == item.as)
|
20
|
+
|
21
|
+
_item = case item.type
|
22
|
+
when :link: link_to(item.label.html_safe, item.path, _html_options)
|
23
|
+
else content_tag(item.type, item.label, _html_options)
|
24
|
+
end
|
25
|
+
|
26
|
+
if item.items.size > 0
|
27
|
+
_item << elements_for(item, current_element)
|
28
|
+
end
|
29
|
+
|
30
|
+
result << content_tag(:li, _item)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
result.blank? ? '' : content_tag(:ul, result.html_safe, menu.group_options)
|
35
|
+
end
|
36
|
+
|
37
|
+
end
|
data/lib/alacarte.rb
ADDED
@@ -0,0 +1,64 @@
|
|
1
|
+
module Alacarte
|
2
|
+
|
3
|
+
class Menu
|
4
|
+
|
5
|
+
VALID_ELEMENTS = [:link, :span]
|
6
|
+
|
7
|
+
cattr_reader :env
|
8
|
+
attr_reader :parent, :type, :name, :deep_name, :path, :as, :label, :options, :items, :block, :html_options, :group_options
|
9
|
+
|
10
|
+
def initialize(parent, type, *args, &block)
|
11
|
+
@options = args.extract_options!.dup
|
12
|
+
@parent = parent
|
13
|
+
@type = type
|
14
|
+
@name = options[:name] || args[0]
|
15
|
+
@path = options[:path] || args[1]
|
16
|
+
@deep_name = @parent ? "#{@parent.deep_name}.#{@name.to_s}" : @name.to_s
|
17
|
+
@label = options[:label] || I18n.t("alacarte.menus.#{@deep_name}", :default => @deep_name.to_s)
|
18
|
+
@as = options[:as] || @name
|
19
|
+
@block = block if block_given?
|
20
|
+
@html_options = options[:html]
|
21
|
+
@group_options = options[:group]
|
22
|
+
|
23
|
+
build
|
24
|
+
end
|
25
|
+
|
26
|
+
def block?
|
27
|
+
!!@block
|
28
|
+
end
|
29
|
+
|
30
|
+
def self.env?
|
31
|
+
!!@@env
|
32
|
+
end
|
33
|
+
|
34
|
+
def build(env = nil)
|
35
|
+
@@env = env if env
|
36
|
+
@items = []
|
37
|
+
|
38
|
+
self.instance_eval(&@block) if Menu.env? && self.block?
|
39
|
+
end
|
40
|
+
|
41
|
+
def valid?
|
42
|
+
if options[:if] && options[:if].respond_to?(:call)
|
43
|
+
return options[:if].call
|
44
|
+
end
|
45
|
+
|
46
|
+
if options[:unless] && options[:unless].respond_to?(:call)
|
47
|
+
return !options[:unless].call
|
48
|
+
end
|
49
|
+
|
50
|
+
return true
|
51
|
+
end
|
52
|
+
|
53
|
+
def method_missing(id, *args, &block)
|
54
|
+
if Menu.env? && Menu.env.respond_to?(id)
|
55
|
+
@@env.send(id, *args, &block)
|
56
|
+
elsif VALID_ELEMENTS.member?(id)
|
57
|
+
@items << Menu.new(self, id, *args, &block)
|
58
|
+
else
|
59
|
+
super
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
end
|
metadata
ADDED
@@ -0,0 +1,79 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: alacarte
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
hash: 29
|
5
|
+
prerelease: false
|
6
|
+
segments:
|
7
|
+
- 0
|
8
|
+
- 0
|
9
|
+
- 1
|
10
|
+
version: 0.0.1
|
11
|
+
platform: ruby
|
12
|
+
authors:
|
13
|
+
- Stijn Mathysen
|
14
|
+
autorequire:
|
15
|
+
bindir: bin
|
16
|
+
cert_chain: []
|
17
|
+
|
18
|
+
date: 2010-12-17 00:00:00 +01:00
|
19
|
+
default_executable:
|
20
|
+
dependencies: []
|
21
|
+
|
22
|
+
description: This Rails plugin allows you to create a menu system, using a dsl (similar to routes).
|
23
|
+
email:
|
24
|
+
- stijn@skylight.be
|
25
|
+
executables: []
|
26
|
+
|
27
|
+
extensions: []
|
28
|
+
|
29
|
+
extra_rdoc_files: []
|
30
|
+
|
31
|
+
files:
|
32
|
+
- .gitignore
|
33
|
+
- Gemfile
|
34
|
+
- README.textile
|
35
|
+
- Rakefile
|
36
|
+
- alacarte.gemspec
|
37
|
+
- app/helpers/alacarte_helper.rb
|
38
|
+
- lib/alacarte.rb
|
39
|
+
- lib/alacarte/engine.rb
|
40
|
+
- lib/alacarte/menu.rb
|
41
|
+
- lib/alacarte/menus.rb
|
42
|
+
- lib/alacarte/rails.rb
|
43
|
+
- lib/alacarte/version.rb
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/stijnster/alacarte
|
46
|
+
licenses: []
|
47
|
+
|
48
|
+
post_install_message:
|
49
|
+
rdoc_options: []
|
50
|
+
|
51
|
+
require_paths:
|
52
|
+
- lib
|
53
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
54
|
+
none: false
|
55
|
+
requirements:
|
56
|
+
- - ">="
|
57
|
+
- !ruby/object:Gem::Version
|
58
|
+
hash: 3
|
59
|
+
segments:
|
60
|
+
- 0
|
61
|
+
version: "0"
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
none: false
|
64
|
+
requirements:
|
65
|
+
- - ">="
|
66
|
+
- !ruby/object:Gem::Version
|
67
|
+
hash: 3
|
68
|
+
segments:
|
69
|
+
- 0
|
70
|
+
version: "0"
|
71
|
+
requirements: []
|
72
|
+
|
73
|
+
rubyforge_project: alacarte
|
74
|
+
rubygems_version: 1.3.7
|
75
|
+
signing_key:
|
76
|
+
specification_version: 3
|
77
|
+
summary: Provides a generic menu system for Rails
|
78
|
+
test_files: []
|
79
|
+
|