simple-navigation-ext 0.0.1
Sign up to get free protection for your applications and to get access to all the features.
- data/CHANGELOG +177 -0
- data/README +22 -0
- data/Rakefile +48 -0
- data/VERSION +1 -0
- data/generators/navigation_config/USAGE +1 -0
- data/generators/navigation_config/navigation_config_generator.rb +8 -0
- data/generators/navigation_config/templates/config/navigation.rb +67 -0
- data/lib/generators/navigation_config/navigation_config_generator.rb +12 -0
- data/lib/simple-navigation.rb +1 -0
- data/lib/simple_navigation/adapters/base.rb +37 -0
- data/lib/simple_navigation/adapters/padrino.rb +20 -0
- data/lib/simple_navigation/adapters/rails.rb +94 -0
- data/lib/simple_navigation/adapters/sinatra.rb +68 -0
- data/lib/simple_navigation/adapters.rb +9 -0
- data/lib/simple_navigation/core/configuration.rb +70 -0
- data/lib/simple_navigation/core/item.rb +117 -0
- data/lib/simple_navigation/core/item_adapter.rb +63 -0
- data/lib/simple_navigation/core/item_container.rb +146 -0
- data/lib/simple_navigation/core/items_provider.rb +35 -0
- data/lib/simple_navigation/core.rb +5 -0
- data/lib/simple_navigation/rails_controller_methods.rb +144 -0
- data/lib/simple_navigation/rendering/helpers.rb +82 -0
- data/lib/simple_navigation/rendering/renderer/base.rb +71 -0
- data/lib/simple_navigation/rendering/renderer/breadcrumbs.rb +37 -0
- data/lib/simple_navigation/rendering/renderer/links.rb +25 -0
- data/lib/simple_navigation/rendering/renderer/list.rb +47 -0
- data/lib/simple_navigation/rendering.rb +10 -0
- data/lib/simple_navigation.rb +162 -0
- data/rails/init.rb +1 -0
- data/spec/lib/simple_navigation/adapters/padrino_spec.rb +29 -0
- data/spec/lib/simple_navigation/adapters/rails_spec.rb +284 -0
- data/spec/lib/simple_navigation/adapters/sinatra_spec.rb +60 -0
- data/spec/lib/simple_navigation/core/configuration_spec.rb +122 -0
- data/spec/lib/simple_navigation/core/item_adapter_spec.rb +209 -0
- data/spec/lib/simple_navigation/core/item_container_spec.rb +396 -0
- data/spec/lib/simple_navigation/core/item_spec.rb +513 -0
- data/spec/lib/simple_navigation/core/items_provider_spec.rb +60 -0
- data/spec/lib/simple_navigation/rails_controller_methods_spec.rb +249 -0
- data/spec/lib/simple_navigation/rendering/helpers_spec.rb +183 -0
- data/spec/lib/simple_navigation/rendering/renderer/base_spec.rb +199 -0
- data/spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb +58 -0
- data/spec/lib/simple_navigation/rendering/renderer/links_spec.rb +58 -0
- data/spec/lib/simple_navigation/rendering/renderer/list_spec.rb +189 -0
- data/spec/lib/simple_navigation_spec.rb +307 -0
- data/spec/spec_helper.rb +96 -0
- metadata +158 -0
@@ -0,0 +1,37 @@
|
|
1
|
+
module SimpleNavigation
|
2
|
+
module Renderer
|
3
|
+
|
4
|
+
# Renders an ItemContainer as a <div> element and its containing items as <a> elements.
|
5
|
+
# It only renders 'selected' elements.
|
6
|
+
#
|
7
|
+
# By default, the renderer sets the item's key as dom_id for the rendered <a> element unless the config option <tt>autogenerate_item_ids</tt> is set to false.
|
8
|
+
# The id can also be explicitely specified by setting the id in the html-options of the 'item' method in the config/navigation.rb file.
|
9
|
+
# The ItemContainer's dom_class and dom_id are applied to the surrounding <div> element.
|
10
|
+
#
|
11
|
+
class Breadcrumbs < SimpleNavigation::Renderer::Base
|
12
|
+
|
13
|
+
def render(item_container)
|
14
|
+
content_tag(:div, a_tags(item_container).join(join_with), {:id => item_container.dom_id, :class => item_container.dom_class})
|
15
|
+
end
|
16
|
+
|
17
|
+
protected
|
18
|
+
|
19
|
+
def a_tags(item_container)
|
20
|
+
item_container.items.inject([]) do |list, item|
|
21
|
+
if item.selected?
|
22
|
+
list << link_to(item.name, item.url, {:method => item.method}.merge(item.html_options.except(:class,:id))) if item.selected?
|
23
|
+
if include_sub_navigation?(item)
|
24
|
+
list.concat a_tags(item.sub_navigation)
|
25
|
+
end
|
26
|
+
end
|
27
|
+
list
|
28
|
+
end
|
29
|
+
end
|
30
|
+
|
31
|
+
def join_with
|
32
|
+
@join_with ||= options[:join_with] || " "
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
end
|
@@ -0,0 +1,25 @@
|
|
1
|
+
module SimpleNavigation
|
2
|
+
module Renderer
|
3
|
+
|
4
|
+
# Renders an ItemContainer as a <div> element and its containing items as <a> elements.
|
5
|
+
# It adds the 'selected' class to the <a> element that is currently active.
|
6
|
+
#
|
7
|
+
# The Links renderer cannot be used to render nested navigations. If you would like it to use with nested navigations, you have to render each level separately.
|
8
|
+
#
|
9
|
+
# By default, the renderer sets the item's key as dom_id for the rendered <a> element unless the config option <tt>autogenerate_item_ids</tt> is set to false.
|
10
|
+
# The id can also be explicitely specified by setting the id in the html-options of the 'item' method in the config/navigation.rb file.
|
11
|
+
# The ItemContainer's dom_class and dom_id are applied to the surrounding <div> element.
|
12
|
+
#
|
13
|
+
class Links < SimpleNavigation::Renderer::Base
|
14
|
+
|
15
|
+
def render(item_container)
|
16
|
+
div_content = item_container.items.inject([]) do |list, item|
|
17
|
+
list << link_to(item.name, item.url, {:method => item.method}.merge(item.html_options))
|
18
|
+
end.join
|
19
|
+
content_tag(:div, div_content, {:id => item_container.dom_id, :class => item_container.dom_class})
|
20
|
+
end
|
21
|
+
|
22
|
+
end
|
23
|
+
|
24
|
+
end
|
25
|
+
end
|
@@ -0,0 +1,47 @@
|
|
1
|
+
module SimpleNavigation
|
2
|
+
module Renderer
|
3
|
+
|
4
|
+
# Renders an ItemContainer as a <ul> element and its containing items as <li> elements.
|
5
|
+
# It adds the 'selected' class to li element AND the link inside the li element that is currently active.
|
6
|
+
#
|
7
|
+
# If the sub navigation should be included (based on the level and expand_all options), it renders another <ul> containing the sub navigation inside the active <li> element.
|
8
|
+
#
|
9
|
+
# By default, the renderer sets the item's key as dom_id for the rendered <li> element unless the config option <tt>autogenerate_item_ids</tt> is set to false.
|
10
|
+
# The id can also be explicitely specified by setting the id in the html-options of the 'item' method in the config/navigation.rb file.
|
11
|
+
class List < SimpleNavigation::Renderer::Base
|
12
|
+
|
13
|
+
def render(item_container)
|
14
|
+
list_content = item_container.items.inject([]) do |list, item|
|
15
|
+
li_options = item.html_options.reject {|k, v| k == :link}
|
16
|
+
li_content = link_to(item.name, item.url, link_options_for(item))
|
17
|
+
if include_sub_navigation?(item)
|
18
|
+
li_content << render_sub_navigation_for(item)
|
19
|
+
end
|
20
|
+
list << content_tag(:li, li_content, li_options)
|
21
|
+
end.join
|
22
|
+
if skip_if_empty? && item_container.empty?
|
23
|
+
''
|
24
|
+
else
|
25
|
+
content_tag(:ul, list_content, {:id => item_container.dom_id, :class => item_container.dom_class})
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
protected
|
30
|
+
|
31
|
+
# Extracts the options relevant for the generated link
|
32
|
+
#
|
33
|
+
def link_options_for(item)
|
34
|
+
special_options = {:method => item.method, :class => item.selected_class}.reject {|k, v| v.nil? }
|
35
|
+
link_options = item.html_options[:link]
|
36
|
+
return special_options unless link_options
|
37
|
+
opts = special_options.merge(link_options)
|
38
|
+
opts[:class] = [link_options[:class], item.selected_class].flatten.compact.join(' ')
|
39
|
+
opts.delete(:class) if opts[:class].nil? || opts[:class] == ''
|
40
|
+
opts
|
41
|
+
end
|
42
|
+
|
43
|
+
|
44
|
+
end
|
45
|
+
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,10 @@
|
|
1
|
+
require 'simple_navigation/rendering/helpers'
|
2
|
+
require 'simple_navigation/rendering/renderer/base'
|
3
|
+
|
4
|
+
module SimpleNavigation
|
5
|
+
module Renderer
|
6
|
+
autoload :List, 'simple_navigation/rendering/renderer/list'
|
7
|
+
autoload :Links, 'simple_navigation/rendering/renderer/links'
|
8
|
+
autoload :Breadcrumbs, 'simple_navigation/rendering/renderer/breadcrumbs'
|
9
|
+
end
|
10
|
+
end
|
@@ -0,0 +1,162 @@
|
|
1
|
+
# cherry picking active_support stuff
|
2
|
+
require 'active_support/core_ext/array'
|
3
|
+
require 'active_support/core_ext/hash'
|
4
|
+
require 'active_support/core_ext/module/attribute_accessors'
|
5
|
+
|
6
|
+
require 'simple_navigation/core'
|
7
|
+
require 'simple_navigation/rendering'
|
8
|
+
require 'simple_navigation/adapters'
|
9
|
+
|
10
|
+
require 'forwardable'
|
11
|
+
|
12
|
+
# A plugin for generating a simple navigation. See README for resources on usage instructions.
|
13
|
+
module SimpleNavigation
|
14
|
+
|
15
|
+
mattr_accessor :adapter_class, :adapter, :config_files, :config_file_paths, :default_renderer, :registered_renderers, :root, :environment
|
16
|
+
|
17
|
+
# Cache for loaded config files
|
18
|
+
self.config_files = {}
|
19
|
+
|
20
|
+
# Allows for multiple config_file_paths. Needed if a plugin itself uses simple-navigation and therefore has its own config file
|
21
|
+
self.config_file_paths = []
|
22
|
+
|
23
|
+
# Maps renderer keys to classes. The keys serve as shortcut in the render_navigation calls (:renderer => :list)
|
24
|
+
self.registered_renderers = {
|
25
|
+
:list => SimpleNavigation::Renderer::List,
|
26
|
+
:links => SimpleNavigation::Renderer::Links,
|
27
|
+
:breadcrumbs => SimpleNavigation::Renderer::Breadcrumbs
|
28
|
+
}
|
29
|
+
|
30
|
+
class << self
|
31
|
+
extend Forwardable
|
32
|
+
|
33
|
+
def_delegators :adapter, :request, :request_uri, :request_path, :context_for_eval, :current_page?
|
34
|
+
def_delegators :adapter_class, :register
|
35
|
+
|
36
|
+
# Sets the root path and current environment as specified. Also sets the default config_file_path.
|
37
|
+
def set_env(root, environment)
|
38
|
+
self.root = root
|
39
|
+
self.environment = environment
|
40
|
+
self.config_file_paths << SimpleNavigation.default_config_file_path
|
41
|
+
end
|
42
|
+
|
43
|
+
# Returns the current framework in which the plugin is running.
|
44
|
+
def framework
|
45
|
+
return :rails if defined?(Rails)
|
46
|
+
return :padrino if defined?(Padrino)
|
47
|
+
return :sinatra if defined?(Sinatra)
|
48
|
+
raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps'
|
49
|
+
end
|
50
|
+
|
51
|
+
# Loads the adapter for the current framework
|
52
|
+
def load_adapter
|
53
|
+
self.adapter_class = case framework
|
54
|
+
when :rails
|
55
|
+
SimpleNavigation::Adapters::Rails
|
56
|
+
when :sinatra
|
57
|
+
SimpleNavigation::Adapters::Sinatra
|
58
|
+
when :padrino
|
59
|
+
SimpleNavigation::Adapters::Padrino
|
60
|
+
end
|
61
|
+
end
|
62
|
+
|
63
|
+
# Creates a new adapter instance based on the context in which render_navigation has been called.
|
64
|
+
def init_adapter_from(context)
|
65
|
+
self.adapter = self.adapter_class.new(context)
|
66
|
+
end
|
67
|
+
|
68
|
+
def default_config_file_path
|
69
|
+
File.join(SimpleNavigation.root, 'config')
|
70
|
+
end
|
71
|
+
|
72
|
+
# Returns true if the config_file for specified context does exist.
|
73
|
+
def config_file?(navigation_context = :default)
|
74
|
+
!!config_file(navigation_context)
|
75
|
+
end
|
76
|
+
|
77
|
+
# Returns the path to the config file for the given navigation context or nil if no matching config file can be found.
|
78
|
+
# If multiple config_paths are set, it returns the first matching path.
|
79
|
+
def config_file(navigation_context = :default)
|
80
|
+
config_file_paths.collect { |path| File.join(path, config_file_name(navigation_context)) }.detect {|full_path| File.exists?(full_path)}
|
81
|
+
end
|
82
|
+
|
83
|
+
# Returns the name of the config file for the given navigation_context
|
84
|
+
def config_file_name(navigation_context = :default)
|
85
|
+
prefix = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
|
86
|
+
"#{prefix}navigation.rb"
|
87
|
+
end
|
88
|
+
|
89
|
+
# Resets the list of config_file_paths to the specified path
|
90
|
+
def config_file_path=(path)
|
91
|
+
self.config_file_paths = [path]
|
92
|
+
end
|
93
|
+
|
94
|
+
# Reads the config_file for the specified navigation_context and stores it for later evaluation.
|
95
|
+
def load_config(navigation_context = :default)
|
96
|
+
raise "Config file '#{config_file_name(navigation_context)}' not found in path(s) #{config_file_paths.join(', ')}!" unless config_file?(navigation_context)
|
97
|
+
if self.environment == 'production'
|
98
|
+
self.config_files[navigation_context] ||= IO.read(config_file(navigation_context))
|
99
|
+
else
|
100
|
+
self.config_files[navigation_context] = IO.read(config_file(navigation_context))
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
# Returns the singleton instance of the SimpleNavigation::Configuration
|
105
|
+
def config
|
106
|
+
SimpleNavigation::Configuration.instance
|
107
|
+
end
|
108
|
+
|
109
|
+
# Returns the ItemContainer that contains the items for the primary navigation
|
110
|
+
def primary_navigation
|
111
|
+
config.primary_navigation
|
112
|
+
end
|
113
|
+
|
114
|
+
# Returns the active item container for the specified level. Valid levels are
|
115
|
+
# * :all - in this case the primary_navigation is returned.
|
116
|
+
# * :leaves - the 'deepest' active item_container will be returned
|
117
|
+
# * a specific level - the active item_container for the specified level will be returned
|
118
|
+
# * a range of levels - the active item_container for the range's minimum will be returned
|
119
|
+
#
|
120
|
+
# Returns nil if there is no active item_container for the specified level.
|
121
|
+
def active_item_container_for(level)
|
122
|
+
case level
|
123
|
+
when :all
|
124
|
+
self.primary_navigation
|
125
|
+
when :leaves
|
126
|
+
self.primary_navigation.active_leaf_container
|
127
|
+
when Integer
|
128
|
+
self.primary_navigation.active_item_container_for(level)
|
129
|
+
when Range
|
130
|
+
self.primary_navigation.active_item_container_for(level.min)
|
131
|
+
else
|
132
|
+
raise ArgumentError, "Invalid navigation level: #{level}"
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
# Registers a renderer.
|
137
|
+
#
|
138
|
+
# === Example
|
139
|
+
# To register your own renderer:
|
140
|
+
#
|
141
|
+
# SimpleNavigation.register_renderer :my_renderer => My::RendererClass
|
142
|
+
#
|
143
|
+
# Then in the view you can call:
|
144
|
+
#
|
145
|
+
# render_navigation(:renderer => :my_renderer)
|
146
|
+
def register_renderer(renderer_hash)
|
147
|
+
self.registered_renderers.merge!(renderer_hash)
|
148
|
+
end
|
149
|
+
|
150
|
+
private
|
151
|
+
|
152
|
+
def apply_defaults(options)
|
153
|
+
options[:level] = options.delete(:levels) if options[:levels]
|
154
|
+
{:context => :default, :level => :all}.merge(options)
|
155
|
+
end
|
156
|
+
|
157
|
+
|
158
|
+
end
|
159
|
+
|
160
|
+
end
|
161
|
+
|
162
|
+
SimpleNavigation.load_adapter
|
data/rails/init.rb
ADDED
@@ -0,0 +1 @@
|
|
1
|
+
SimpleNavigation.register
|
@@ -0,0 +1,29 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleNavigation::Adapters::Padrino do
|
4
|
+
|
5
|
+
def create_adapter
|
6
|
+
SimpleNavigation::Adapters::Padrino.new(@context)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@request = stub(:request)
|
11
|
+
@context = stub(:context, :request => @request)
|
12
|
+
@adapter = create_adapter
|
13
|
+
end
|
14
|
+
|
15
|
+
describe 'link_to' do
|
16
|
+
it "should delegate to context" do
|
17
|
+
@context.should_receive(:link_to).with('name', 'url', :my_option => true)
|
18
|
+
@adapter.link_to('name', 'url', :my_option => true)
|
19
|
+
end
|
20
|
+
end
|
21
|
+
|
22
|
+
describe 'content_tag' do
|
23
|
+
it "should delegate to context" do
|
24
|
+
@context.should_receive(:content_tag).with('type', 'content', :my_option => true)
|
25
|
+
@adapter.content_tag('type', 'content', :my_option => true)
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
end
|
@@ -0,0 +1,284 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleNavigation::Adapters::Rails do
|
4
|
+
|
5
|
+
def create_adapter
|
6
|
+
SimpleNavigation::Adapters::Rails.new(@context)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@context = stub(:context)
|
11
|
+
@controller = stub(:controller)
|
12
|
+
@context.stub!(:controller => @controller)
|
13
|
+
@request = stub(:request)
|
14
|
+
@template = stub(:template, :request => @request)
|
15
|
+
@adapter = create_adapter
|
16
|
+
end
|
17
|
+
|
18
|
+
describe 'self.register' do
|
19
|
+
before(:each) do
|
20
|
+
ActionController::Base.stub!(:include)
|
21
|
+
end
|
22
|
+
it "should call set_env" do
|
23
|
+
SimpleNavigation.should_receive(:set_env).with('./', 'test')
|
24
|
+
SimpleNavigation.register
|
25
|
+
end
|
26
|
+
it "should extend the ActionController::Base with the Helpers" do
|
27
|
+
ActionController::Base.should_receive(:include).with(SimpleNavigation::Helpers)
|
28
|
+
SimpleNavigation.register
|
29
|
+
end
|
30
|
+
it "should install the helper methods in the controller" do
|
31
|
+
ActionController::Base.should_receive(:helper_method).with(:render_navigation)
|
32
|
+
ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_name)
|
33
|
+
SimpleNavigation.register
|
34
|
+
end
|
35
|
+
|
36
|
+
end
|
37
|
+
|
38
|
+
describe 'initialize' do
|
39
|
+
context 'regarding setting the request' do
|
40
|
+
context 'template is present' do
|
41
|
+
before(:each) do
|
42
|
+
@controller.stub!(:instance_variable_get => @template)
|
43
|
+
@adapter = create_adapter
|
44
|
+
end
|
45
|
+
it {@adapter.request.should == @request}
|
46
|
+
end
|
47
|
+
context 'template is not present' do
|
48
|
+
before(:each) do
|
49
|
+
@controller.stub!(:instance_variable_get => nil)
|
50
|
+
end
|
51
|
+
it {@adapter.request.should be_nil}
|
52
|
+
end
|
53
|
+
end
|
54
|
+
context 'regarding setting the controller' do
|
55
|
+
it "should set the controller" do
|
56
|
+
@adapter.controller.should == @controller
|
57
|
+
end
|
58
|
+
end
|
59
|
+
context 'regarding setting the template' do
|
60
|
+
context 'template is stored in controller as instance_var (Rails2)' do
|
61
|
+
context 'template is set' do
|
62
|
+
before(:each) do
|
63
|
+
@controller.stub!(:instance_variable_get => @template)
|
64
|
+
@adapter = create_adapter
|
65
|
+
end
|
66
|
+
it {@adapter.template.should == @template}
|
67
|
+
end
|
68
|
+
context 'template is not set' do
|
69
|
+
before(:each) do
|
70
|
+
@controller.stub!(:instance_variable_get => nil)
|
71
|
+
@adapter = create_adapter
|
72
|
+
end
|
73
|
+
it {@adapter.template.should be_nil}
|
74
|
+
end
|
75
|
+
end
|
76
|
+
context 'template is stored in controller as view_context (Rails3)' do
|
77
|
+
context 'template is set' do
|
78
|
+
before(:each) do
|
79
|
+
@controller.stub!(:view_context => @template)
|
80
|
+
@adapter = create_adapter
|
81
|
+
end
|
82
|
+
it {@adapter.template.should == @template}
|
83
|
+
end
|
84
|
+
context 'template is not set' do
|
85
|
+
before(:each) do
|
86
|
+
@controller.stub!(:view_context => nil)
|
87
|
+
@adapter = create_adapter
|
88
|
+
end
|
89
|
+
it {@adapter.template.should be_nil}
|
90
|
+
end
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
|
95
|
+
describe 'request_uri' do
|
96
|
+
context 'request is set' do
|
97
|
+
context 'fullpath is defined on request' do
|
98
|
+
before(:each) do
|
99
|
+
@request = stub(:request, :fullpath => '/fullpath')
|
100
|
+
@adapter.stub!(:request => @request)
|
101
|
+
end
|
102
|
+
it {@adapter.request_uri.should == '/fullpath'}
|
103
|
+
end
|
104
|
+
context 'fullpath is not defined on request' do
|
105
|
+
before(:each) do
|
106
|
+
@request = stub(:request, :request_uri => '/request_uri')
|
107
|
+
@adapter.stub!(:request => @request)
|
108
|
+
end
|
109
|
+
it {@adapter.request_uri.should == '/request_uri'}
|
110
|
+
end
|
111
|
+
end
|
112
|
+
context 'request is not set' do
|
113
|
+
before(:each) do
|
114
|
+
@adapter.stub!(:request => nil)
|
115
|
+
end
|
116
|
+
it {@adapter.request_uri.should == ''}
|
117
|
+
end
|
118
|
+
end
|
119
|
+
|
120
|
+
describe 'request_path' do
|
121
|
+
context 'request is set' do
|
122
|
+
before(:each) do
|
123
|
+
@request = stub(:request, :path => '/request_path')
|
124
|
+
@adapter.stub!(:request => @request)
|
125
|
+
end
|
126
|
+
it {@adapter.request_path.should == '/request_path'}
|
127
|
+
end
|
128
|
+
context 'request is not set' do
|
129
|
+
before(:each) do
|
130
|
+
@adapter.stub!(:request => nil)
|
131
|
+
end
|
132
|
+
it {@adapter.request_path.should == ''}
|
133
|
+
end
|
134
|
+
end
|
135
|
+
|
136
|
+
describe 'context_for_eval' do
|
137
|
+
context 'controller is present' do
|
138
|
+
before(:each) do
|
139
|
+
@controller = stub(:controller)
|
140
|
+
@adapter.instance_variable_set(:@controller, @controller)
|
141
|
+
end
|
142
|
+
context 'template is present' do
|
143
|
+
before(:each) do
|
144
|
+
@template = stub(:template)
|
145
|
+
@adapter.instance_variable_set(:@template, @template)
|
146
|
+
end
|
147
|
+
it {@adapter.context_for_eval.should == @template}
|
148
|
+
end
|
149
|
+
context 'template is not present' do
|
150
|
+
before(:each) do
|
151
|
+
@adapter.instance_variable_set(:@template, nil)
|
152
|
+
end
|
153
|
+
it {@adapter.context_for_eval.should == @controller}
|
154
|
+
end
|
155
|
+
end
|
156
|
+
context 'controller is not present' do
|
157
|
+
before(:each) do
|
158
|
+
@adapter.instance_variable_set(:@controller, nil)
|
159
|
+
end
|
160
|
+
context 'template is present' do
|
161
|
+
before(:each) do
|
162
|
+
@template = stub(:template)
|
163
|
+
@adapter.instance_variable_set(:@template, @template)
|
164
|
+
end
|
165
|
+
it {@adapter.context_for_eval.should == @template}
|
166
|
+
end
|
167
|
+
context 'template is not present' do
|
168
|
+
before(:each) do
|
169
|
+
@adapter.instance_variable_set(:@template, nil)
|
170
|
+
end
|
171
|
+
it {lambda {@adapter.context_for_eval}.should raise_error}
|
172
|
+
end
|
173
|
+
end
|
174
|
+
end
|
175
|
+
|
176
|
+
describe 'current_page?' do
|
177
|
+
context 'template is set' do
|
178
|
+
before(:each) do
|
179
|
+
@adapter.stub!(:template => @template)
|
180
|
+
end
|
181
|
+
it "should delegate the call to the template" do
|
182
|
+
@template.should_receive(:current_page?).with(:page)
|
183
|
+
@adapter.current_page?(:page)
|
184
|
+
end
|
185
|
+
end
|
186
|
+
context 'template is not set' do
|
187
|
+
before(:each) do
|
188
|
+
@adapter.stub!(:template => nil)
|
189
|
+
end
|
190
|
+
it {@adapter.should_not be_current_page(:page)}
|
191
|
+
end
|
192
|
+
end
|
193
|
+
|
194
|
+
describe 'link_to' do
|
195
|
+
context 'template is set' do
|
196
|
+
before(:each) do
|
197
|
+
@adapter.stub!(:template => @template)
|
198
|
+
@adapter.stub!(:html_safe => 'safe_text')
|
199
|
+
@options = stub(:options)
|
200
|
+
end
|
201
|
+
it "should delegate the call to the template (with html_safe text)" do
|
202
|
+
@template.should_receive(:link_to).with('safe_text', 'url', @options)
|
203
|
+
@adapter.link_to('text', 'url', @options)
|
204
|
+
end
|
205
|
+
end
|
206
|
+
context 'template is not set' do
|
207
|
+
before(:each) do
|
208
|
+
@adapter.stub!(:template => nil)
|
209
|
+
end
|
210
|
+
it {@adapter.link_to('text', 'url', @options).should be_nil}
|
211
|
+
end
|
212
|
+
end
|
213
|
+
|
214
|
+
describe 'content_tag' do
|
215
|
+
context 'template is set' do
|
216
|
+
before(:each) do
|
217
|
+
@adapter.stub!(:template => @template)
|
218
|
+
@adapter.stub!(:html_safe => 'safe_text')
|
219
|
+
@options = stub(:options)
|
220
|
+
end
|
221
|
+
it "should delegate the call to the template (with html_safe text)" do
|
222
|
+
@template.should_receive(:content_tag).with(:div, 'safe_text', @options)
|
223
|
+
@adapter.content_tag(:div, 'text', @options)
|
224
|
+
end
|
225
|
+
end
|
226
|
+
context 'template is not set' do
|
227
|
+
before(:each) do
|
228
|
+
@adapter.stub!(:template => nil)
|
229
|
+
end
|
230
|
+
it {@adapter.content_tag(:div, 'text', @options).should be_nil}
|
231
|
+
end
|
232
|
+
end
|
233
|
+
|
234
|
+
describe 'self.extract_controller_from' do
|
235
|
+
context 'object responds to controller' do
|
236
|
+
before(:each) do
|
237
|
+
@context.stub!(:controller => @controller)
|
238
|
+
end
|
239
|
+
it "should return the controller" do
|
240
|
+
@adapter.send(:extract_controller_from, @context).should == @controller
|
241
|
+
end
|
242
|
+
end
|
243
|
+
context 'object does not respond to controller' do
|
244
|
+
before(:each) do
|
245
|
+
@context = stub(:context)
|
246
|
+
end
|
247
|
+
it "should return the context" do
|
248
|
+
@adapter.send(:extract_controller_from, @context).should == @context
|
249
|
+
end
|
250
|
+
end
|
251
|
+
end
|
252
|
+
|
253
|
+
describe 'html_safe' do
|
254
|
+
before(:each) do
|
255
|
+
@input = stub :input
|
256
|
+
end
|
257
|
+
context 'input does respond to html_safe' do
|
258
|
+
before(:each) do
|
259
|
+
@safe = stub :safe
|
260
|
+
@input.stub!(:html_safe => @safe)
|
261
|
+
end
|
262
|
+
it {@adapter.send(:html_safe, @input).should == @safe}
|
263
|
+
end
|
264
|
+
context 'input does not respond to html_safe' do
|
265
|
+
it {@adapter.send(:html_safe, @input).should == @input}
|
266
|
+
end
|
267
|
+
end
|
268
|
+
|
269
|
+
describe 'template_from' do
|
270
|
+
context 'Rails3' do
|
271
|
+
before(:each) do
|
272
|
+
@controller.stub!(:view_context => 'view')
|
273
|
+
end
|
274
|
+
it {@adapter.send(:template_from, @controller).should == 'view'}
|
275
|
+
end
|
276
|
+
context 'Rails2' do
|
277
|
+
before(:each) do
|
278
|
+
@controller.instance_variable_set(:@template, 'view')
|
279
|
+
end
|
280
|
+
it {@adapter.send(:template_from, @controller).should == 'view'}
|
281
|
+
end
|
282
|
+
end
|
283
|
+
|
284
|
+
end
|
@@ -0,0 +1,60 @@
|
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe SimpleNavigation::Adapters::Sinatra do
|
4
|
+
|
5
|
+
def create_adapter
|
6
|
+
SimpleNavigation::Adapters::Sinatra.new(@context)
|
7
|
+
end
|
8
|
+
|
9
|
+
before(:each) do
|
10
|
+
@context = stub(:context)
|
11
|
+
@request = stub(:request, :fullpath => '/full?param=true', :path => '/full')
|
12
|
+
@context.stub!(:request => @request)
|
13
|
+
@adapter = create_adapter
|
14
|
+
end
|
15
|
+
|
16
|
+
describe 'context_for_eval' do
|
17
|
+
it "should raise error if no context" do
|
18
|
+
@adapter.stub!(:context => nil)
|
19
|
+
lambda {@adapter.context_for_eval}.should raise_error
|
20
|
+
end
|
21
|
+
it "should return the context" do
|
22
|
+
@adapter.context_for_eval.should == @context
|
23
|
+
end
|
24
|
+
end
|
25
|
+
|
26
|
+
describe 'request_uri' do
|
27
|
+
it {@adapter.request_uri.should == '/full?param=true'}
|
28
|
+
end
|
29
|
+
|
30
|
+
describe 'request_path' do
|
31
|
+
it {@adapter.request_path.should == '/full'}
|
32
|
+
end
|
33
|
+
|
34
|
+
describe 'current_page?' do
|
35
|
+
before(:each) do
|
36
|
+
@request.stub!(:protocol => 'http://', :host_with_port => 'my_host:5000')
|
37
|
+
end
|
38
|
+
it {@adapter.current_page?('/full?param=true').should be_true}
|
39
|
+
it {@adapter.current_page?('/full?param3=true').should be_false}
|
40
|
+
it {@adapter.current_page?('/full').should be_true}
|
41
|
+
it {@adapter.current_page?('http://my_host:5000/full?param=true').should be_true}
|
42
|
+
it {@adapter.current_page?('http://my_host:5000/full?param3=true').should be_false}
|
43
|
+
it {@adapter.current_page?('http://my_host:5000/full').should be_true}
|
44
|
+
it {@adapter.current_page?('http://my_host:6000/full').should be_false}
|
45
|
+
it {@adapter.current_page?('http://my_other_host:5000/full').should be_false}
|
46
|
+
end
|
47
|
+
|
48
|
+
describe 'link_to' do
|
49
|
+
it "should return a link" do
|
50
|
+
@adapter.link_to('link', 'url', :class => 'clazz', :id => 'id').should == "<a href='url' class='clazz' id='id'>link</a>"
|
51
|
+
end
|
52
|
+
end
|
53
|
+
|
54
|
+
describe 'content_tag' do
|
55
|
+
it "should return a tag" do
|
56
|
+
@adapter.content_tag(:div, 'content', :class => 'clazz', :id => 'id').should == "<div class='clazz' id='id'>content</div>"
|
57
|
+
end
|
58
|
+
end
|
59
|
+
|
60
|
+
end
|