simple-navigation-ext 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
Files changed (46) hide show
  1. data/CHANGELOG +177 -0
  2. data/README +22 -0
  3. data/Rakefile +48 -0
  4. data/VERSION +1 -0
  5. data/generators/navigation_config/USAGE +1 -0
  6. data/generators/navigation_config/navigation_config_generator.rb +8 -0
  7. data/generators/navigation_config/templates/config/navigation.rb +67 -0
  8. data/lib/generators/navigation_config/navigation_config_generator.rb +12 -0
  9. data/lib/simple-navigation.rb +1 -0
  10. data/lib/simple_navigation/adapters/base.rb +37 -0
  11. data/lib/simple_navigation/adapters/padrino.rb +20 -0
  12. data/lib/simple_navigation/adapters/rails.rb +94 -0
  13. data/lib/simple_navigation/adapters/sinatra.rb +68 -0
  14. data/lib/simple_navigation/adapters.rb +9 -0
  15. data/lib/simple_navigation/core/configuration.rb +70 -0
  16. data/lib/simple_navigation/core/item.rb +117 -0
  17. data/lib/simple_navigation/core/item_adapter.rb +63 -0
  18. data/lib/simple_navigation/core/item_container.rb +146 -0
  19. data/lib/simple_navigation/core/items_provider.rb +35 -0
  20. data/lib/simple_navigation/core.rb +5 -0
  21. data/lib/simple_navigation/rails_controller_methods.rb +144 -0
  22. data/lib/simple_navigation/rendering/helpers.rb +82 -0
  23. data/lib/simple_navigation/rendering/renderer/base.rb +71 -0
  24. data/lib/simple_navigation/rendering/renderer/breadcrumbs.rb +37 -0
  25. data/lib/simple_navigation/rendering/renderer/links.rb +25 -0
  26. data/lib/simple_navigation/rendering/renderer/list.rb +47 -0
  27. data/lib/simple_navigation/rendering.rb +10 -0
  28. data/lib/simple_navigation.rb +162 -0
  29. data/rails/init.rb +1 -0
  30. data/spec/lib/simple_navigation/adapters/padrino_spec.rb +29 -0
  31. data/spec/lib/simple_navigation/adapters/rails_spec.rb +284 -0
  32. data/spec/lib/simple_navigation/adapters/sinatra_spec.rb +60 -0
  33. data/spec/lib/simple_navigation/core/configuration_spec.rb +122 -0
  34. data/spec/lib/simple_navigation/core/item_adapter_spec.rb +209 -0
  35. data/spec/lib/simple_navigation/core/item_container_spec.rb +396 -0
  36. data/spec/lib/simple_navigation/core/item_spec.rb +513 -0
  37. data/spec/lib/simple_navigation/core/items_provider_spec.rb +60 -0
  38. data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +249 -0
  39. data/spec/lib/simple_navigation/rendering/helpers_spec.rb +183 -0
  40. data/spec/lib/simple_navigation/rendering/renderer/base_spec.rb +199 -0
  41. data/spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb +58 -0
  42. data/spec/lib/simple_navigation/rendering/renderer/links_spec.rb +58 -0
  43. data/spec/lib/simple_navigation/rendering/renderer/list_spec.rb +189 -0
  44. data/spec/lib/simple_navigation_spec.rb +307 -0
  45. data/spec/spec_helper.rb +96 -0
  46. metadata +158 -0
data/CHANGELOG ADDED
@@ -0,0 +1,177 @@
1
+ *3.1.1
2
+
3
+ * Item#selected_by_url? now strips anchors from the item's url before comparing it with the current request's url. Credits to opengovernment.
4
+
5
+ *3.1.0
6
+
7
+ * added new helper method 'active_navigation_item_name' to render the name of the currently active item
8
+
9
+ *3.0.2
10
+
11
+ * dom_id and dom_class can now be set as option in the item's definition (useful for dynamic menu items).
12
+
13
+ *3.0.1
14
+
15
+ * allow controller instance variables named @template for rails3 apps. Credits to cmartyn.
16
+ * added possibility to specify and item's URL as a block which is evaulated after the :if and :unless conditions have been checked. Credits to Nicholas Firth-McCoy.
17
+ * upgraded to rspec 2.0.0
18
+ * fixed cgi error in sinatra adapter. Credits to Jack Dempsey.
19
+
20
+ *3.0.0
21
+
22
+ * added ability to specify dynamic items using an array of hashes. Credits to Anshul Khandelwal for input and discussion.
23
+ * added ability to specify attributes for the generated link-tag in list renderer (up to now the attributes have been applied to the li-tag). Credits to Anthony Navarre for input and discussion.
24
+
25
+ *3.0.0.beta2
26
+
27
+ * moving code for initializing plugin in sinatra to separate gem
28
+
29
+ *3.0.0.beta1
30
+
31
+ * moving deprecated rails controller methods (navigation, current_navigation) to separate file 'rails_controller_methods'. Deprecations removed. File can be required explicitly if controller methods should be still available.
32
+ * decoupling from Rails. Introducing the concept of adapters to work with several frameworks.
33
+ * tested with Rails 3.0.0
34
+ * adding support for Sinatra and Padrino frameworks.
35
+ * cherry picking active_support stuff instead of requiring the whole bunch (tested with active_support >= 2.3.2 and 3.0.0)
36
+ * created public sample project which includes demo for Rails2, Rails3, Sinatra and Padrino (will be available on github soon)
37
+ * better src file organization (adapters/core/rendering folders)
38
+
39
+ *2.7.3
40
+
41
+ * initializing SimpleNavigation.config_file_path with empty array (was nil before). Allows for adding paths before gem has been initialized.
42
+
43
+ *2.7.2
44
+
45
+ * added ability to have more than one config_file_path (useful if simple-navigation is used as a part of another gem/plugin). Credits to Luke Imhoff.
46
+
47
+ *2.7.1
48
+
49
+ * added SimpleNavigation.request and SimpleNavigation.request_uri as abstraction for getting request_uri (rails2/rails3)
50
+ * use request.fullpath instead of request.request_uri for Rails3. Credits to Ben Langfeld.
51
+
52
+ *2.7.0
53
+
54
+ * added new option :highlights_on to item definition in config-file. Specify a regexp which is matched against the current_uri to determine if an item is active or not. Replaces explicit highlighting in controllers.
55
+ * deprecated explicit highlighting in the controllers.
56
+
57
+ *2.6.0
58
+
59
+ * added rendering option 'skip_if_empty' to Renderer::List to avoid rendering of empty ul-tags
60
+ * added breadcrumbs renderer incl. specs. A big thanks to Markus Schirp.
61
+ * added ability to register a renderer / specify your renderer as symbol in render_navigation
62
+ * renderer can be specified in render_navigation. Credits to Andi Bade from Galaxy Cats.
63
+
64
+ *2.5.4
65
+
66
+ * bugfix: SimpleNavigation.config_file? without params does not check for _navigation.rb file anymore. Credits to Markus Schirp.
67
+
68
+ *2.5.3
69
+
70
+ * removed deprecated railtie_name from simple_navigation/railtie. Credits to Markus Schirp.
71
+
72
+ *2.5.2
73
+
74
+ * added Rails3 generator for navigation_config.rb. Thanks to Josep Jaume Rey.
75
+
76
+ *2.5.1
77
+
78
+ * set template correctly for Rails3 (brings auto highlighting to life again). Credits to Josep Jaume Rey.
79
+
80
+ *2.5.0
81
+
82
+ * added new renderer Renderer::Links to simply render the navigation as links inside a div.
83
+ * also make item.name html_safe (in order you have html_code in the item's name). Thanks again, Johan Svensson.
84
+
85
+ *2.4.2
86
+
87
+ * Rails 3.0.0.beta2 compatibility
88
+ * Renderer::List --> make content of ul-tag html_safe for rails 3.0.0.beta2 (due to rails3 XSS protection). Credits to Johan Svensson and Disha Albaqui.
89
+ * updated copyright
90
+ * reduced visibility of 'sn_set_navigation' to protected
91
+
92
+ *2.4.1
93
+
94
+ * removing depencency to rails. It installs the newest rails, even if a matching version is present. why is that?
95
+
96
+ *2.4.0
97
+
98
+ * added Rails3 compatibility
99
+ * added Jeweler::Gemcutter Tasks to Rakefile
100
+
101
+ *2.2.3
102
+
103
+ * changed error handling in config-file. Do not ignore errors in config-file anymore.
104
+ * only load config-file if it is present. Needed when directly providing items in render_navigation.
105
+
106
+ *2.2.2
107
+
108
+ * added lib/simple-navigation.rb to allow 'require simple-navigation' or 'config.gem "simple-navigation"'
109
+
110
+ *2.2.1
111
+
112
+ * Corrected URL to API-Doc on rubyforge in README
113
+
114
+ *2.2.0
115
+
116
+ * Allow Ranges for :level option. Credits to Ying Tsen Hong.
117
+ * Changing the API of Helpers#render_navigation. Now supports Ranges for :level option and :expand_all to render all levels as expanded. Old Api is still supported, but deprecated.
118
+ * Deprecated render_all_levels in config-file.
119
+
120
+ *2.1.0
121
+
122
+ * included Ben Marini's commit which allows individual id-generators. Thanks Ben!
123
+ * added ability to specify navigation items through items_provider. This is useful for generating the navigation dynamically (e.g. from database)
124
+ * items can now even be passed directly into render_navigation method.
125
+
126
+ *2.0.1
127
+
128
+ * fixed handling of a non-existent explicit navigation item for a navigation context
129
+
130
+ *2.0.0
131
+
132
+ * added auto_highlight feature. Active navigation is determined by comparing urls, no need to explicitly set it in the controllers anymore. Thanks to Jack Dempsey and Florian Hanke for the support on this.
133
+ * added ability to create multi-level navigations (not just limited to primary and secondary navigation). Thanks again to Jack Dempsey for the motivation ;-)
134
+ * simplified the process to explicitly set the navigation in the controller (where needed) - only deepest level has to be specified
135
+ * made auto_highlight feature configurable both on global and item_container's level
136
+ * config file is now evaluated in template if ever possible (not in controller anymore)
137
+
138
+ *1.4.2
139
+
140
+ * explicitly loading all source files when requiring 'simple_navigation'.
141
+
142
+ *1.4.0
143
+
144
+ * added the capability to have several navigation-contexts
145
+ * doc-fix
146
+
147
+ *1.3.1
148
+
149
+ * now compliant with ruby 1.9.1 (thanks to Gernot Kogler for the feedback)
150
+
151
+ *1.3.0
152
+
153
+ * render_all_levels-option allows to render all subnavigation independent from the active primary navigation ('full open tree'). Userful for javascript menus. Thanks to Richard Hulse.
154
+ * ability to turn off automatic generation of dom_ids for the list items (autogenerate_item_ids). Credits again to Richard Hulse.
155
+ * ability to specify dom_class for primary and secondary lists. Thanks Richard!
156
+
157
+ *1.2.2
158
+
159
+ * renderers now have access to request_forgery_protection stuff (this allows delete-links as navigation-items)
160
+
161
+ *1.2.1
162
+
163
+ * changed way to include render_*-helper_methods into view (including them into Controller and declaring them as helper_methods instead of adding whole module as Helper). this seems to be more reliable under certain conditions. Credits to Gernot Kogler.
164
+
165
+ *1.2.0
166
+
167
+ * added capability to add conditions to navigation-items (primary.item key, name, url, :if => Proc.new {current_user.admin?})
168
+
169
+ *1.1.2
170
+
171
+ * Bugfix: config now gets evaluated on every render_navigation call. Credits to Joël Azémar.
172
+ * Config file gets reloaded on every render_navigation call in development mode. Only load config file on server start in production mode.
173
+
174
+
175
+ *1.1.1
176
+
177
+ * Change plugin into a GemPlugin
data/README ADDED
@@ -0,0 +1,22 @@
1
+
2
+ == Simple Navigation
3
+
4
+ Simple Navigation is a ruby library for creating navigations (with multiple levels) for your Rails 2, Rails 3, Sinatra or Padrino applications.
5
+
6
+ Source code:
7
+ git://github.com/andi/simple-navigation.git
8
+
9
+ Documentation:
10
+ http://wiki.github.com/andi/simple-navigation
11
+
12
+ RDoc:
13
+ http://andi.rubyforge.org/simple-navigation
14
+
15
+ Online Demo:
16
+ http://simple-navigation-demo.andischacke.com
17
+
18
+ Discussion Group for Feedback and Questions
19
+ http://groups.google.com/group/simple-navigation
20
+
21
+ Copyright (c) 2010 Andi Schacke, released under the MIT license
22
+
data/Rakefile ADDED
@@ -0,0 +1,48 @@
1
+ require 'rake'
2
+ require 'rspec/core/rake_task'
3
+ require 'rake/rdoctask'
4
+
5
+ desc 'Default: run specs.'
6
+ task :default => :spec
7
+
8
+ desc 'Run the specs'
9
+ RSpec::Core::RakeTask.new(:spec) do |t|
10
+ t.rspec_opts = ['--colour --format progress']
11
+ end
12
+
13
+ namespace :spec do
14
+ desc "Run all specs with RCov"
15
+ RSpec::Core::RakeTask.new(:rcov) do |t|
16
+ t.rspec_opts = ['--colour --format progress']
17
+ t.rcov = true
18
+ t.rcov_opts = ['--exclude', 'spec,/Users/']
19
+ end
20
+ end
21
+
22
+ desc 'Generate documentation for the simple_navigation_ext plugin.'
23
+ Rake::RDocTask.new(:rdoc) do |rdoc|
24
+ rdoc.rdoc_dir = 'rdoc'
25
+ rdoc.title = 'SimpleNavigationExt'
26
+ rdoc.options << '--line-numbers' << '--inline-source'
27
+ rdoc.rdoc_files.include('README')
28
+ rdoc.rdoc_files.include('lib/**/*.rb')
29
+ end
30
+
31
+ begin
32
+ require 'jeweler'
33
+ Jeweler::Tasks.new do |gemspec|
34
+ gemspec.name = "simple-navigation-ext"
35
+ gemspec.summary = "simple-navigation-ext is an extension of andi schacke's library and add the feature to explicitly exclude urls from highlighting."
36
+ gemspec.email = "info@screenconcept.ch"
37
+ gemspec.homepage = "https://github.com/screenconcept/simple-navigation"
38
+ gemspec.description = "simple-navigation-ext is an extension of andi schacke's library and add the feature to explicitly exclude urls from highlighting."
39
+ gemspec.add_development_dependency('rspec', '>= 2.0.1')
40
+ gemspec.add_dependency('activesupport', '>= 2.3.2')
41
+ gemspec.authors = ["Marco"]
42
+ gemspec.rdoc_options = ["--inline-source", "--charset=UTF-8"]
43
+ gemspec.files = FileList["[A-Z]*", "{lib,spec,rails,generators}/**/*"] - FileList["**/*.log"]
44
+ end
45
+ Jeweler::GemcutterTasks.new
46
+ rescue LoadError => e
47
+ puts "Jeweler not available (#{e}). Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
48
+ end
data/VERSION ADDED
@@ -0,0 +1 @@
1
+ 0.0.1
@@ -0,0 +1 @@
1
+ Creates a template config file for the simple-navigation plugin. You will find the generated file in config/navigation.rb.
@@ -0,0 +1,8 @@
1
+ class NavigationConfigGenerator < Rails::Generator::Base
2
+ def manifest
3
+ record do |m|
4
+ m.file "config/navigation.rb", "config/navigation.rb"
5
+ m.readme "../../../README"
6
+ end
7
+ end
8
+ end
@@ -0,0 +1,67 @@
1
+ # Configures your navigation
2
+ SimpleNavigation::Configuration.run do |navigation|
3
+ # Specify a custom renderer if needed.
4
+ # The default renderer is SimpleNavigation::Renderer::List which renders HTML lists.
5
+ # The renderer can also be specified as option in the render_navigation call.
6
+ # navigation.renderer = Your::Custom::Renderer
7
+
8
+ # Specify the class that will be applied to active navigation items. Defaults to 'selected'
9
+ # navigation.selected_class = 'your_selected_class'
10
+
11
+ # Item keys are normally added to list items as id.
12
+ # This setting turns that off
13
+ # navigation.autogenerate_item_ids = false
14
+
15
+ # You can override the default logic that is used to autogenerate the item ids.
16
+ # To do this, define a Proc which takes the key of the current item as argument.
17
+ # The example below would add a prefix to each key.
18
+ # navigation.id_generator = Proc.new {|key| "my-prefix-#{key}"}
19
+
20
+ # The auto highlight feature is turned on by default.
21
+ # This turns it off globally (for the whole plugin)
22
+ # navigation.auto_highlight = false
23
+
24
+ # Define the primary navigation
25
+ navigation.items do |primary|
26
+ # Add an item to the primary navigation. The following params apply:
27
+ # key - a symbol which uniquely defines your navigation item in the scope of the primary_navigation
28
+ # name - will be displayed in the rendered navigation. This can also be a call to your I18n-framework.
29
+ # url - the address that the generated item links to. You can also use url_helpers (named routes, restful routes helper, url_for etc.)
30
+ # options - can be used to specify attributes that will be included in the rendered navigation item (e.g. id, class etc.)
31
+ # some special options that can be set:
32
+ # :if - Specifies a proc to call to determine if the item should
33
+ # be rendered (e.g. <tt>:if => Proc.new { current_user.admin? }</tt>). The
34
+ # proc should evaluate to a true or false value and is evaluated in the context of the view.
35
+ # :unless - Specifies a proc to call to determine if the item should not
36
+ # be rendered (e.g. <tt>:unless => Proc.new { current_user.admin? }</tt>). The
37
+ # proc should evaluate to a true or false value and is evaluated in the context of the view.
38
+ # :method - Specifies the http-method for the generated link - default is :get.
39
+ # :highlights_on - if autohighlighting is turned off and/or you want to explicitly specify
40
+ # when the item should be highlighted, you can set a regexp which is matched
41
+ # against the current URI.
42
+ #
43
+ primary.item :key_1, 'name', url, options
44
+
45
+ # Add an item which has a sub navigation (same params, but with block)
46
+ primary.item :key_2, 'name', url, options do |sub_nav|
47
+ # Add an item to the sub navigation (same params again)
48
+ sub_nav.item :key_2_1, 'name', url, options
49
+ end
50
+
51
+ # You can also specify a condition-proc that needs to be fullfilled to display an item.
52
+ # Conditions are part of the options. They are evaluated in the context of the views,
53
+ # thus you can use all the methods and vars you have available in the views.
54
+ primary.item :key_3, 'Admin', url, :class => 'special', :if => Proc.new { current_user.admin? }
55
+ primary.item :key_4, 'Account', url, :unless => Proc.new { logged_in? }
56
+
57
+ # you can also specify a css id or class to attach to this particular level
58
+ # works for all levels of the menu
59
+ # primary.dom_id = 'menu-id'
60
+ # primary.dom_class = 'menu-class'
61
+
62
+ # You can turn off auto highlighting for a specific level
63
+ # primary.auto_highlight = false
64
+
65
+ end
66
+
67
+ end
@@ -0,0 +1,12 @@
1
+ class NavigationConfigGenerator < Rails::Generators::Base
2
+ def self.source_root
3
+ @source_root ||= File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','generators','navigation_config', 'templates'))
4
+ end
5
+
6
+ desc 'Creates a template config file for the simple-navigation plugin. You will find the generated file in config/navigation.rb.'
7
+ def navigation_config
8
+ copy_file('config/navigation.rb', 'config/navigation.rb')
9
+ say File.read(File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','README')))
10
+ end
11
+
12
+ end
@@ -0,0 +1 @@
1
+ require 'simple_navigation'
@@ -0,0 +1,37 @@
1
+ module SimpleNavigation
2
+ module Adapters
3
+
4
+ # This is the base class for all adapters. This class mainly exists for documenting reasons.
5
+ # It lists all the methods that an adapter should implement.
6
+ #
7
+ class Base
8
+ attr_reader :context, :request
9
+
10
+ # This method is usually called when the framework is initialized.
11
+ # It should call SimpleNavigation.set_env and install SimpleNavigation::Helpers where appropriate.
12
+ def self.register; end
13
+
14
+ # Returns the full path incl. query params
15
+ def request_uri; end
16
+
17
+ # Returns the path without query params
18
+ def request_path; end
19
+
20
+ # Returns the context in which the config files will be evaluated
21
+ def context_for_eval; end
22
+
23
+ # Returns true if the current request's url matches the specified url.
24
+ # Used to determine if an item should be autohighlighted.
25
+ def current_page?(url); end
26
+
27
+ # Returns a link with the specified name, url and options.
28
+ # Used for rendering.
29
+ def link_to(name, url, options={}); end
30
+
31
+ # Returns a tag of the specified type, content and options.
32
+ # Used for rendering.
33
+ def content_tag(type, content, options={}); end
34
+
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,20 @@
1
+ module SimpleNavigation
2
+ module Adapters
3
+ class Padrino < Sinatra
4
+
5
+ def self.register
6
+ SimpleNavigation.set_env(PADRINO_ROOT, PADRINO_ENV)
7
+ ::Padrino::Application.send(:helpers, SimpleNavigation::Helpers)
8
+ end
9
+
10
+ def link_to(name, url, options={})
11
+ context.link_to name, url, options
12
+ end
13
+
14
+ def content_tag(type, content, options={})
15
+ context.content_tag type, content, options
16
+ end
17
+
18
+ end
19
+ end
20
+ end
@@ -0,0 +1,94 @@
1
+ module SimpleNavigation
2
+ module Adapters
3
+ class Rails < Base
4
+
5
+ attr_reader :controller, :template
6
+
7
+ def self.register
8
+ SimpleNavigation.set_env(rails_root, rails_env)
9
+ ActionController::Base.send(:include, SimpleNavigation::Helpers)
10
+ ActionController::Base.send(:helper_method, :render_navigation)
11
+ ActionController::Base.send(:helper_method, :active_navigation_item_name)
12
+ end
13
+
14
+ def initialize(context)
15
+ @controller = extract_controller_from context
16
+ @template = template_from @controller
17
+ @request = @template.request if @template
18
+ end
19
+
20
+ def request_uri
21
+ return '' unless request
22
+ return request.fullpath if request.respond_to?(:fullpath)
23
+ request.request_uri
24
+ end
25
+
26
+ def request_path
27
+ return '' unless request
28
+ request.path
29
+ end
30
+
31
+ def context_for_eval
32
+ raise 'no context set for evaluation the config file' unless template || controller
33
+ template || controller
34
+ end
35
+
36
+ def current_page?(url)
37
+ template.current_page?(url) if template
38
+ end
39
+
40
+ def link_to(name, url, options={})
41
+ template.link_to(html_safe(name), url, options) if template
42
+ end
43
+
44
+ def content_tag(type, content, options={})
45
+ template.content_tag(type, html_safe(content), options) if template
46
+ end
47
+
48
+ protected
49
+
50
+ def self.rails_root
51
+ rails3? ? ::Rails.root : ::RAILS_ROOT
52
+ end
53
+
54
+ def self.rails_env
55
+ rails3? ? ::Rails.env : ::RAILS_ENV
56
+ end
57
+
58
+ def self.rails3?
59
+ ::Rails::VERSION::MAJOR == 3
60
+ end
61
+
62
+ def template_from(controller)
63
+ controller.respond_to?(:view_context) ? controller.view_context : controller.instance_variable_get(:@template)
64
+ end
65
+
66
+ # Marks the specified input as html_safe (for Rails3). Does nothing if html_safe is not defined on input.
67
+ #
68
+ def html_safe(input)
69
+ input.respond_to?(:html_safe) ? input.html_safe : input
70
+ end
71
+
72
+ # Extracts a controller from the context.
73
+ def extract_controller_from(context)
74
+ if context.respond_to? :controller
75
+ context.controller
76
+ else
77
+ context
78
+ end
79
+ end
80
+
81
+ end
82
+ end
83
+ end
84
+
85
+ # Initializer for Rails3
86
+ if defined?(Rails) && Rails::VERSION::MAJOR == 3
87
+ module SimpleNavigation
88
+ class Railtie < Rails::Railtie
89
+ initializer "simple_navigation.register" do |app|
90
+ SimpleNavigation.register
91
+ end
92
+ end
93
+ end
94
+ end
@@ -0,0 +1,68 @@
1
+ require 'cgi'
2
+
3
+ module SimpleNavigation
4
+ module Adapters
5
+ class Sinatra < Base
6
+
7
+ def self.register
8
+ SimpleNavigation.set_env(sinatra_root, sinatra_environment)
9
+ ::Sinatra::Application.send(:helpers, SimpleNavigation::Helpers)
10
+ end
11
+
12
+ def initialize(context)
13
+ @context = context
14
+ @request = context.request
15
+ end
16
+
17
+ def context_for_eval
18
+ raise 'no context set for evaluation the config file' unless context
19
+ context
20
+ end
21
+
22
+ def request_uri
23
+ request.fullpath
24
+ end
25
+
26
+ def request_path
27
+ request.path
28
+ end
29
+
30
+ def current_page?(url)
31
+ url_string = CGI.unescape(url)
32
+ if url_string.index("?")
33
+ uri = request_uri
34
+ else
35
+ uri = request_uri.split('?').first
36
+ end
37
+ if url_string =~ /^\w+:\/\//
38
+ url_string == "#{request.protocol}#{request.host_with_port}#{uri}"
39
+ else
40
+ url_string == uri
41
+ end
42
+ end
43
+
44
+ def link_to(name, url, options={})
45
+ "<a href='#{url}' #{to_attributes(options)}>#{name}</a>"
46
+ end
47
+
48
+ def content_tag(type, content, options={})
49
+ "<#{type} #{to_attributes(options)}>#{content}</#{type}>"
50
+ end
51
+
52
+ protected
53
+
54
+ def self.sinatra_root
55
+ ::Sinatra::Application.root
56
+ end
57
+
58
+ def self.sinatra_environment
59
+ ::Sinatra::Application.environment
60
+ end
61
+
62
+ def to_attributes(options)
63
+ options.map {|k, v| "#{k}='#{v}'"}.join(' ')
64
+ end
65
+
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,9 @@
1
+ require 'simple_navigation/adapters/base'
2
+
3
+ module SimpleNavigation
4
+ module Adapters
5
+ autoload :Rails, 'simple_navigation/adapters/rails'
6
+ autoload :Padrino, 'simple_navigation/adapters/padrino'
7
+ autoload :Sinatra, 'simple_navigation/adapters/sinatra'
8
+ end
9
+ end
@@ -0,0 +1,70 @@
1
+ require 'singleton'
2
+
3
+ module SimpleNavigation
4
+
5
+ # Responsible for evaluating and handling the config/navigation.rb file.
6
+ class Configuration
7
+ include Singleton
8
+
9
+ attr_accessor :renderer, :selected_class, :autogenerate_item_ids, :id_generator, :auto_highlight
10
+ attr_reader :primary_navigation
11
+
12
+ class << self
13
+
14
+ # Evals the config_file for the given navigation_context
15
+ def eval_config(navigation_context = :default)
16
+ SimpleNavigation.context_for_eval.instance_eval(SimpleNavigation.config_files[navigation_context])
17
+ end
18
+
19
+ # Starts processing the configuration
20
+ def run(&block)
21
+ block.call Configuration.instance
22
+ end
23
+
24
+ end #class << self
25
+
26
+ # Sets the config's default-settings
27
+ def initialize
28
+ @renderer = SimpleNavigation.default_renderer || SimpleNavigation::Renderer::List
29
+ @selected_class = 'selected'
30
+ @autogenerate_item_ids = true
31
+ @id_generator = Proc.new {|id| id.to_s }
32
+ @auto_highlight = true
33
+ end
34
+
35
+ # This is the main method for specifying the navigation items. It can be used in two ways:
36
+ #
37
+ # 1. Declaratively specify your items in the config/navigation.rb file using a block. It then yields an SimpleNavigation::ItemContainer for adding navigation items.
38
+ # 1. Directly provide your items to the method (e.g. when loading your items from the database).
39
+ #
40
+ # ==== Example for block style (configuration file)
41
+ # config.items do |primary|
42
+ # primary.item :my_item, 'My item', my_item_path
43
+ # ...
44
+ # end
45
+ #
46
+ # ==== To consider when directly providing items
47
+ # items_provider should be:
48
+ # * a methodname (as symbol) that returns your items. The method needs to be available in the view (i.e. a helper method)
49
+ # * an object that responds to :items
50
+ # * an enumerable containing your items
51
+ # The items you specify have to fullfill certain requirements. See SimpleNavigation::ItemAdapter for more details.
52
+ #
53
+ def items(items_provider=nil, &block)
54
+ raise 'please specify either items_provider or block, but not both' if (items_provider && block) || (items_provider.nil? && block.nil?)
55
+ @primary_navigation = ItemContainer.new
56
+ if block
57
+ block.call @primary_navigation
58
+ else
59
+ @primary_navigation.items = SimpleNavigation::ItemsProvider.new(items_provider).items
60
+ end
61
+ end
62
+
63
+ # Returns true if the config_file has already been evaluated.
64
+ def loaded?
65
+ !@primary_navigation.nil?
66
+ end
67
+
68
+ end
69
+
70
+ end