element.
#
class Links < SimpleNavigation::Renderer::Base
def render(item_container)
div_content = item_container.items.inject([]) do |list, item|
list << tag_for(item)
end.join(join_with)
content_tag(:div, div_content, {:id => item_container.dom_id, :class => item_container.dom_class})
end
protected
def join_with
@join_with ||= options[:join_with] || ""
end
def options_for(item)
{:method => item.method}.merge(item.html_options)
end
end
end
end
simple-navigation-3.11.0/lib/simple_navigation/rendering/renderer/base.rb 0000644 0001756 0001756 00000006033 12202013367 025622 0 ustar synrg synrg require 'forwardable'
module SimpleNavigation
module Renderer
# This is the base class for all renderers.
#
# A renderer is responsible for rendering an ItemContainer and its containing items to HTML.
class Base
extend Forwardable
attr_reader :options, :adapter
def_delegators :adapter, :link_to, :content_tag
def initialize(options) #:nodoc:
@options = options
@adapter = SimpleNavigation.adapter
end
def expand_all?
!!options[:expand_all]
end
def level
options[:level] || :all
end
def skip_if_empty?
!!options[:skip_if_empty]
end
def include_sub_navigation?(item)
consider_sub_navigation?(item) && expand_sub_navigation?(item)
end
def render_sub_navigation_for(item)
item.sub_navigation.render(self.options)
end
# Renders the specified ItemContainer to HTML.
#
# When implementing a renderer, please consider to call include_sub_navigation? to determin
# whether an item's sub_navigation should be rendered or not.
#
def render(item_container)
raise 'subclass responsibility'
end
protected
def consider_sub_navigation?(item)
return false if item.sub_navigation.nil?
case level
when :all
return true
when Integer
return false
when Range
return item.sub_navigation.level <= level.max
end
false
end
def expand_sub_navigation?(item)
expand_all? || item.selected?
end
# to allow overriding when there is specific logic determining
# when a link should not be rendered (eg. breadcrumbs renderer
# does not render the final breadcrumb as a link when instructed
# not to do so.)
def suppress_link?(item)
item.url.nil?
end
# determine and return link or static content depending on
# item/renderer conditions.
def tag_for(item)
if suppress_link?(item)
content_tag('span', item.name, link_options_for(item).except(:method))
else
link_to(item.name, item.url, options_for(item))
end
end
# to allow overriding when link options should be special-cased
# (eg. links renderer uses item options for the a-tag rather
# than an li-tag).
def options_for(item)
link_options_for(item)
end
# Extracts the options relevant for the generated link
#
def link_options_for(item)
special_options = {:method => item.method, :class => item.selected_class}.reject {|k, v| v.nil? }
link_options = item.html_options[:link]
return special_options unless link_options
opts = special_options.merge(link_options)
opts[:class] = [link_options[:class], item.selected_class].flatten.compact.join(' ')
opts.delete(:class) if opts[:class].nil? || opts[:class] == ''
opts
end
end
end
end
simple-navigation-3.11.0/lib/simple_navigation/rendering/renderer/text.rb 0000644 0001756 0001756 00000001326 12202013367 025674 0 ustar synrg synrg module SimpleNavigation
module Renderer
# Renders the 'chain' of selected navigation items as simple text items, joined with an optional separator (similar to breadcrumbs, but without markup).
#
class Text < SimpleNavigation::Renderer::Base
def render(item_container)
list(item_container).compact.join(options[:join_with] || " ")
end
private
def list(item_container)
item_container.items.inject([]) do |array, item|
if item.selected?
array + [item.name(:apply_generator => false)] + (include_sub_navigation?(item) ? list(item.sub_navigation) : [])
else
array
end
end
end
end
end
end
simple-navigation-3.11.0/lib/simple_navigation/rendering/renderer/json.rb 0000644 0001756 0001756 00000001376 12202013367 025666 0 ustar synrg synrg module SimpleNavigation
module Renderer
# Renders the navigation items as a object tree serialized as a json string, can also output raw ruby Hashes
class Json < SimpleNavigation::Renderer::Base
def render(item_container)
results = hash_render(item_container)
results = results.to_json unless options[:as_hash]
results
end
private
def hash_render(item_container)
return nil if item_container.nil?
item_container.items.map do |item|
item_hash = {
:name => item.name,
:url => item.url,
:selected => item.selected?,
:items => hash_render(item.sub_navigation)
}
end
end
end
end
end
simple-navigation-3.11.0/lib/simple_navigation/rendering/helpers.rb 0000644 0001756 0001756 00000017126 12202013367 024551 0 ustar synrg synrg module SimpleNavigation
# View helpers to render the navigation.
#
# Use render_navigation as following to render your navigation:
# * call
render_navigation without :level option to render your complete navigation as nested tree.
# * call
render_navigation(:level => x) to render a specific navigation level (e.g. :level => 1 to render your primary navigation, :level => 2 to render the sub navigation and so forth)
# * call
render_navigation(:level => 2..3) to render navigation levels 2 and 3).
# For example, you could use render_navigation(:level => 1) to render your primary navigation as tabs and render_navigation(:level => 2..3) to render the rest of the navigation as a tree in a sidebar.
#
# ==== Examples (using Haml)
# #primary_navigation= render_navigation(:level => 1)
#
# #sub_navigation= render_navigation(:level => 2)
#
# #nested_navigation= render_navigation
#
# #top_navigation= render_navigation(:level => 1..2)
# #sidebar_navigation= render_navigation(:level => 3)
module Helpers
# Renders the navigation according to the specified options-hash.
#
# The following options are supported:
# *
:level - defaults to :all which renders the the sub_navigation for an active primary_navigation inside that active primary_navigation item.
# Specify a specific level to only render that level of navigation (e.g. :level => 1 for primary_navigation etc...).
# Specifiy a Range of levels to render only those specific levels (e.g. :level => 1..2 to render both your first and second levels, maybe you want to render your third level somewhere else on the page)
# *
:expand_all - defaults to false. If set to true the all specified levels will be rendered as a fully expanded tree (always open). This is useful for javascript menus like Superfish.
# *
:context - specifies the context for which you would render the navigation. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb).
# If you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call
render_navigation(:context => :admin) the file config/admin_navigation.rb
# will be loaded and used for rendering the navigation.
# *
:items - you can specify the items directly (e.g. if items are dynamically generated from database). See SimpleNavigation::ItemsProvider for documentation on what to provide as items.
# *
:renderer - specify the renderer to be used for rendering the navigation. Either provide the Class or a symbol matching a registered renderer. Defaults to :list (html list renderer).
#
# Instead of using the
:items option, a block can be passed to specify the items dynamically
#
# render_navigation do |menu|
# menu.item :posts, "Posts", posts_path
# end
#
def render_navigation(options={},&block)
container = active_navigation_item_container(options,&block)
container && container.render(options)
end
# Returns the name of the currently active navigation item belonging to the specified level.
#
# See Helpers#active_navigation_item for supported options.
#
# Returns an empty string if no active item can be found for the specified options
def active_navigation_item_name(options={})
active_navigation_item(options,'') { |item| item.name(:apply_generator => false) }
end
# Returns the key of the currently active navigation item belonging to the specified level.
#
# See Helpers#active_navigation_item for supported options.
#
# Returns
nil if no active item can be found for the specified options
def active_navigation_item_key(options={})
active_navigation_item(options) { |item| item.key }
end
# Returns the currently active navigation item belonging to the specified level.
#
# The following options are supported:
# *
:level - defaults to :all which returns the most specific/deepest selected item (the leaf).
# Specify a specific level to only look for the selected item in the specified level of navigation (e.g. :level => 1 for primary_navigation etc...).
# *
:context - specifies the context for which you would like to find the active navigation item. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb).
# If you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call
active_navigation_item_name(:context => :admin) the file config/admin_navigation.rb
# will be loaded and used for searching the active item.
# *
:items - you can specify the items directly (e.g. if items are dynamically generated from database). See SimpleNavigation::ItemsProvider for documentation on what to provide as items.
#
# Returns the supplied
value_for_nil object (
nil
# by default) if no active item can be found for the specified
# options
def active_navigation_item(options={},value_for_nil = nil)
options[:level] = :leaves if options[:level].nil? || options[:level] == :all
container = active_navigation_item_container(options)
if container && (item = container.selected_item)
block_given? ? yield(item) : item
else
value_for_nil
end
end
# Returns the currently active item container belonging to the specified level.
#
# The following options are supported:
# *
:level - defaults to :all which returns the least specific/shallowest selected item.
# Specify a specific level to only look for the selected item in the specified level of navigation (e.g. :level => 1 for primary_navigation etc...).
# *
:context - specifies the context for which you would like to find the active navigation item. Defaults to :default which loads the default navigation.rb (i.e. config/navigation.rb).
# If you specify a context then the plugin tries to load the configuration file for that context, e.g. if you call
active_navigation_item_name(:context => :admin) the file config/admin_navigation.rb
# will be loaded and used for searching the active item.
# *
:items - you can specify the items directly (e.g. if items are dynamically generated from database). See SimpleNavigation::ItemsProvider for documentation on what to provide as items.
#
# Returns
nil if no active item container can be found
def active_navigation_item_container(options={},&block)
options = SimpleNavigation::Helpers::apply_defaults(options)
SimpleNavigation::Helpers::load_config(options,self,&block)
container = SimpleNavigation.active_item_container_for(options[:level])
end
class << self
def load_config(options,includer,&block)
ctx = options.delete(:context)
SimpleNavigation.init_adapter_from includer
SimpleNavigation.load_config(ctx)
SimpleNavigation::Configuration.eval_config(ctx)
if block_given? || options[:items]
SimpleNavigation.config.items(options[:items],&block)
end
SimpleNavigation.handle_explicit_navigation if SimpleNavigation.respond_to?(:handle_explicit_navigation)
raise "no primary navigation defined, either use a navigation config file or pass items directly to render_navigation" unless SimpleNavigation.primary_navigation
end
def apply_defaults(options)
options[:level] = options.delete(:levels) if options[:levels]
{:context => :default, :level => :all}.merge(options)
end
end
end
end
simple-navigation-3.11.0/lib/generators/ 0000755 0001756 0001756 00000000000 12202013367 017237 5 ustar synrg synrg simple-navigation-3.11.0/lib/generators/navigation_config/ 0000755 0001756 0001756 00000000000 12202013367 022723 5 ustar synrg synrg simple-navigation-3.11.0/lib/generators/navigation_config/navigation_config_generator.rb 0000644 0001756 0001756 00000001047 12202013367 031004 0 ustar synrg synrg class NavigationConfigGenerator < Rails::Generators::Base
def self.source_root
@source_root ||= File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','generators','navigation_config', 'templates'))
end
desc 'Creates a template config file for the simple-navigation plugin. You will find the generated file in config/navigation.rb.'
def navigation_config
copy_file('config/navigation.rb', 'config/navigation.rb')
say File.read(File.expand_path(File.join(File.dirname(__FILE__),'..','..','..','README')))
end
end simple-navigation-3.11.0/lib/simple_navigation.rb 0000644 0001756 0001756 00000014011 12202013367 021120 0 ustar synrg synrg # cherry picking active_support stuff
require 'active_support/core_ext/array'
require 'active_support/core_ext/hash'
require 'active_support/core_ext/module/attribute_accessors'
require 'simple_navigation/core'
require 'simple_navigation/rendering'
require 'simple_navigation/adapters'
require 'forwardable'
# A plugin for generating a simple navigation. See README for resources on usage instructions.
module SimpleNavigation
mattr_accessor :adapter_class, :adapter, :config_files, :config_file_paths, :default_renderer, :registered_renderers, :root, :environment
# Cache for loaded config files
self.config_files = {}
# Allows for multiple config_file_paths. Needed if a plugin itself uses simple-navigation and therefore has its own config file
self.config_file_paths = []
# Maps renderer keys to classes. The keys serve as shortcut in the render_navigation calls (:renderer => :list)
self.registered_renderers = {
:list => SimpleNavigation::Renderer::List,
:links => SimpleNavigation::Renderer::Links,
:breadcrumbs => SimpleNavigation::Renderer::Breadcrumbs,
:text => SimpleNavigation::Renderer::Text,
:json => SimpleNavigation::Renderer::Json
}
class << self
extend Forwardable
def_delegators :adapter, :request, :request_uri, :request_path, :context_for_eval, :current_page?
def_delegators :adapter_class, :register
# Sets the root path and current environment as specified. Also sets the default config_file_path.
def set_env(root, environment)
self.root = root
self.environment = environment
self.config_file_paths << SimpleNavigation.default_config_file_path
end
# Returns the current framework in which the plugin is running.
def framework
return :rails if defined?(Rails)
return :padrino if defined?(Padrino)
return :sinatra if defined?(Sinatra)
return :nanoc if defined?(Nanoc3)
raise 'simple_navigation currently only works for Rails, Sinatra and Padrino apps'
end
# Loads the adapter for the current framework
def load_adapter
self.adapter_class = case framework
when :rails
SimpleNavigation::Adapters::Rails
when :sinatra
SimpleNavigation::Adapters::Sinatra
when :padrino
SimpleNavigation::Adapters::Padrino
when :nanoc
SimpleNavigation::Adapters::Nanoc
end
end
# Creates a new adapter instance based on the context in which render_navigation has been called.
def init_adapter_from(context)
self.adapter = self.adapter_class.new(context)
end
def default_config_file_path
File.join(SimpleNavigation.root, 'config')
end
# Returns true if the config_file for specified context does exist.
def config_file?(navigation_context = :default)
!!config_file(navigation_context)
end
# Returns the path to the config file for the given navigation context or nil if no matching config file can be found.
# If multiple config_paths are set, it returns the first matching path.
def config_file(navigation_context = :default)
config_file_paths.collect { |path| File.join(path, config_file_name(navigation_context)) }.detect {|full_path| File.exists?(full_path)}
end
# Returns the name of the config file for the given navigation_context
def config_file_name(navigation_context = :default)
prefix = navigation_context == :default ? '' : "#{navigation_context.to_s.underscore}_"
"#{prefix}navigation.rb"
end
# Resets the list of config_file_paths to the specified path
def config_file_path=(path)
self.config_file_paths = [path]
end
# Reads the config_file for the specified navigation_context and stores it for later evaluation.
def load_config(navigation_context = :default)
raise "Config file '#{config_file_name(navigation_context)}' not found in path(s) #{config_file_paths.join(', ')}!" unless config_file?(navigation_context)
if self.environment == 'production'
self.config_files[navigation_context] ||= IO.read(config_file(navigation_context))
else
self.config_files[navigation_context] = IO.read(config_file(navigation_context))
end
end
# Returns the singleton instance of the SimpleNavigation::Configuration
def config
SimpleNavigation::Configuration.instance
end
# Returns the ItemContainer that contains the items for the primary navigation
def primary_navigation
config.primary_navigation
end
# Returns the active item container for the specified level. Valid levels are
# * :all - in this case the primary_navigation is returned.
# * :leaves - the 'deepest' active item_container will be returned
# * a specific level - the active item_container for the specified level will be returned
# * a range of levels - the active item_container for the range's minimum will be returned
#
# Returns nil if there is no active item_container for the specified level.
def active_item_container_for(level)
case level
when :all
self.primary_navigation
when :leaves
self.primary_navigation.active_leaf_container
when Integer
self.primary_navigation.active_item_container_for(level)
when Range
self.primary_navigation.active_item_container_for(level.min)
else
raise ArgumentError, "Invalid navigation level: #{level}"
end
end
# Registers a renderer.
#
# === Example
# To register your own renderer:
#
# SimpleNavigation.register_renderer :my_renderer => My::RendererClass
#
# Then in the view you can call:
#
# render_navigation(:renderer => :my_renderer)
def register_renderer(renderer_hash)
self.registered_renderers.merge!(renderer_hash)
end
private
def apply_defaults(options)
options[:level] = options.delete(:levels) if options[:levels]
{:context => :default, :level => :all}.merge(options)
end
end
end
SimpleNavigation.load_adapter
simple-navigation-3.11.0/Gemfile 0000644 0001756 0001756 00000000562 12202013367 015616 0 ustar synrg synrg source "https://rubygems.org"
gem 'activesupport', '>= 2.3.2'
# install the rails group if you want to ensure rails compatibility -
# but you've already got rails installed anyway, right? :-)
group :rails do
gem 'actionpack', '>= 2.3.2'
end
group :development do
gem 'rspec', '>= 2.0.1'
gem 'json_spec', '~> 1.1.1'
gem 'rake'
gem 'rdoc'
gem 'jeweler'
end
simple-navigation-3.11.0/metadata.yml 0000644 0001756 0001756 00000013711 12202013367 016626 0 ustar synrg synrg --- !ruby/object:Gem::Specification
name: simple-navigation
version: !ruby/object:Gem::Version
version: 3.11.0
prerelease:
platform: ruby
authors:
- Andi Schacke
- Mark J. Titorenko
autorequire:
bindir: bin
cert_chain: []
date: 2013-06-05 00:00:00.000000000 Z
dependencies:
- !ruby/object:Gem::Dependency
name: activesupport
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 2.3.2
type: :runtime
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 2.3.2
- !ruby/object:Gem::Dependency
name: rspec
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 2.0.1
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: 2.0.1
- !ruby/object:Gem::Dependency
name: json_spec
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: 1.1.1
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ~>
- !ruby/object:Gem::Version
version: 1.1.1
- !ruby/object:Gem::Dependency
name: rake
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- !ruby/object:Gem::Dependency
name: rdoc
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
- !ruby/object:Gem::Dependency
name: jeweler
requirement: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
type: :development
prerelease: false
version_requirements: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
description: With the simple-navigation gem installed you can easily create multilevel
navigations for your Rails, Sinatra or Padrino applications. The navigation is defined
in a single configuration file. It supports automatic as well as explicit highlighting
of the currently active navigation through regular expressions.
email: andreas.schacke@gmail.com
executables: []
extensions: []
extra_rdoc_files:
- README
files:
- CHANGELOG
- Gemfile
- README
- Rakefile
- VERSION
- generators/navigation_config/USAGE
- generators/navigation_config/navigation_config_generator.rb
- generators/navigation_config/templates/config/navigation.rb
- lib/generators/navigation_config/navigation_config_generator.rb
- lib/simple-navigation.rb
- lib/simple_navigation.rb
- lib/simple_navigation/adapters.rb
- lib/simple_navigation/adapters/base.rb
- lib/simple_navigation/adapters/nanoc.rb
- lib/simple_navigation/adapters/padrino.rb
- lib/simple_navigation/adapters/rails.rb
- lib/simple_navigation/adapters/sinatra.rb
- lib/simple_navigation/core.rb
- lib/simple_navigation/core/configuration.rb
- lib/simple_navigation/core/item.rb
- lib/simple_navigation/core/item_adapter.rb
- lib/simple_navigation/core/item_container.rb
- lib/simple_navigation/core/items_provider.rb
- lib/simple_navigation/rails_controller_methods.rb
- lib/simple_navigation/rendering.rb
- lib/simple_navigation/rendering/helpers.rb
- lib/simple_navigation/rendering/renderer/base.rb
- lib/simple_navigation/rendering/renderer/breadcrumbs.rb
- lib/simple_navigation/rendering/renderer/json.rb
- lib/simple_navigation/rendering/renderer/links.rb
- lib/simple_navigation/rendering/renderer/list.rb
- lib/simple_navigation/rendering/renderer/text.rb
- rails/init.rb
- spec/lib/simple_navigation/adapters/padrino_spec.rb
- spec/lib/simple_navigation/adapters/rails_spec.rb
- spec/lib/simple_navigation/adapters/sinatra_spec.rb
- spec/lib/simple_navigation/core/configuration_spec.rb
- spec/lib/simple_navigation/core/item_adapter_spec.rb
- spec/lib/simple_navigation/core/item_container_spec.rb
- spec/lib/simple_navigation/core/item_spec.rb
- spec/lib/simple_navigation/core/items_provider_spec.rb
- spec/lib/simple_navigation/rails_controller_methods_spec.rb
- spec/lib/simple_navigation/rendering/helpers_spec.rb
- spec/lib/simple_navigation/rendering/renderer/base_spec.rb
- spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb
- spec/lib/simple_navigation/rendering/renderer/json_spec.rb
- spec/lib/simple_navigation/rendering/renderer/links_spec.rb
- spec/lib/simple_navigation/rendering/renderer/list_spec.rb
- spec/lib/simple_navigation/rendering/renderer/text_spec.rb
- spec/lib/simple_navigation_spec.rb
- spec/spec_helper.rb
homepage: http://github.com/andi/simple-navigation
licenses: []
post_install_message:
rdoc_options:
- --inline-source
- --charset=UTF-8
require_paths:
- lib
required_ruby_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
required_rubygems_version: !ruby/object:Gem::Requirement
none: false
requirements:
- - ! '>='
- !ruby/object:Gem::Version
version: '0'
requirements: []
rubyforge_project: andi
rubygems_version: 1.8.24
signing_key:
specification_version: 3
summary: simple-navigation is a ruby library for creating navigations (with multiple
levels) for your Rails2, Rails3, Sinatra or Padrino application.
test_files: []
simple-navigation-3.11.0/spec/ 0000755 0001756 0001756 00000000000 12202013367 015252 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/ 0000755 0001756 0001756 00000000000 12202013367 016020 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/simple_navigation_spec.rb 0000644 0001756 0001756 00000023672 12202013367 023101 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation do
before(:each) do
SimpleNavigation.config_file_path = 'path_to_config'
end
describe 'config_file_name' do
context 'for :default navigation_context' do
it "should return the name of the default config file" do
SimpleNavigation.config_file_name.should == 'navigation.rb'
end
end
context 'for other navigation_context' do
it "should return the name of the config file matching the specified context" do
SimpleNavigation.config_file_name(:my_other_context).should == 'my_other_context_navigation.rb'
end
it "should convert camelcase-contexts to underscore" do
SimpleNavigation.config_file_name(:WhyWouldYouDoThis).should == 'why_would_you_do_this_navigation.rb'
end
end
end
describe 'config_file_path=' do
before(:each) do
SimpleNavigation.config_file_paths = ['existing_path']
end
it "should override the config_file_paths" do
SimpleNavigation.config_file_path = 'new_path'
SimpleNavigation.config_file_paths.should == ['new_path']
end
end
describe 'config_file' do
context 'no config_file_paths are set' do
before(:each) do
SimpleNavigation.config_file_paths = []
end
it "should return nil" do
SimpleNavigation.config_file.should be_nil
end
end
context 'one config_file_path is set' do
before(:each) do
SimpleNavigation.config_file_paths = ['my_config_file_path']
end
context 'requested config file does exist' do
before(:each) do
File.stub!(:exists? => true)
end
it "should return the path to the config_file" do
SimpleNavigation.config_file.should == 'my_config_file_path/navigation.rb'
end
end
context 'requested config file does not exist' do
before(:each) do
File.stub!(:exists? => false)
end
it "should return nil" do
SimpleNavigation.config_file.should be_nil
end
end
end
context 'multiple config_file_paths are set' do
before(:each) do
SimpleNavigation.config_file_paths = ['first_path', 'second_path']
end
context 'requested config file does exist' do
before(:each) do
File.stub!(:exists? => true)
end
it "should return the path to the first matching config_file" do
SimpleNavigation.config_file.should == 'first_path/navigation.rb'
end
end
context 'requested config file does not exist' do
before(:each) do
File.stub!(:exists? => false)
end
it "should return nil" do
SimpleNavigation.config_file.should be_nil
end
end
end
end
describe 'config_file?' do
context 'config_file present' do
before(:each) do
SimpleNavigation.stub!(:config_file => 'file')
end
it {SimpleNavigation.config_file?.should be_true}
end
context 'config_file not present' do
before(:each) do
SimpleNavigation.stub!(:config_file => nil)
end
it {SimpleNavigation.config_file?.should be_false}
end
end
describe 'self.default_config_file_path' do
before(:each) do
SimpleNavigation.stub!(:root => 'root')
end
it {SimpleNavigation.default_config_file_path.should == 'root/config'}
end
describe 'regarding renderers' do
it "should have registered the builtin renderers by default" do
SimpleNavigation.registered_renderers.should_not be_empty
end
describe 'register_renderer' do
before(:each) do
@renderer = stub(:renderer)
end
it "should add the specified renderer to the list of renderers" do
SimpleNavigation.register_renderer(:my_renderer => @renderer)
SimpleNavigation.registered_renderers[:my_renderer].should == @renderer
end
end
end
describe 'set_env' do
before(:each) do
SimpleNavigation.config_file_paths = []
SimpleNavigation.stub!(:default_config_file_path => 'default_path')
SimpleNavigation.set_env('root', 'my_env')
end
it "should set the root" do
SimpleNavigation.root.should == 'root'
end
it "should set the environment" do
SimpleNavigation.environment.should == 'my_env'
end
it "should add the default-config path to the list of config_file_paths" do
SimpleNavigation.config_file_paths.should == ['default_path']
end
end
describe 'load_config' do
context 'config_file_path is set' do
before(:each) do
SimpleNavigation.stub!(:config_file => 'path_to_config_file')
end
context 'config_file does exist' do
before(:each) do
SimpleNavigation.stub!(:config_file? => true)
IO.stub!(:read => 'file_content')
end
it "should not raise an error" do
lambda{SimpleNavigation.load_config}.should_not raise_error
end
it "should read the specified config file from disc" do
IO.should_receive(:read).with('path_to_config_file')
SimpleNavigation.load_config
end
it "should store the read content in the module (default context)" do
SimpleNavigation.should_receive(:config_file).with(:default)
SimpleNavigation.load_config
SimpleNavigation.config_files[:default].should == 'file_content'
end
it "should store the content in the module (non default context)" do
SimpleNavigation.should_receive(:config_file).with(:my_context)
SimpleNavigation.load_config(:my_context)
SimpleNavigation.config_files[:my_context].should == 'file_content'
end
end
context 'config_file does not exist' do
before(:each) do
SimpleNavigation.stub!(:config_file? => false)
end
it {lambda{SimpleNavigation.load_config}.should raise_error}
end
end
context 'config_file_path is not set' do
before(:each) do
SimpleNavigation.config_file_path = nil
end
it {lambda{SimpleNavigation.load_config}.should raise_error}
end
describe 'regarding caching of the config-files' do
before(:each) do
IO.stub!(:read).and_return('file_content')
SimpleNavigation.config_file_path = 'path_to_config'
File.stub!(:exists? => true)
end
context "environment undefined" do
before(:each) do
SimpleNavigation.stub!(:environment => nil)
end
it "should load the config file twice" do
IO.should_receive(:read).twice
SimpleNavigation.load_config
SimpleNavigation.load_config
end
end
context "environment defined" do
before(:each) do
SimpleNavigation.stub!(:environment => 'production')
end
context "environment=production" do
it "should load the config file only once" do
IO.should_receive(:read).once
SimpleNavigation.load_config
SimpleNavigation.load_config
end
end
context "environment=development" do
before(:each) do
SimpleNavigation.stub!(:environment => 'development')
end
it "should load the config file twice" do
IO.should_receive(:read).twice
SimpleNavigation.load_config
SimpleNavigation.load_config
end
end
context "environment=test" do
before(:each) do
SimpleNavigation.stub!(:environment => 'test')
end
it "should load the config file twice" do
IO.should_receive(:read).twice
SimpleNavigation.load_config
SimpleNavigation.load_config
end
end
end
after(:each) do
SimpleNavigation.config_files = {}
end
end
end
describe 'config' do
it {SimpleNavigation.config.should == SimpleNavigation::Configuration.instance}
end
describe 'active_item_container_for' do
before(:each) do
@primary = stub(:primary)
SimpleNavigation.config.stub!(:primary_navigation => @primary)
end
context 'level is :all' do
it "should return the primary_navigation" do
SimpleNavigation.active_item_container_for(:all).should == @primary
end
end
context 'level is :leaves' do
it "should return the currently active leaf-container" do
@primary.should_receive(:active_leaf_container)
SimpleNavigation.active_item_container_for(:leaves)
end
end
context 'level is a Range' do
it "should take the min of the range to lookup the active container" do
@primary.should_receive(:active_item_container_for).with(2)
SimpleNavigation.active_item_container_for(2..3)
end
end
context 'level is an Integer' do
it "should consider the Integer to lookup the active container" do
@primary.should_receive(:active_item_container_for).with(1)
SimpleNavigation.active_item_container_for(1)
end
end
context 'level is something else' do
it "should raise an ArgumentError" do
lambda {SimpleNavigation.active_item_container_for('something else')}.should raise_error(ArgumentError)
end
end
end
describe 'load_adapter' do
context 'Rails' do
before(:each) do
SimpleNavigation.stub!(:framework => :rails)
SimpleNavigation.load_adapter
end
it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Rails}
end
context 'Padrino' do
before(:each) do
SimpleNavigation.stub!(:framework => :padrino)
SimpleNavigation.load_adapter
end
it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Padrino}
end
context 'Sinatra' do
before(:each) do
SimpleNavigation.stub!(:framework => :sinatra)
SimpleNavigation.load_adapter
end
it {SimpleNavigation.adapter_class.should == SimpleNavigation::Adapters::Sinatra}
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/ 0000755 0001756 0001756 00000000000 12202013367 021530 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/simple_navigation/adapters/ 0000755 0001756 0001756 00000000000 12202013367 023333 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/simple_navigation/adapters/rails_spec.rb 0000644 0001756 0001756 00000021537 12202013367 026014 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Adapters::Rails do
def create_adapter
SimpleNavigation::Adapters::Rails.new(@context)
end
before(:each) do
@context = stub(:context)
@controller = stub(:controller)
@context.stub!(:controller => @controller)
@request = stub(:request)
@template = stub(:template, :request => @request)
@adapter = create_adapter
end
describe 'self.register' do
before(:each) do
ActionController::Base.stub!(:include)
end
it "should call set_env" do
SimpleNavigation.should_receive(:set_env).with('./', 'test')
SimpleNavigation.register
end
it "should extend the ActionController::Base with the Helpers" do
ActionController::Base.should_receive(:include).with(SimpleNavigation::Helpers)
SimpleNavigation.register
end
it "should install the helper methods in the controller" do
ActionController::Base.should_receive(:helper_method).with(:render_navigation)
ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_name)
ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_key)
ActionController::Base.should_receive(:helper_method).with(:active_navigation_item)
ActionController::Base.should_receive(:helper_method).with(:active_navigation_item_container)
SimpleNavigation.register
end
end
describe 'initialize' do
context 'regarding setting the request' do
context 'template is present' do
before(:each) do
@controller.stub!(:instance_variable_get => @template)
@adapter = create_adapter
end
it {@adapter.request.should == @request}
end
context 'template is not present' do
before(:each) do
@controller.stub!(:instance_variable_get => nil)
end
it {@adapter.request.should be_nil}
end
end
context 'regarding setting the controller' do
it "should set the controller" do
@adapter.controller.should == @controller
end
end
context 'regarding setting the template' do
context 'template is stored in controller as instance_var (Rails2)' do
context 'template is set' do
before(:each) do
@controller.stub!(:instance_variable_get => @template)
@adapter = create_adapter
end
it {@adapter.template.should == @template}
end
context 'template is not set' do
before(:each) do
@controller.stub!(:instance_variable_get => nil)
@adapter = create_adapter
end
it {@adapter.template.should be_nil}
end
end
context 'template is stored in controller as view_context (Rails3)' do
context 'template is set' do
before(:each) do
@controller.stub!(:view_context => @template)
@adapter = create_adapter
end
it {@adapter.template.should == @template}
end
context 'template is not set' do
before(:each) do
@controller.stub!(:view_context => nil)
@adapter = create_adapter
end
it {@adapter.template.should be_nil}
end
end
end
end
describe 'request_uri' do
context 'request is set' do
context 'fullpath is defined on request' do
before(:each) do
@request = stub(:request, :fullpath => '/fullpath')
@adapter.stub!(:request => @request)
end
it {@adapter.request_uri.should == '/fullpath'}
end
context 'fullpath is not defined on request' do
before(:each) do
@request = stub(:request, :request_uri => '/request_uri')
@adapter.stub!(:request => @request)
end
it {@adapter.request_uri.should == '/request_uri'}
end
end
context 'request is not set' do
before(:each) do
@adapter.stub!(:request => nil)
end
it {@adapter.request_uri.should == ''}
end
end
describe 'request_path' do
context 'request is set' do
before(:each) do
@request = stub(:request, :path => '/request_path')
@adapter.stub!(:request => @request)
end
it {@adapter.request_path.should == '/request_path'}
end
context 'request is not set' do
before(:each) do
@adapter.stub!(:request => nil)
end
it {@adapter.request_path.should == ''}
end
end
describe 'context_for_eval' do
context 'controller is present' do
before(:each) do
@controller = stub(:controller)
@adapter.instance_variable_set(:@controller, @controller)
end
context 'template is present' do
before(:each) do
@template = stub(:template)
@adapter.instance_variable_set(:@template, @template)
end
it {@adapter.context_for_eval.should == @template}
end
context 'template is not present' do
before(:each) do
@adapter.instance_variable_set(:@template, nil)
end
it {@adapter.context_for_eval.should == @controller}
end
end
context 'controller is not present' do
before(:each) do
@adapter.instance_variable_set(:@controller, nil)
end
context 'template is present' do
before(:each) do
@template = stub(:template)
@adapter.instance_variable_set(:@template, @template)
end
it {@adapter.context_for_eval.should == @template}
end
context 'template is not present' do
before(:each) do
@adapter.instance_variable_set(:@template, nil)
end
it {lambda {@adapter.context_for_eval}.should raise_error}
end
end
end
describe 'current_page?' do
context 'template is set' do
before(:each) do
@adapter.stub!(:template => @template)
end
it "should delegate the call to the template" do
@template.should_receive(:current_page?).with(:page)
@adapter.current_page?(:page)
end
end
context 'template is not set' do
before(:each) do
@adapter.stub!(:template => nil)
end
it {@adapter.should_not be_current_page(:page)}
end
end
describe 'link_to' do
context 'template is set' do
before(:each) do
@adapter.stub!(:template => @template)
@adapter.stub!(:html_safe => 'safe_text')
@options = stub(:options)
end
it "should delegate the call to the template (with html_safe text)" do
@template.should_receive(:link_to).with('safe_text', 'url', @options)
@adapter.link_to('text', 'url', @options)
end
end
context 'template is not set' do
before(:each) do
@adapter.stub!(:template => nil)
end
it {@adapter.link_to('text', 'url', @options).should be_nil}
end
end
describe 'content_tag' do
context 'template is set' do
before(:each) do
@adapter.stub!(:template => @template)
@adapter.stub!(:html_safe => 'safe_text')
@options = stub(:options)
end
it "should delegate the call to the template (with html_safe text)" do
@template.should_receive(:content_tag).with(:div, 'safe_text', @options)
@adapter.content_tag(:div, 'text', @options)
end
end
context 'template is not set' do
before(:each) do
@adapter.stub!(:template => nil)
end
it {@adapter.content_tag(:div, 'text', @options).should be_nil}
end
end
describe 'self.extract_controller_from' do
context 'object responds to controller' do
before(:each) do
@context.stub!(:controller => @controller)
end
it "should return the controller" do
@adapter.send(:extract_controller_from, @context).should == @controller
end
end
context 'object does not respond to controller' do
before(:each) do
@context = stub(:context)
end
it "should return the context" do
@adapter.send(:extract_controller_from, @context).should == @context
end
end
end
describe 'html_safe' do
before(:each) do
@input = stub :input
end
context 'input does respond to html_safe' do
before(:each) do
@safe = stub :safe
@input.stub!(:html_safe => @safe)
end
it {@adapter.send(:html_safe, @input).should == @safe}
end
context 'input does not respond to html_safe' do
it {@adapter.send(:html_safe, @input).should == @input}
end
end
describe 'template_from' do
context 'Rails3' do
before(:each) do
@controller.stub!(:view_context => 'view')
end
it {@adapter.send(:template_from, @controller).should == 'view'}
end
context 'Rails2' do
before(:each) do
@controller.instance_variable_set(:@template, 'view')
end
it {@adapter.send(:template_from, @controller).should == 'view'}
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/adapters/padrino_spec.rb 0000644 0001756 0001756 00000001521 12202013367 026325 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Adapters::Padrino do
def create_adapter
SimpleNavigation::Adapters::Padrino.new(@context)
end
before(:each) do
@request = stub(:request)
@content = stub(:content)
@context = stub(:context, :request => @request)
@adapter = create_adapter
end
describe 'link_to' do
it "should delegate to context" do
@context.should_receive(:link_to).with('name', 'url', :my_option => true)
@adapter.link_to('name', 'url', :my_option => true)
end
end
describe 'content_tag' do
it "should delegate to context" do
@content.should_receive(:html_safe).and_return('content')
@context.should_receive(:content_tag).with('type', 'content', :my_option => true)
@adapter.content_tag('type', @content, :my_option => true)
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/adapters/sinatra_spec.rb 0000644 0001756 0001756 00000006112 12202013367 026333 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Adapters::Sinatra do
def create_adapter
SimpleNavigation::Adapters::Sinatra.new(@context)
end
before(:each) do
@context = stub(:context)
@request = stub(:request, :fullpath => '/full?param=true', :path => '/full')
@context.stub!(:request => @request)
@adapter = create_adapter
end
describe 'context_for_eval' do
it "should raise error if no context" do
@adapter.stub!(:context => nil)
lambda {@adapter.context_for_eval}.should raise_error
end
it "should return the context" do
@adapter.context_for_eval.should == @context
end
end
describe 'request_uri' do
it {@adapter.request_uri.should == '/full?param=true'}
end
describe 'request_path' do
it {@adapter.request_path.should == '/full'}
end
describe 'current_page?' do
before(:each) do
@request.stub!(:scheme => 'http', :host_with_port => 'my_host:5000')
end
describe 'when URL is not encoded' do
it {@adapter.current_page?('/full?param=true').should be_true}
it {@adapter.current_page?('/full?param3=true').should be_false}
it {@adapter.current_page?('/full').should be_true}
it {@adapter.current_page?('http://my_host:5000/full?param=true').should be_true}
it {@adapter.current_page?('http://my_host:5000/full?param3=true').should be_false}
it {@adapter.current_page?('http://my_host:5000/full').should be_true}
it {@adapter.current_page?('https://my_host:5000/full').should be_false}
it {@adapter.current_page?('http://my_host:6000/full').should be_false}
it {@adapter.current_page?('http://my_other_host:5000/full').should be_false}
end
describe 'when URL is encoded' do
before(:each) do
@request.stub!(:fullpath => '/full%20with%20spaces?param=true', :path => '/full%20with%20spaces')
end
it {@adapter.current_page?('/full%20with%20spaces?param=true').should be_true}
it {@adapter.current_page?('/full%20with%20spaces?param3=true').should be_false}
it {@adapter.current_page?('/full%20with%20spaces').should be_true}
it {@adapter.current_page?('http://my_host:5000/full%20with%20spaces?param=true').should be_true}
it {@adapter.current_page?('http://my_host:5000/full%20with%20spaces?param3=true').should be_false}
it {@adapter.current_page?('http://my_host:5000/full%20with%20spaces').should be_true}
it {@adapter.current_page?('https://my_host:5000/full%20with%20spaces').should be_false}
it {@adapter.current_page?('http://my_host:6000/full%20with%20spaces').should be_false}
it {@adapter.current_page?('http://my_other_host:5000/full%20with%20spaces').should be_false}
end
end
describe 'link_to' do
it "should return a link" do
@adapter.link_to('link', 'url', :class => 'clazz', :id => 'id').should == "
link"
end
end
describe 'content_tag' do
it "should return a tag" do
@adapter.content_tag(:div, 'content', :class => 'clazz', :id => 'id').should == "
content
"
end
end
end simple-navigation-3.11.0/spec/lib/simple_navigation/core/ 0000755 0001756 0001756 00000000000 12202013367 022460 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/simple_navigation/core/items_provider_spec.rb 0000644 0001756 0001756 00000003416 12202013367 027056 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::ItemsProvider do
before(:each) do
@provider = stub(:provider)
@items_provider = SimpleNavigation::ItemsProvider.new(@provider)
end
describe 'initialize' do
it "should set the provider" do
@items_provider.provider.should == @provider
end
end
describe 'items' do
before(:each) do
@items = stub(:items)
end
context 'provider is symbol' do
before(:each) do
@items_provider.instance_variable_set(:@provider, :provider_method)
@context = stub(:context, :provider_method => @items)
SimpleNavigation.stub!(:context_for_eval => @context)
end
it "should call the method specified by symbol on the context" do
@context.should_receive(:provider_method)
@items_provider.items
end
it "should return the items returned by the helper method" do
@items_provider.items.should == @items
end
end
context 'provider responds to items' do
before(:each) do
@provider.stub!(:items => @items)
end
it "should get the items from the items_provider" do
@provider.should_receive(:items)
@items_provider.items
end
it "should return the items of the provider" do
@items_provider.items.should == @items
end
end
context 'provider is a collection' do
before(:each) do
@items_collection = []
@items_provider.instance_variable_set(:@provider, @items_collection)
end
it "should return the collection itsself" do
@items_provider.items.should == @items_collection
end
end
context 'neither symbol nor items_provider.items nor collection' do
it {lambda {@items_provider.items}.should raise_error}
end
end
end simple-navigation-3.11.0/spec/lib/simple_navigation/core/item_adapter_spec.rb 0000644 0001756 0001756 00000013250 12202013367 026456 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::ItemAdapter, 'when item is an object' do
before(:each) do
@item = stub(:item)
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
describe 'key' do
it "should delegate key to item" do
@item.should_receive(:key)
@item_adapter.key
end
end
describe 'url' do
it "should delegate url to item" do
@item.should_receive(:url)
@item_adapter.url
end
end
describe 'name' do
it "should delegate name to item" do
@item.should_receive(:name)
@item_adapter.name
end
end
describe 'initialize' do
it "should set the item" do
@item_adapter.item.should == @item
end
end
describe 'options' do
context 'item does respond to options' do
before(:each) do
@options = stub(:options)
@item.stub!(:options => @options)
end
it "should return the item's options'" do
@item_adapter.options.should == @options
end
end
context 'item does not respond to options' do
it "should return an empty hash" do
@item_adapter.options.should == {}
end
end
end
describe 'items' do
context 'item does respond to items' do
context 'items is nil' do
before(:each) do
@item.stub!(:items => nil)
end
it "should return nil" do
@item_adapter.items.should be_nil
end
end
context 'items is not nil' do
context 'items is empty' do
before(:each) do
@item.stub!(:items => [])
end
it "should return nil" do
@item_adapter.items.should be_nil
end
end
context 'items is not empty' do
before(:each) do
@items = stub(:items, :empty? => false)
@item.stub!(:items => @items)
end
it "should return the items" do
@item_adapter.items.should == @items
end
end
end
end
context 'item does not respond to items' do
it "should return nil" do
@item_adapter.items.should be_nil
end
end
end
describe 'to_simple_navigation_item' do
before(:each) do
@container = stub(:container)
@item.stub!(:url => 'url', :name => 'name', :key => 'key', :options => {}, :items => [])
end
it "should create a SimpleNavigation::Item" do
SimpleNavigation::Item.should_receive(:new).with(@container, 'key', 'name', 'url', {}, nil)
@item_adapter.to_simple_navigation_item(@container)
end
end
end
describe SimpleNavigation::ItemAdapter, 'when item is a hash' do
before(:each) do
@item = {:key => 'key', :url => 'url', :name => 'name'}
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
describe 'key' do
it "should delegate key to item" do
@item_adapter.item.should_receive(:key)
@item_adapter.key
end
end
describe 'url' do
it "should delegate url to item" do
@item_adapter.item.should_receive(:url)
@item_adapter.url
end
end
describe 'name' do
it "should delegate name to item" do
@item_adapter.item.should_receive(:name)
@item_adapter.name
end
end
describe 'initialize' do
it "should set the item" do
@item_adapter.item.should_not be_nil
end
it "should have converted the item into an object" do
@item_adapter.item.should respond_to(:url)
end
end
describe 'options' do
context 'item does respond to options' do
before(:each) do
@item = {:key => 'key', :url => 'url', :name => 'name', :options => {:my => :options}}
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
it "should return the item's options'" do
@item_adapter.options.should == {:my => :options}
end
end
context 'item does not respond to options' do
it "should return an empty hash" do
@item_adapter.options.should == {}
end
end
end
describe 'items' do
context 'item does respond to items' do
context 'items is nil' do
before(:each) do
@item = {:key => 'key', :url => 'url', :name => 'name', :items => nil}
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
it "should return nil" do
@item_adapter.items.should be_nil
end
end
context 'items is not nil' do
context 'items is empty' do
before(:each) do
@item = {:key => 'key', :url => 'url', :name => 'name', :items => []}
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
it "should return nil" do
@item_adapter.items.should be_nil
end
end
context 'items is not empty' do
before(:each) do
@item = {:key => 'key', :url => 'url', :name => 'name', :items => ['not', 'empty']}
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
it "should return the items" do
@item_adapter.items.should == ['not', 'empty']
end
end
end
end
context 'item does not respond to items' do
it "should return nil" do
@item_adapter.items.should be_nil
end
end
end
describe 'to_simple_navigation_item' do
before(:each) do
@container = stub(:container)
@item = {:key => 'key', :url => 'url', :name => 'name', :items => [], :options => {}}
@item_adapter = SimpleNavigation::ItemAdapter.new(@item)
end
it "should create a SimpleNavigation::Item" do
SimpleNavigation::Item.should_receive(:new).with(@container, 'key', 'name', 'url', {}, nil)
@item_adapter.to_simple_navigation_item(@container)
end
end
end simple-navigation-3.11.0/spec/lib/simple_navigation/core/item_spec.rb 0000644 0001756 0001756 00000046061 12202013367 024764 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Item do
before(:each) do
@item_container = stub(:item_container, :level => 1, :selected_class => nil).as_null_object
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
@adapter = stub(:adapter)
SimpleNavigation.stub!(:adapter => @adapter)
end
describe 'initialize' do
context 'subnavigation' do
before(:each) do
@subnav_container = stub(:subnav_container).as_null_object
SimpleNavigation::ItemContainer.stub!(:new => @subnav_container)
end
context 'block given' do
it "should create a new ItemContainer with a level+1" do
SimpleNavigation::ItemContainer.should_receive(:new).with(2)
SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}) {}
end
it "should call the block" do
@subnav_container.should_receive(:test)
SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}) {|subnav| subnav.test}
end
end
context 'no block given' do
context 'items given' do
before(:each) do
@items = stub(:items)
end
it "should create a new ItemContainer with a level+1" do
SimpleNavigation::ItemContainer.should_receive(:new).with(2)
SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, @items)
end
it "should set the items on the subnav_container" do
@subnav_container.should_receive(:items=).with(@items)
SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {}, @items)
end
end
context 'no items given' do
it "should not create a new ItemContainer" do
SimpleNavigation::ItemContainer.should_not_receive(:new)
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
end
end
end
end
context ':method option' do
context 'defined' do
before(:each) do
@options = {:method => :delete}
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
end
it 'should set the method as instance_var' do
@item.method.should == :delete
end
it 'should set the html-options without the method' do
@item.instance_variable_get(:@html_options).key?(:method).should be_false
end
end
context 'undefined' do
it 'should set the instance-var to nil' do
@item.method.should be_nil
end
end
end
context 'setting class and id on the container' do
before(:each) do
@options = {:container_class => 'container_class', :container_id => 'container_id'}
end
it {@item_container.should_receive(:dom_class=).with('container_class')}
it {@item_container.should_receive(:dom_id=).with('container_id')}
after(:each) do
SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
end
end
context ':highlights_on option' do
context 'defined' do
before(:each) do
@highlights_on = stub(:option)
@options = {:highlights_on => @highlights_on}
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
end
it 'should set the method as instance_var' do
@item.highlights_on.should == @highlights_on
end
it 'should set the html-options without the method' do
@item.instance_variable_get(:@html_options).key?(:highlights_on).should be_false
end
end
context 'undefined' do
it 'should set the instance-var to nil' do
@item.highlights_on.should be_nil
end
end
end
context 'url' do
context 'url is a string' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
end
it {@item.url.should == 'url'}
end
context 'url is a proc' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', Proc.new {"my_" + "url"}, {})
end
it {@item.url.should == 'my_url'}
end
context 'url is nil' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', nil, {})
end
it {@item.url.should == nil}
end
context 'url is unspecified' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name')
end
it {@item.url.should == nil}
end
end
context 'optional url and optional options' do
context 'when constructed without any optional parameters' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name')
end
it {@item.url.should == nil}
it {@item.instance_variable_get(:@html_options).should == {}}
end
context 'when constructed with only a url' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url')
end
it {@item.url.should == 'url'}
it {@item.instance_variable_get(:@html_options).should == {}}
end
context 'when constructed with only options' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', {:option => true})
end
it {@item.url.should == nil}
it {@item.instance_variable_get(:@html_options).should == {:option => true}}
end
context 'when constructed with a url and options' do
before(:each) do
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {:option => true})
end
it {@item.url.should == 'url'}
it {@item.instance_variable_get(:@html_options).should == {:option => true}}
end
end
end
describe 'name' do
before(:each) do
SimpleNavigation.config.stub!(:name_generator => Proc.new {|name| "
#{name}"})
end
context 'default (generator is applied)' do
it {@item.name.should == "
name"}
end
context 'generator is skipped' do
it {@item.name(:apply_generator => false).should == 'name'}
end
end
describe 'selected?' do
context 'explicitly selected' do
before(:each) do
@item.stub!(:selected_by_config? => true)
end
it {@item.should be_selected}
it "should not evaluate the subnav or urls" do
@item.should_not_receive(:selected_by_subnav?)
@item.should_not_receive(:selected_by_condition?)
@item.selected?
end
end
context 'not explicitly selected' do
before(:each) do
@item.stub!(:selected_by_config? => false)
end
context 'subnav is selected' do
before(:each) do
@item.stub!(:selected_by_subnav? => true)
end
it {@item.should be_selected}
end
context 'subnav is not selected' do
before(:each) do
@item.stub!(:selected_by_subnav? => false)
end
context 'selected by condition' do
before(:each) do
@item.stub!(:selected_by_condition? => true)
end
it {@item.should be_selected}
end
context 'not selected by condition' do
before(:each) do
@item.stub!(:selected_by_condition? => false)
end
it {@item.should_not be_selected}
end
end
end
end
describe 'selected_class' do
context 'selected_class is defined in context' do
before(:each) do
@item_container = stub(:item_container, :level => 1, :selected_class => 'context_defined').as_null_object
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
@item.stub!(:selected? => true)
end
it {@item.instance_eval {selected_class.should == 'context_defined'}}
end
context 'item is selected' do
before(:each) do
@item.stub!(:selected? => true)
end
it {@item.instance_eval {selected_class.should == 'selected'}}
end
context 'item is not selected' do
before(:each) do
@item.stub!(:selected? => false)
end
it {@item.instance_eval {selected_class.should == nil}}
end
end
describe 'html_options' do
describe 'class' do
context 'with classes defined in options' do
before(:each) do
@options = {:class => 'my_class'}
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
end
context 'with item selected' do
before(:each) do
@item.stub!(:selected? => true, :selected_by_condition? => true)
end
it {@item.html_options[:class].should == 'my_class selected simple-navigation-active-leaf'}
end
context 'with item not selected' do
before(:each) do
@item.stub!(:selected? => false, :selected_by_condition? => false)
end
it {@item.html_options[:class].should == 'my_class'}
end
end
context 'without classes in options' do
before(:each) do
@options = {}
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', @options)
end
context 'with item selected' do
before(:each) do
@item.stub!(:selected? => true, :selected_by_condition? => true)
end
it {@item.html_options[:class].should == 'selected simple-navigation-active-leaf'}
end
context 'with item not selected' do
before(:each) do
@item.stub!(:selected? => false, :selected_by_condition? => false)
end
it {@item.html_options[:class].should be_blank}
end
end
end
describe 'id' do
context 'with autogenerate_item_ids == true' do
before(:each) do
@item.stub!(:autogenerate_item_ids? => true)
@item.stub!(:selected? => false, :selected_by_condition? => false)
end
context 'with id defined in options' do
before(:each) do
@item.html_options = {:id => 'my_id'}
end
it {@item.html_options[:id].should == 'my_id'}
end
context 'with no id defined in options (using default id)' do
before(:each) do
@item.html_options = {}
end
it {@item.html_options[:id].should == 'my_key'}
end
end
context 'with autogenerate_item_ids == false' do
before(:each) do
@item.stub!(:autogenerate_item_ids? => false)
@item.stub!(:selected? => false, :selected_by_condition? => false)
end
context 'with id defined in options' do
before(:each) do
@item.html_options = {:id => 'my_id'}
end
it {@item.html_options[:id].should == 'my_id'}
end
context 'with no id definied in options (using default id)' do
before(:each) do
@item.html_options = {}
end
it {@item.html_options[:id].should be_nil}
end
end
end
end
describe 'selected_by_subnav?' do
context 'item has subnav' do
before(:each) do
@sub_navigation = stub(:sub_navigation)
@item.stub!(:sub_navigation => @sub_navigation)
end
it "should return true if subnav is selected" do
@sub_navigation.stub!(:selected? => true, :selected_by_condition? => true)
@item.should be_selected_by_subnav
end
it "should return false if subnav is not selected" do
@sub_navigation.stub!(:selected? => false, :selected_by_condition? => true)
@item.should_not be_selected_by_subnav
end
end
context 'item does not have subnav' do
before(:each) do
@item.stub!(:sub_navigation => @sub_navigation)
end
it {@item.should_not be_selected_by_subnav}
end
end
describe 'selected_by_condition?' do
context ':highlights_on option is set' do
before(:each) do
@item.stub!(:highlights_on => /^\/current/)
SimpleNavigation.stub!(:request_uri => '/current_url')
end
it "should not check for autohighlighting" do
@item.should_not_receive(:auto_highlight?)
@item.send(:selected_by_condition?)
end
context ':highlights_on is a regexp' do
context 'regexp matches current_url' do
it {@item.send(:selected_by_condition?).should be_true}
end
context 'regexp does not match current_url' do
before(:each) do
@item.stub!(:highlights_on => /^\/no_match/)
end
it {@item.send(:selected_by_condition?).should be_false}
end
end
context ':highlights_on is a lambda' do
context 'truthy lambda results in selection' do
before(:each) do
@item.stub!(:highlights_on => lambda{true})
end
it {@item.send(:selected_by_condition?).should be_true}
end
context 'falsey lambda results in no selection' do
before(:each) do
@item.stub!(:highlights_on => lambda{false})
end
it {@item.send(:selected_by_condition?).should be_false}
end
end
context ':highlights_on is :subpath' do
before(:each) do
@item.stub!(:url => '/resources')
@item.stub!(:highlights_on => :subpath)
end
context 'we are in a route beginning with this item path' do
before(:each) do
SimpleNavigation.stub!(:request_uri => '/resources/id')
end
it {@item.send(:selected_by_condition?).should be_true}
end
context 'we are in a route that has a similar name' do
before(:each) do
SimpleNavigation.stub!(:request_uri => '/resources_group/id')
end
it {@item.send(:selected_by_condition?).should be_false}
end
context 'we are in a route not beginning with this item path' do
before(:each) do
SimpleNavigation.stub!(:request_uri => '/another_resource/id')
end
it {@item.send(:selected_by_condition?).should be_false}
end
end
context ':highlights_on is not a regexp or a proc' do
before(:each) do
@item.stub!(:highlights_on => "not a regexp")
end
it "should raise an error" do
lambda {@item.send(:selected_by_condition?).should raise_error(ArgumentError)}
end
end
end
context ':highlights_on option is not set' do
before(:each) do
@item.stub!(:highlights_on => nil)
end
it "should check for autohighlighting" do
@item.should_receive(:auto_highlight?)
@item.send(:selected_by_condition?)
end
end
context 'auto_highlight is turned on' do
before(:each) do
@item.stub!(:auto_highlight? => true)
end
context 'root path matches' do
before(:each) do
@item.stub!(:root_path_match? => true)
end
it {@item.send(:selected_by_condition?).should be_true}
end
context 'root path does not match' do
before(:each) do
@item.stub!(:root_path_match? => false)
end
context 'current request url matches url' do
before(:each) do
@adapter.stub!(:current_page? => true)
end
it "should test with the item's url" do
@adapter.should_receive(:current_page?).with('url')
@item.send(:selected_by_condition?)
end
it "should remove anchors before testing the item's url" do
@item.stub!(:url => 'url#anchor')
@adapter.should_receive(:current_page?).with('url')
@item.send(:selected_by_condition?)
end
it "should not be queried when url is nil" do
@item.stub!(:url => nil)
@adapter.should_not_receive(:current_page?)
@item.send(:selected_by_condition?)
end
it {@item.send(:selected_by_condition?).should be_true}
end
context 'no match' do
before(:each) do
@adapter.stub!(:current_page? => false)
end
it {@item.send(:selected_by_condition?).should be_false}
end
end
end
context 'auto_highlight is turned off' do
before(:each) do
@item.stub!(:auto_highlight? => false)
end
it {@item.send(:selected_by_condition?).should be_false}
end
end
describe 'root_path_match?' do
it "should match if both url == /" do
@adapter.stub!(:request_path => '/')
@item.stub!(:url => '/')
@item.send(:root_path_match?).should be_true
end
it "should not match if item url is not /" do
@adapter.stub(:request_path => '/')
@item.stub!(:url => '/bla')
@item.send(:root_path_match?).should be_false
end
it "should not match if request url is not /" do
@adapter.stub(:request_path => '/bla')
@item.stub!(:url => '/')
@item.send(:root_path_match?).should be_false
end
it "should not match if urls do not match" do
@adapter.stub(:request_path => 'bla')
@item.stub!(:url => '/bli')
@item.send(:root_path_match?).should be_false
end
it "should not match if url is nil" do
@adapter.stub(:request_path => 'bla')
@item.stub!(:url => nil)
@item.send(:root_path_match?).should be_false
end
end
describe 'auto_highlight?' do
before(:each) do
@global = stub(:config)
SimpleNavigation.stub!(:config => @global)
end
context 'global auto_highlight on' do
before(:each) do
@global.stub!(:auto_highlight => true)
end
context 'container auto_highlight on' do
before(:each) do
@item_container.stub!(:auto_highlight => true)
end
it {@item.send(:auto_highlight?).should be_true}
end
context 'container auto_highlight off' do
before(:each) do
@item_container.stub!(:auto_highlight => false)
end
it {@item.send(:auto_highlight?).should be_false}
end
end
context 'global auto_highlight off' do
before(:each) do
@global.stub!(:auto_highlight => false)
end
context 'container auto_highlight on' do
before(:each) do
@item_container.stub!(:auto_highlight => true)
end
it {@item.send(:auto_highlight?).should be_false}
end
context 'container auto_highlight off' do
before(:each) do
@item_container.stub!(:auto_highlight => false)
end
it {@item.send(:auto_highlight?).should be_false}
end
end
end
describe 'autogenerated_item_id' do
context 'calls' do
before(:each) do
@id_generator = stub(:id_generator)
SimpleNavigation.config.stub!(:id_generator => @id_generator)
end
it "should call the configured generator with the key as param" do
@id_generator.should_receive(:call).with(:my_key)
@item.send(:autogenerated_item_id)
end
end
context 'default generator' do
it {@item.send(:autogenerated_item_id).should == 'my_key'}
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/core/item_container_spec.rb 0000644 0001756 0001756 00000037113 12202013367 027024 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::ItemContainer do
before(:each) do
@item_container = SimpleNavigation::ItemContainer.new
end
describe 'initialize' do
it "should set the renderer to the globally-configured renderer per default" do
SimpleNavigation::Configuration.instance.should_receive(:renderer)
@item_container = SimpleNavigation::ItemContainer.new
end
it "should have an empty items-array" do
@item_container = SimpleNavigation::ItemContainer.new
@item_container.items.should be_empty
end
end
describe 'items=' do
before(:each) do
@item = stub(:item)
@items = [@item]
@item_adapter = stub(:item_adapter).as_null_object
SimpleNavigation::ItemAdapter.stub(:new => @item_adapter)
@item_container.stub!(:should_add_item? => true)
end
it "should wrap each item in an ItemAdapter" do
SimpleNavigation::ItemAdapter.should_receive(:new)
@item_container.items = @items
end
context 'item should be added' do
before(:each) do
@item_container.stub!(:should_add_item? => true)
@simple_navigation_item = stub(:simple_navigation_item)
@item_adapter.stub!(:to_simple_navigation_item => @simple_navigation_item)
end
it "should convert the item to a SimpleNavigation::Item" do
@item_adapter.should_receive(:to_simple_navigation_item).with(@item_container)
@item_container.items = @items
end
it "should add the item to the items-collection" do
@item_container.items.should_receive(:<<).with(@simple_navigation_item)
@item_container.items = @items
end
end
context 'item should not be added' do
before(:each) do
@item_container.stub!(:should_add_item? => false)
end
it "should not convert the item to a SimpleNavigation::Item" do
@item_adapter.should_not_receive(:to_simple_navigation_item)
@item_container.items = @items
end
it "should not add the item to the items-collection" do
@item_container.items.should_not_receive(:<<)
@item_container.items = @items
end
end
end
describe 'selected?' do
before(:each) do
@item_1 = stub(:item, :selected? => false)
@item_2 = stub(:item, :selected? => false)
@item_container.instance_variable_set(:@items, [@item_1, @item_2])
end
it "should return nil if no item is selected" do
@item_container.should_not be_selected
end
it "should return true if one item is selected" do
@item_1.stub!(:selected? => true)
@item_container.should be_selected
end
end
describe 'selected_item' do
before(:each) do
SimpleNavigation.stub!(:current_navigation_for => :nav)
@item_container.stub!(:[] => nil)
@item_1 = stub(:item, :selected? => false)
@item_2 = stub(:item, :selected? => false)
@item_container.instance_variable_set(:@items, [@item_1, @item_2])
end
context 'navigation not explicitely set' do
context 'no item selected' do
it "should return nil" do
@item_container.selected_item.should be_nil
end
end
context 'one item selected' do
before(:each) do
@item_1.stub!(:selected? => true)
end
it "should return the selected item" do
@item_container.selected_item.should == @item_1
end
end
end
end
describe 'selected_sub_navigation?' do
context 'with an item selected' do
before(:each) do
@selected_item = stub(:selected_item)
@item_container.stub!(:selected_item => @selected_item)
end
context 'selected item has sub_navigation' do
before(:each) do
@sub_navigation = stub(:sub_navigation)
@selected_item.stub!(:sub_navigation => @sub_navigation)
end
it {@item_container.send(:selected_sub_navigation?).should be_true}
end
context 'selected item does not have sub_navigation' do
before(:each) do
@selected_item.stub!(:sub_navigation => nil)
end
it {@item_container.send(:selected_sub_navigation?).should be_false}
end
end
context 'without an item selected' do
before(:each) do
@item_container.stub!(:selected_item => nil)
end
it {@item_container.send(:selected_sub_navigation?).should be_false}
end
end
describe 'active_item_container_for' do
context "the desired level is the same as the container's" do
it {@item_container.active_item_container_for(1).should == @item_container}
end
context "the desired level is different than the container's" do
context 'with no selected subnavigation' do
before(:each) do
@item_container.stub!(:selected_sub_navigation? => false)
end
it {@item_container.active_item_container_for(2).should be_nil}
end
context 'with selected subnavigation' do
before(:each) do
@item_container.stub!(:selected_sub_navigation? => true)
@sub_nav = stub(:sub_nav)
@selected_item = stub(:selected_item)
@item_container.stub!(:selected_item => @selected_item)
@selected_item.stub!(:sub_navigation => @sub_nav)
end
it "should call recursively on the sub_navigation" do
@sub_nav.should_receive(:active_item_container_for).with(2)
@item_container.active_item_container_for(2)
end
end
end
end
describe 'active_leaf_container' do
context 'the current container has a selected subnavigation' do
before(:each) do
@item_container.stub!(:selected_sub_navigation? => true)
@sub_nav = stub(:sub_nav)
@selected_item = stub(:selected_item)
@item_container.stub!(:selected_item => @selected_item)
@selected_item.stub!(:sub_navigation => @sub_nav)
end
it "should call recursively on the sub_navigation" do
@sub_nav.should_receive(:active_leaf_container)
@item_container.active_leaf_container
end
end
context 'the current container is the leaf already' do
before(:each) do
@item_container.stub!(:selected_sub_navigation? => false)
end
it "should return itsself" do
@item_container.active_leaf_container.should == @item_container
end
end
end
describe 'item' do
context 'unconditional item' do
before(:each) do
@item_container.stub!(:should_add_item?).and_return(true)
@options = {}
end
context 'block given' do
before(:each) do
@sub_container = stub(:sub_container)
SimpleNavigation::ItemContainer.stub!(:new).and_return(@sub_container)
end
it "should should yield an new ItemContainer" do
@item_container.item('key', 'name', 'url', @options) do |container|
container.should == @sub_container
end
end
it "should create a new Navigation-Item with the given params and the specified block" do
SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil, &@proc)
@item_container.item('key', 'name', 'url', @options, &@proc)
end
it "should add the created item to the list of items" do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', 'url', @options) {}
end
end
context 'no block given' do
it "should create a new Navigation_item with the given params and nil as sub_navi" do
SimpleNavigation::Item.should_receive(:new).with(@item_container, 'key', 'name', 'url', @options, nil)
@item_container.item('key', 'name', 'url', @options)
end
it "should add the created item to the list of items" do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', 'url', @options)
end
end
end
context 'optional url and optional options' do
context 'item specifed without url or options' do
it 'should add the create item to the list of items' do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name')
end
end
context 'item specified with only a url' do
it 'should add the item to the list' do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', 'url')
end
end
context 'item specified with only options' do
context 'containing no conditions' do
it 'should add the created item to the list of items' do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', {:option => true})
end
end
context 'containing negative condition' do
it 'should not add the created item to the list of items' do
@item_container.items.should_not_receive(:<<)
@item_container.item('key', 'name', {:if => lambda{false}, :option => true})
end
end
context 'containing positive condition' do
it 'should add the created item to the list of items' do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', {:if => lambda{true}, :option => true})
end
end
end
context 'item specified with a url and options' do
context 'containing no conditions' do
it 'should add the created item to the list of items' do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', 'url', {:option => true})
end
end
context 'containing negative condition' do
it 'should not add the created item to the list of items' do
@item_container.items.should_not_receive(:<<)
@item_container.item('key', 'name', 'url', {:if => lambda{false}, :option => true})
end
end
context 'containing positive condition' do
it 'should add the created item to the list of items' do
@item_container.items.should_receive(:<<)
@item_container.item('key', 'name', 'url', {:if => lambda{true}, :option => true})
end
end
end
end
context 'conditions given for item' do
context '"if" given' do
before(:each) do
@options = {:if => Proc.new {@condition}}
end
it "should remove if from options" do
@item_container.item('key', 'name', 'url', @options)
@options[:if].should be_nil
end
context 'if evals to true' do
before(:each) do
@condition = true
end
it "should create a new Navigation-Item" do
SimpleNavigation::Item.should_receive(:new)
@item_container.item('key', 'name', 'url', @options)
end
end
context 'if evals to false' do
before(:each) do
@condition = false
end
it "should not create a new Navigation-Item" do
SimpleNavigation::Item.should_not_receive(:new)
@item_container.item('key', 'name', 'url', @options)
end
end
context 'if is not a proc or method' do
it "should raise an error" do
lambda {@item_container.item('key', 'name', 'url', {:if => 'text'})}.should raise_error
end
end
context '"unless" given' do
before(:each) do
@options = {:unless => Proc.new {@condition}}
end
it "should remove unless from options" do
@item_container.item('key', 'name', 'url', @options)
@options[:unless].should be_nil
end
context 'unless evals to false' do
before(:each) do
@condition = false
end
it "should create a new Navigation-Item" do
SimpleNavigation::Item.should_receive(:new)
@item_container.item('key', 'name', 'url', @options)
end
end
context 'unless evals to true' do
before(:each) do
@condition = true
end
it "should not create a new Navigation-Item" do
SimpleNavigation::Item.should_not_receive(:new)
@item_container.item('key', 'name', 'url', @options)
end
end
end
end
end
end
describe '[]' do
before(:each) do
@item_container.item(:first, 'first', 'bla')
@item_container.item(:second, 'second', 'bla')
@item_container.item(:third, 'third', 'bla')
end
it "should return the item with the specified navi_key" do
@item_container[:second].name.should == 'second'
end
it "should return nil if no item exists for the specified navi_key" do
@item_container[:invalid].should be_nil
end
end
describe 'render' do
before(:each) do
@renderer_instance = stub(:renderer).as_null_object
@renderer_class = stub(:renderer_class, :new => @renderer_instance)
end
context 'renderer specified as option' do
context 'renderer-class specified' do
it "should instantiate the passed renderer_class with the options" do
@renderer_class.should_receive(:new).with(:renderer => @renderer_class)
end
it "should call render on the renderer and pass self" do
@renderer_instance.should_receive(:render).with(@item_container)
end
after(:each) do
@item_container.render(:renderer => @renderer_class)
end
end
context 'renderer-symbol specified' do
before(:each) do
SimpleNavigation.registered_renderers = {:my_renderer => @renderer_class}
end
it "should instantiate the passed renderer_class with the options" do
@renderer_class.should_receive(:new).with(:renderer => :my_renderer)
end
it "should call render on the renderer and pass self" do
@renderer_instance.should_receive(:render).with(@item_container)
end
after(:each) do
@item_container.render(:renderer => :my_renderer)
end
end
end
context 'no renderer specified' do
before(:each) do
@item_container.stub!(:renderer => @renderer_class)
@options = {}
end
it "should instantiate the container's renderer with the options" do
@renderer_class.should_receive(:new).with(@options)
end
it "should call render on the renderer and pass self" do
@renderer_instance.should_receive(:render).with(@item_container)
end
after(:each) do
@item_container.render(@options)
end
end
end
describe 'level_for_item' do
before(:each) do
@item_container.item(:p1, 'p1', 'p1')
@item_container.item(:p2, 'p2', 'p2') do |p2|
p2.item(:s1, 's1', 's1')
p2.item(:s2, 's2', 's2') do |s2|
s2.item(:ss1, 'ss1', 'ss1')
s2.item(:ss2, 'ss2', 'ss2')
end
p2.item(:s3, 's3', 's3')
end
@item_container.item(:p3, 'p3', 'p3')
end
it {@item_container.level_for_item(:p1).should == 1}
it {@item_container.level_for_item(:p3).should == 1}
it {@item_container.level_for_item(:s1).should == 2}
it {@item_container.level_for_item(:ss1).should == 3}
it {@item_container.level_for_item(:x).should be_nil}
end
describe 'empty?' do
it "should be empty if there are no items" do
@item_container.instance_variable_set(:@items, [])
@item_container.should be_empty
end
it "should not be empty if there are some items" do
@item_container.instance_variable_set(:@items, [stub(:item)])
@item_container.should_not be_empty
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/core/configuration_spec.rb 0000644 0001756 0001756 00000010514 12202013367 026667 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Configuration do
before(:each) do
@config = SimpleNavigation::Configuration.instance
end
describe 'self.run' do
it "should yield the singleton Configuration object" do
SimpleNavigation::Configuration.run do |c|
c.should == @config
end
end
end
describe 'self.eval_config' do
before(:each) do
@context = mock(:context)
@context.stub!(:instance_eval)
SimpleNavigation.stub!(:context_for_eval => @context)
@config_files = {:default => 'default', :my_context => 'my_context'}
SimpleNavigation.stub!(:config_files).and_return(@config_files)
end
context "with default navigation context" do
it "should instance_eval the default config_file-string inside the context" do
@context.should_receive(:instance_eval).with('default')
SimpleNavigation::Configuration.eval_config
end
end
context 'with non default navigation context' do
it "should instance_eval the specified config_file-string inside the context" do
@context.should_receive(:instance_eval).with('my_context')
SimpleNavigation::Configuration.eval_config(:my_context)
end
end
end
describe 'initialize' do
it "should set the List-Renderer as default upon initialize" do
@config.renderer.should == SimpleNavigation::Renderer::List
end
it "should set the selected_class to 'selected' as default" do
@config.selected_class.should == 'selected'
end
it "should set the active_leaf_class to 'simple-navigation-active-leaf' as default" do
@config.active_leaf_class.should == 'simple-navigation-active-leaf'
end
it "should set autogenerate_item_ids to true as default" do
@config.autogenerate_item_ids.should be_true
end
it "should set auto_highlight to true as default" do
@config.auto_highlight.should be_true
end
it "should set the id_generator" do
@config.id_generator.should_not be_nil
end
it "should set the name_generator" do
@config.name_generator.should_not be_nil
end
end
describe 'items' do
before(:each) do
@container = stub(:items_container)
SimpleNavigation::ItemContainer.stub!(:new).and_return(@container)
end
context 'block given' do
context 'items_provider specified' do
it {lambda {@config.items(stub(:provider)) {}}.should raise_error}
end
context 'no items_provider specified' do
it "should should yield an new ItemContainer" do
@config.items do |container|
container.should == @container
end
end
it "should assign the ItemContainer to an instance-var" do
@config.items {}
@config.primary_navigation.should == @container
end
it "should not set the items on the container" do
@container.should_not_receive(:items=)
@config.items {}
end
end
end
context 'no block given' do
context 'items_provider specified' do
before(:each) do
@external_provider = stub(:external_provider)
@items = stub(:items)
@items_provider = stub(:items_provider, :items => @items)
SimpleNavigation::ItemsProvider.stub!(:new => @items_provider)
@container.stub!(:items=)
end
it "should create an new Provider object for the specified provider" do
SimpleNavigation::ItemsProvider.should_receive(:new).with(@external_provider)
@config.items(@external_provider)
end
it "should call items on the provider object" do
@items_provider.should_receive(:items)
@config.items(@external_provider)
end
it "should set the items on the container" do
@container.should_receive(:items=).with(@items)
@config.items(@external_provider)
end
end
context 'items_provider not specified' do
it {lambda {@config.items}.should raise_error}
end
end
end
describe 'loaded?' do
it "should return true if primary_nav is set" do
@config.instance_variable_set(:@primary_navigation, :bla)
@config.should be_loaded
end
it "should return false if no primary_nav is set" do
@config.instance_variable_set(:@primary_navigation, nil)
@config.should_not be_loaded
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/ 0000755 0001756 0001756 00000000000 12202013367 023505 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/ 0000755 0001756 0001756 00000000000 12202013367 025313 5 ustar synrg synrg simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/text_spec.rb 0000644 0001756 0001756 00000002371 12202013367 027641 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Renderer::Text do
describe 'render' do
def render(current_nav=nil, options={:level => :all})
primary_navigation = primary_container
select_item(current_nav)
setup_renderer_for SimpleNavigation::Renderer::Text, :rails, options
@renderer.render(primary_navigation)
end
context 'regarding result' do
it "should render the selected page" do
render(:invoices).should == "invoices"
end
context 'nested sub_navigation' do
it "should add an entry for each selected item" do
render(:subnav1).should == "invoices subnav1"
end
end
context 'with a custom seperator specified' do
it "should separate the items with the separator" do
render(:subnav1, :join_with => " | ").should == "invoices | subnav1"
end
end
context 'custom name generator is set' do
before(:each) do
SimpleNavigation.config.stub!(:name_generator => Proc.new {|name| "
name"})
end
it "should not apply the name generator (since it is text only)" do
render(:subnav1, :join_with => " | ").should == "invoices | subnav1"
end
end
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/list_spec.rb 0000644 0001756 0001756 00000022366 12202013367 027636 0 ustar synrg synrg require 'spec_helper'
require 'html/document' unless defined? HTML::Document
describe SimpleNavigation::Renderer::List do
describe 'render' do
def render(current_nav=nil, options={:level => :all})
primary_navigation = primary_container
select_item(current_nav) if current_nav
setup_renderer_for SimpleNavigation::Renderer::List, :rails, options
HTML::Document.new(@renderer.render(primary_navigation)).root
end
context 'regarding result' do
it "should render a ul-tag around the items" do
HTML::Selector.new('ul').select(render).should have(1).entries
end
it "the rendered ul-tag should have the specified dom_id" do
HTML::Selector.new('ul#nav_dom_id').select(render).should have(1).entries
end
it "the rendered ul-tag should have the specified class" do
HTML::Selector.new('ul.nav_dom_class').select(render).should have(1).entries
end
it "should render a li tag for each item" do
HTML::Selector.new('li').select(render).should have(4).entries
end
it "should render an a-tag inside each li-tag (for items with links)" do
HTML::Selector.new('li a').select(render).should have(3).entries
end
it "should render a span-tag inside each li-tag (for items without links)" do
HTML::Selector.new('li span').select(render).should have(1).entries
end
context 'concerning item names' do
context 'with a custom name generator defined' do
before(:each) do
SimpleNavigation.config.stub!(:name_generator => Proc.new {|name| "
name"})
end
it "should apply the name generator" do
HTML::Selector.new('li a span').select(render).should have(3).entries
end
end
context 'no customer generator defined' do
before(:each) do
SimpleNavigation.config.stub!(:name_generator => Proc.new {|name| "name"})
end
it "should apply the name generator" do
HTML::Selector.new('li a span').select(render).should have(0).entries
end
end
end
context 'concerning html attributes' do
context 'default case (no options defined for link tag)' do
it "should pass the specified html_options to the li element" do
HTML::Selector.new('li[style=float:right]').select(render).should have(1).entries
end
it "should give the li the id specified in the html_options" do
HTML::Selector.new('li#my_id').select(render).should have(1).entries
end
it "should give the li the default id (stringified key) if no id is specified in the html_options" do
HTML::Selector.new('ul li#invoices').select(render).should have(1).entries
end
it "should not apply the the default id where there is an id specified in the html_options" do
HTML::Selector.new('ul li#users').select(render).should be_empty
end
end
context 'with attributes defined for the link tag as well' do
it "should add the link attributes to the link" do
HTML::Selector.new('a[style=float:left]').select(render).should have(1).entries
end
it "should add the li attributes to the li element" do
HTML::Selector.new('li[style=float:right]').select(render).should have(1).entries
end
it "should give the li the default id (stringified key) if no id is specified in the html_options for the li-element" do
HTML::Selector.new('ul li#invoices').select(render).should have(1).entries
end
it "should not apply the the default id where there is an id specified in the html_options for th li-element" do
HTML::Selector.new('ul li#users').select(render).should be_empty
end
end
end
context 'with current_navigation set' do
it "should mark the matching li-item as selected (with the css_class specified in configuration)" do
HTML::Selector.new('li.selected').select(render(:invoices)).should have(1).entries
end
it "should also mark the links inside the selected li's as selected" do
HTML::Selector.new('li.selected a.selected').select(render(:invoices)).should have(1).entries
end
end
context 'without current_navigation set' do
it "should not mark any of the items as selected" do
HTML::Selector.new('li.selected').select(render).should be_empty
end
it "should not mark any links as selected" do
HTML::Selector.new('a.selected').select(render).should be_empty
end
end
context 'nested sub_navigation' do
it "should nest the current_primary's subnavigation inside the selected li-element" do
HTML::Selector.new('li.selected ul li').select(render(:invoices)).should have(2).entries
end
it "should be possible to identify sub items using an html selector (using ids)" do
HTML::Selector.new('#invoices #subnav1').select(render(:invoices)).should have(1).entries
end
context 'expand_all => false' do
it "should not render the invoices submenu if the user-primary is active" do
HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => false)).should be_empty
HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => false)).should be_empty
end
end
context 'expand_all => true' do
it "should render the invoices submenu even if the user-primary is active" do
HTML::Selector.new('#invoices #subnav1').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
HTML::Selector.new('#invoices #subnav2').select(render(:users, :level => :all, :expand_all => true)).should have(1).entry
end
end
end
context 'skip_if_empty' do
def render_container(options={})
setup_renderer_for SimpleNavigation::Renderer::List, :rails, options
HTML::Document.new(@renderer.render(@container)).root
end
context 'container is empty' do
before(:each) do
@container = SimpleNavigation::ItemContainer.new(0)
end
context 'skip_if_empty is true' do
it "should not render a ul tag for the empty container" do
HTML::Selector.new('ul').select(render_container(:skip_if_empty => true)).should be_empty
end
end
context 'skip_if_empty is false' do
it "should render a ul tag for the empty container" do
HTML::Selector.new('ul').select(render_container(:skip_if_empty => false)).should have(1).entry
end
end
end
context 'container is not empty' do
before(:each) do
@container = primary_container
end
context 'skip_if_empty is true' do
it "should render a ul tag for the container" do
HTML::Selector.new('ul').select(render_container(:skip_if_empty => true)).should have(1).entry
end
end
context 'skip_if_empty is false' do
it "should render a ul tag for the container" do
HTML::Selector.new('ul').select(render_container(:skip_if_empty => false)).should have(1).entry
end
end
end
end
end
describe 'link_options_for' do
before(:each) do
setup_renderer_for SimpleNavigation::Renderer::List, :rails, {}
end
context 'no link options specified' do
context 'method specified' do
context 'item selected' do
before(:each) do
@item = stub(:item, :method => :delete, :selected_class => 'selected', :html_options => {})
end
it {@renderer.send(:link_options_for, @item).should == {:method => :delete, :class => 'selected'}}
end
context 'item not selected' do
before(:each) do
@item = stub(:item, :method => :delete, :selected_class => nil, :html_options => {})
end
it {@renderer.send(:link_options_for, @item).should == {:method => :delete}}
end
end
context 'method not specified' do
context 'item selected' do
before(:each) do
@item = stub(:item, :method => nil, :selected_class => 'selected', :html_options => {})
end
it {@renderer.send(:link_options_for, @item).should == {:class => 'selected'}}
end
context 'item not selected' do
before(:each) do
@item = stub(:item, :method => nil, :selected_class => nil, :html_options => {})
end
it {@renderer.send(:link_options_for, @item).should == {}}
end
end
end
context 'link options specified' do
before(:each) do
@item = stub(:item, :method => :delete, :selected_class => 'selected', :html_options => {:link => {:class => 'link_class', :style => 'float:left'}})
end
it {@renderer.send(:link_options_for, @item).should == {:method => :delete, :class => 'link_class selected', :style => 'float:left'}}
end
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/links_spec.rb 0000644 0001756 0001756 00000005001 12202013367 027766 0 ustar synrg synrg require 'spec_helper'
require 'html/document' unless defined? HTML::Document
describe SimpleNavigation::Renderer::Links do
describe 'render' do
def render(current_nav=nil, options={:level => :all})
primary_navigation = primary_container
select_item(current_nav) if current_nav
setup_renderer_for SimpleNavigation::Renderer::Links, :rails, options
HTML::Document.new(@renderer.render(primary_navigation)).root
end
context 'regarding result' do
it "should render a div-tag around the items" do
HTML::Selector.new('div').select(render).should have(1).entries
end
it "the rendered div-tag should have the specified dom_id" do
HTML::Selector.new('div#nav_dom_id').select(render).should have(1).entries
end
it "the rendered div-tag should have the specified class" do
HTML::Selector.new('div.nav_dom_class').select(render).should have(1).entries
end
it "should render an a-tag for each item" do
HTML::Selector.new('a').select(render).should have(3).entries
end
it "should pass the specified html_options to the a element" do
HTML::Selector.new('a[style=float:right]').select(render).should have(1).entries
end
it "should give the a-tag the id specified in the html_options" do
HTML::Selector.new('a#my_id').select(render).should have(1).entries
end
it "should give the a-tag the default id (stringified key) if no id is specified in the html_options" do
HTML::Selector.new('a#invoices').select(render).should have(1).entries
end
it "should not apply the the default id where there is an id specified in the html_options" do
HTML::Selector.new('a#users').select(render).should be_empty
end
context 'with current_navigation set' do
it "should mark the matching a-item as selected (with the css_class specified in configuration)" do
HTML::Selector.new('a.selected').select(render(:invoices)).should have(1).entries
end
end
context 'without current_navigation set' do
it "should not mark any of the items as selected" do
HTML::Selector.new('a.selected').select(render).should be_empty
end
end
context 'with a custom seperator specified' do
it "should separate the items with the separator" do
HTML::Selector.new('div').select_first(render(:subnav1, :join_with => " | ")).to_s.split(" | ").should have(4).entries
end
end
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/json_spec.rb 0000644 0001756 0001756 00000003020 12202013367 027616 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Renderer::Json do
describe 'render' do
def render(current_nav=nil, options={})
primary_navigation = primary_container
select_item(current_nav)
setup_renderer_for SimpleNavigation::Renderer::Json, :rails, options
@renderer.render(primary_navigation)
end
def prerendered_menu
'[{"name":"users","url":"first_url","selected":false,"items":null},{"name":"invoices","url":"second_url","selected":true,"items":[{"name":"subnav1","url":"subnav1_url","selected":false,"items":null},{"name":"subnav2","url":"subnav2_url","selected":false,"items":null}]},{"name":"accounts","url":"third_url","selected":false,"items":null},{"name":"miscellany","url":null,"selected":false,"items":null}]'
end
context 'regarding result' do
it "should return a string" do
render(:invoices).class.should == String
end
it "should render the selected page" do
json = parse_json(render(:invoices))
found = json.any? do |item|
item["name"] == "invoices" and item["selected"]
end
found.should == true
end
end
context 'regarding hash result' do
it "should return a hash" do
render(:invoices, :as_hash => true).class.should == Array
end
it "should render the selected page" do
found = render(:invoices, :as_hash => true).any? do |item|
item[:name] == "invoices" and item[:selected]
end
found.should == true
end
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/base_spec.rb 0000644 0001756 0001756 00000014102 12202013367 027562 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Renderer::Base do
before(:each) do
@options = stub(:options).as_null_object
@adapter = stub(:adapter)
SimpleNavigation.stub!(:adapter => @adapter)
@base_renderer = SimpleNavigation::Renderer::Base.new(@options)
end
describe 'delegated methods' do
it {@base_renderer.should respond_to(:link_to)}
it {@base_renderer.should respond_to(:content_tag)}
end
describe 'initialize' do
it {@base_renderer.adapter.should == @adapter}
it {@base_renderer.options.should == @options}
end
describe 'render' do
it "be subclass responsability" do
lambda {@base_renderer.render(:container)}.should raise_error('subclass responsibility')
end
end
describe 'expand_all?' do
context 'option is set' do
context 'expand_all is true' do
before(:each) do
@base_renderer.stub!(:options => {:expand_all => true})
end
it {@base_renderer.expand_all?.should be_true}
end
context 'expand_all is false' do
before(:each) do
@base_renderer.stub!(:options => {:expand_all => false})
end
it {@base_renderer.expand_all?.should be_false}
end
end
context 'option is not set' do
before(:each) do
@base_renderer.stub!(:options => {})
end
it {@base_renderer.expand_all?.should be_false}
end
end
describe 'skip_if_empty?' do
context 'option is set' do
context 'skip_if_empty is true' do
before(:each) do
@base_renderer.stub!(:options => {:skip_if_empty => true})
end
it {@base_renderer.skip_if_empty?.should be_true}
end
context 'skip_if_empty is false' do
before(:each) do
@base_renderer.stub!(:options => {:skip_if_empty => false})
end
it {@base_renderer.skip_if_empty?.should be_false}
end
end
context 'option is not set' do
before(:each) do
@base_renderer.stub!(:options => {})
end
it {@base_renderer.skip_if_empty?.should be_false}
end
end
describe 'level' do
context 'options[level] is set' do
before(:each) do
@base_renderer.stub!(:options => {:level => 1})
end
it {@base_renderer.level.should == 1}
end
context 'options[level] is not set' do
before(:each) do
@base_renderer.stub!(:options => {})
end
it {@base_renderer.level.should == :all}
end
end
describe 'consider_sub_navigation?' do
before(:each) do
@item = stub(:item)
end
context 'item has no subnavigation' do
before(:each) do
@item.stub!(:sub_navigation => nil)
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_false}
end
context 'item has subnavigation' do
before(:each) do
@sub_navigation = stub(:sub_navigation)
@item.stub!(:sub_navigation => @sub_navigation)
end
context 'level is something unknown' do
before(:each) do
@base_renderer.stub!(:level => 'unknown')
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_false}
end
context 'level is :all' do
before(:each) do
@base_renderer.stub!(:level => :all)
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_true}
end
context 'level is an Integer' do
before(:each) do
@base_renderer.stub!(:level => 2)
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_false}
end
context 'level is a Range' do
before(:each) do
@base_renderer.stub!(:level => 2..3)
end
context 'subnavs level > range.max' do
before(:each) do
@sub_navigation.stub!(:level => 4)
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_false}
end
context 'subnavs level = range.max' do
before(:each) do
@sub_navigation.stub!(:level => 3)
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_true}
end
context 'subnavs level < range.max' do
before(:each) do
@sub_navigation.stub!(:level => 2)
end
it {@base_renderer.send(:consider_sub_navigation?, @item).should be_true}
end
end
end
end
describe 'include_sub_navigation?' do
before(:each) do
@item = stub(:item)
end
context 'consider_sub_navigation? is true' do
before(:each) do
@base_renderer.stub!(:consider_sub_navigation? => true)
end
context 'expand_sub_navigation? is true' do
before(:each) do
@base_renderer.stub!(:expand_sub_navigation? => true)
end
it {@base_renderer.include_sub_navigation?(@item).should be_true}
end
context 'expand_sub_navigation? is false' do
before(:each) do
@base_renderer.stub!(:expand_sub_navigation? => false)
end
it {@base_renderer.include_sub_navigation?(@item).should be_false}
end
end
context 'consider_sub_navigation is false' do
before(:each) do
@base_renderer.stub!(:consider_sub_navigation? => false)
end
context 'expand_sub_navigation? is true' do
before(:each) do
@base_renderer.stub!(:expand_sub_navigation? => true)
end
it {@base_renderer.include_sub_navigation?(@item).should be_false}
end
context 'expand_sub_navigation? is false' do
before(:each) do
@base_renderer.stub!(:expand_sub_navigation? => false)
end
it {@base_renderer.include_sub_navigation?(@item).should be_false}
end
end
end
describe 'render_sub_navigation_for' do
before(:each) do
@sub_navigation = stub(:sub_navigation)
@item = stub(:item, :sub_navigation => @sub_navigation)
end
it "should call render on the sub_navigation (passing the options)" do
@sub_navigation.should_receive(:render).with(@options)
@base_renderer.render_sub_navigation_for(@item)
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/renderer/breadcrumbs_spec.rb 0000644 0001756 0001756 00000007017 12202013367 031150 0 ustar synrg synrg require 'spec_helper'
require 'html/document'# unless defined? HTML::Document
describe SimpleNavigation::Renderer::Breadcrumbs do
describe 'render' do
def render(current_nav=nil, options={:level => :all})
primary_navigation = primary_container
select_item(current_nav) if current_nav
setup_renderer_for SimpleNavigation::Renderer::Breadcrumbs, :rails, options
HTML::Document.new(@renderer.render(primary_navigation)).root
end
context 'regarding result' do
it "should render a div-tag around the items" do
HTML::Selector.new('div').select(render).should have(1).entries
end
it "the rendered div-tag should have the specified dom_id" do
HTML::Selector.new('div#nav_dom_id').select(render).should have(1).entries
end
it "the rendered div-tag should have the specified class" do
HTML::Selector.new('div.nav_dom_class').select(render).should have(1).entries
end
context 'without current_navigation set' do
it "should not render any a-tag in the div-tag" do
HTML::Selector.new('div a').select(render).should have(0).entries
end
end
context 'with current_navigation set' do
before(:each) do
@selection = HTML::Selector.new('div a').select(render(:invoices))
end
it "should render the selected a tags" do
@selection.should have(1).entries
end
it "should not render class or id" do
@selection.each do |tag|
raise unless tag.name == "a"
tag["id"].should be_nil
tag["class"].should be_nil
end
end
context 'with allow_classes_and_ids option' do
before(:each) do
@selection = HTML::Selector.new('div a').select(render(:users, :level => :all, :allow_classes_and_ids => true))
end
it "should render class and id" do
@selection.each do |tag|
raise unless tag.name == "a"
tag["id"].should_not be_nil
tag["class"].should_not be_nil
end
end
end
context 'with prefix option' do
it 'should render prefix before breadcrumbs' do
selection = HTML::Selector.new('div').select(render(:subnav1, :level => :all, :prefix => 'You are here: '))
raise unless selection.count == 1
tag = selection.first
tag.to_s.should =~ /^\
You are here\: /
end
it 'should not render prefix if there is no available breadcrumb' do
allow_message_expectations_on_nil
selection = HTML::Selector.new('div').select(render('', :prefix => 'You are here: '))
tag = selection.first
tag.to_s.should =~ /^\\<\/div\>/
end
end
context 'with static_leaf option' do
before(:each) do
@selection = HTML::Selector.new('div *').select(render(:subnav1, :level => :all, :static_leaf => true))
end
it "should render link for non-leaes" do
@selection[0..-2].each do |tag|
tag.name.should == 'a'
end
end
it "should not render link for leaf" do
@selection.last.name.should == 'span'
end
end
end
context 'nested sub_navigation' do
it "should add an a tag for each selected item" do
HTML::Selector.new('div a').select(render(:subnav1)).should have(2).entries
end
end
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rendering/helpers_spec.rb 0000644 0001756 0001756 00000024016 12202013367 026511 0 ustar synrg synrg require 'spec_helper'
describe SimpleNavigation::Helpers do
class ControllerMock
include SimpleNavigation::Helpers
end
def blackbox_setup()
@controller = ControllerMock.new
SimpleNavigation.stub!(:load_config)
SimpleNavigation::Configuration.stub!(:eval_config)
setup_adapter_for :rails
@primary_container, @subnav_container = containers
@subnav1_item = sub_item(:subnav1)
@invoices_item = primary_item(:invoices)
SimpleNavigation.stub!(:primary_navigation => @primary_container)
end
def whitebox_setup
@controller = ControllerMock.new
SimpleNavigation.stub!(:load_config)
SimpleNavigation::Configuration.stub!(:eval_config)
@primary_navigation = stub(:primary_navigation).as_null_object
SimpleNavigation.stub!(:primary_navigation).and_return(@primary_navigation)
SimpleNavigation.stub!(:config_file? => true)
end
describe 'active_navigation_item_name' do
before(:each) do
blackbox_setup
end
context 'active item_container for desired level exists' do
context 'container has selected item' do
before(:each) do
select_item(:subnav1)
end
it {@controller.active_navigation_item_name(:level => 2).should == 'subnav1'}
it {@controller.active_navigation_item_name.should == 'subnav1'}
it {@controller.active_navigation_item_name(:level => 1).should == 'invoices'}
it {@controller.active_navigation_item_name(:level => :all).should == 'subnav1'}
end
context 'container does not have selected item' do
it {@controller.active_navigation_item_name.should == ''}
end
context 'custom name generator set' do
before(:each) do
select_item(:subnav1)
SimpleNavigation.config.stub!(:name_generator => Proc.new {|name| "name"})
end
it "should not apply the generator" do
@controller.active_navigation_item_name(:level => 1).should == 'invoices'
end
end
end
context 'no active item_container for desired level' do
it {@controller.active_navigation_item_name(:level => 5).should == ''}
end
end
describe 'active_navigation_item_key' do
before(:each) do
blackbox_setup
end
context 'active item_container for desired level exists' do
context 'container has selected item' do
before(:each) do
select_item(:subnav1)
end
it {@controller.active_navigation_item_key(:level => 2).should == :subnav1}
it {@controller.active_navigation_item_key.should == :subnav1}
it {@controller.active_navigation_item_key(:level => 1).should == :invoices}
it {@controller.active_navigation_item_key(:level => :all).should == :subnav1}
end
context 'container does not have selected item' do
it {@controller.active_navigation_item_key.should == nil}
end
end
context 'no active item_container for desired level' do
it {@controller.active_navigation_item_key(:level => 5).should == nil}
end
end
describe 'active_navigation_item' do
before(:each) do
blackbox_setup
end
context 'active item_container for desired level exists' do
context 'container has selected item' do
before(:each) do
select_item(:subnav1)
end
it {@controller.active_navigation_item(:level => 2).should eq(@subnav1_item)}
it {@controller.active_navigation_item.should eq(@subnav1_item)}
it {@controller.active_navigation_item(:level => 1).should eq(@invoices_item)}
it {@controller.active_navigation_item(:level => :all).should eq(@subnav1_item)}
end
context 'container does not have selected item' do
context 'return value defaults to nil' do
it {@controller.active_navigation_item.should == nil}
end
context 'return value reflects passed in value' do
it {@controller.active_navigation_item({},'none').should == 'none'}
it {@controller.active_navigation_item({},@invoices_item).should eq(@invoices_item)}
end
end
end
context 'no active item_container for desired level' do
it {@controller.active_navigation_item(:level => 5).should == nil}
end
end
describe 'active_navigation_item_container' do
before(:each) do
blackbox_setup
end
context 'active item_container for desired level exists' do
before(:each) do
select_item(:subnav1)
end
it {@controller.active_navigation_item_container(:level => 2).should == @subnav_container}
it {@controller.active_navigation_item_container.should == @primary_container}
it {@controller.active_navigation_item_container(:level => 1).should == @primary_container}
it {@controller.active_navigation_item_container(:level => :all).should == @primary_container}
end
context 'no active item_container for desired level' do
it {@controller.active_navigation_item_container(:level => 5).should == nil}
end
end
describe 'render_navigation' do
before(:each) do
whitebox_setup
end
describe 'regarding loading of the config-file' do
context 'no options specified' do
it "should load the config-file for the default context" do
SimpleNavigation.should_receive(:load_config).with(:default)
@controller.render_navigation
end
end
context 'with options specified' do
it "should load the config-file for the specified context" do
SimpleNavigation.should_receive(:load_config).with(:my_context)
@controller.render_navigation(:context => :my_context)
end
end
end
it "should eval the config on every request" do
SimpleNavigation::Configuration.should_receive(:eval_config).with(:default)
@controller.render_navigation
end
describe 'regarding setting of items' do
context 'not items specified in options' do
it "should not set the items directly" do
SimpleNavigation.config.should_not_receive(:items)
@controller.render_navigation
end
end
context 'items specified in options' do
before(:each) do
@items = stub(:items)
end
it "should set the items directly" do
SimpleNavigation.config.should_receive(:items).with(@items)
@controller.render_navigation(:items => @items)
end
end
context 'block given' do
it 'should use block' do
block_executed = 0
expect do
@controller.render_navigation do |menu|
menu.class.should == SimpleNavigation::ItemContainer
block_executed += 1
end
end.to change{block_executed}.by(1)
end
end
end
describe 'no primary navigation defined' do
before(:each) do
SimpleNavigation.stub!(:primary_navigation => nil)
end
it {lambda {@controller.render_navigation}.should raise_error}
end
context 'rendering of the item_container' do
before(:each) do
@active_item_container = stub(:item_container).as_null_object
SimpleNavigation.stub!(:active_item_container_for => @active_item_container)
end
it "should lookup the active_item_container based on the level" do
SimpleNavigation.should_receive(:active_item_container_for).with(:all)
@controller.render_navigation
end
context 'active_item_container is nil' do
before(:each) do
SimpleNavigation.stub!(:active_item_container_for => nil)
end
it "should not call render" do
@active_item_container.should_not_receive(:render)
@controller.render_navigation
end
end
context 'active_item_container is not nil' do
it "should call render on the container" do
@active_item_container.should_receive(:render)
@controller.render_navigation
end
end
end
context 'primary' do
it "should call render on the primary_navigation (specifying level through options)" do
@primary_navigation.should_receive(:render).with(:level => 1)
@controller.render_navigation(:level => 1)
end
end
context 'secondary' do
context 'with current_primary_navigation set' do
before(:each) do
@selected_item_container = stub(:selected_container).as_null_object
SimpleNavigation.stub!(:active_item_container_for => @selected_item_container)
end
it "should find the selected sub_navigation for the specified level" do
SimpleNavigation.should_receive(:active_item_container_for).with(2)
@controller.render_navigation(:level => 2)
end
it "should find the selected sub_navigation for the specified level" do
SimpleNavigation.should_receive(:active_item_container_for).with(1)
@controller.render_navigation(:level => 1)
end
it "should call render on the active item_container" do
@selected_item_container.should_receive(:render).with(:level => 2)
@controller.render_navigation(:level => 2)
end
end
context 'without an active item_container set' do
before(:each) do
SimpleNavigation.stub!(:active_item_container_for => nil)
end
it "should not raise an error" do
lambda {@controller.render_navigation(:level => 2)}.should_not raise_error
end
end
end
context 'unknown level' do
it "should raise an error" do
lambda {@controller.render_navigation(:level => :unknown)}.should raise_error(ArgumentError)
end
end
end
describe "should treat :level and :levels options the same" do
before(:each) do
whitebox_setup
@selected_item_container = stub(:selected_container).as_null_object
SimpleNavigation.stub!(:active_item_container_for => @selected_item_container)
end
it "should pass a valid levels options as level" do
@selected_item_container.should_receive(:render).with(:level => 2)
@controller.render_navigation(:levels => 2)
end
end
end
simple-navigation-3.11.0/spec/lib/simple_navigation/rails_controller_methods_spec.rb 0000644 0001756 0001756 00000020677 12202013367 030203 0 ustar synrg synrg require 'spec_helper'
describe 'explicit navigation in rails' do
require 'simple_navigation/rails_controller_methods'
it 'should have enhanced the ActionController after loading the extensions' do
ActionController::Base.instance_methods.map {|m| m.to_s}.should include('current_navigation')
end
describe SimpleNavigation::ControllerMethods do
def stub_loading_config
SimpleNavigation::Configuration.stub!(:load)
end
before(:each) do
stub_loading_config
class TestController
class << self
def helper_method(*args)
@helper_methods = args
end
def before_filter(*args)
@before_filters = args
end
end
end
TestController.send(:include, SimpleNavigation::ControllerMethods)
@controller = TestController.new
end
describe 'when being included' do
it "should extend the ClassMethods" do
@controller.class.should respond_to(:navigation)
end
it "should include the InstanceMethods" do
@controller.should respond_to(:current_navigation)
end
end
describe 'class_methods' do
describe 'navigation' do
def call_navigation(key1, key2=nil)
@controller.class_eval do
navigation key1, key2
end
end
it "should not have an instance-method 'sn_set_navigation' if navigation-method has not been called" do
@controller.respond_to?(:sn_set_navigation).should be_false
end
it 'should create an instance-method "sn_set_navigation" when being called' do
call_navigation(:key)
@controller.respond_to?(:sn_set_navigation, true).should be_true
end
it "the created method should not be public" do
call_navigation(:key)
@controller.public_methods.map(&:to_sym).should_not include(:sn_set_navigation)
end
it "the created method should be protected" do
call_navigation(:key)
@controller.protected_methods.map(&:to_sym).should include(:sn_set_navigation)
end
it 'the created method should call current_navigation with the specified keys' do
call_navigation(:primary, :secondary)
@controller.should_receive(:current_navigation).with(:primary, :secondary)
@controller.send(:sn_set_navigation)
end
end
end
describe 'instance_methods' do
describe 'current_navigation' do
it "should set the sn_current_navigation_args as specified" do
@controller.current_navigation(:first)
@controller.instance_variable_get(:@sn_current_navigation_args).should == [:first]
end
it "should set the sn_current_navigation_args as specified" do
@controller.current_navigation(:first, :second)
@controller.instance_variable_get(:@sn_current_navigation_args).should == [:first, :second]
end
end
end
end
describe 'SimpleNavigation module additions' do
describe 'handle_explicit_navigation' do
def args(*args)
SimpleNavigation.stub!(:explicit_navigation_args => args.compact.empty? ? nil : args)
end
before(:each) do
@controller = stub(:controller)
@adapter = stub(:adapter, :controller => @controller)
SimpleNavigation.stub!(:adapter => @adapter)
end
context 'with explicit navigation set' do
context 'list of navigations' do
before(:each) do
args :first, :second, :third
end
it "should set the correct instance var in the controller" do
@controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_3, :third)
SimpleNavigation.handle_explicit_navigation
end
end
context 'single navigation' do
context 'specified key is a valid navigation item' do
before(:each) do
@primary = stub(:primary, :level_for_item => 2)
SimpleNavigation.stub!(:primary_navigation => @primary)
args :key
end
it "should set the correct instance var in the controller" do
@controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
SimpleNavigation.handle_explicit_navigation
end
end
context 'specified key is an invalid navigation item' do
before(:each) do
@primary = stub(:primary, :level_for_item => nil)
SimpleNavigation.stub!(:primary_navigation => @primary)
args :key
end
it "should raise an ArgumentError" do
lambda {SimpleNavigation.handle_explicit_navigation}.should raise_error(ArgumentError)
end
end
end
context 'hash with level' do
before(:each) do
args :level_2 => :key
end
it "should set the correct instance var in the controller" do
@controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
SimpleNavigation.handle_explicit_navigation
end
end
context 'hash with multiple_levels' do
before(:each) do
args :level_2 => :key, :level_1 => :bla
end
it "should set the correct instance var in the controller" do
@controller.should_receive(:instance_variable_set).with(:@sn_current_navigation_2, :key)
SimpleNavigation.handle_explicit_navigation
end
end
end
context 'without explicit navigation set' do
before(:each) do
args nil
end
it "should not set the current_navigation instance var in the controller" do
@controller.should_not_receive(:instance_variable_set)
SimpleNavigation.handle_explicit_navigation
end
end
end
describe 'current_navigation_for' do
before(:each) do
@controller = stub(:controller)
@adapter = stub(:adapter, :controller => @controller)
SimpleNavigation.stub!(:adapter => @adapter)
end
it "should access the correct instance_var in the controller" do
@controller.should_receive(:instance_variable_get).with(:@sn_current_navigation_1)
SimpleNavigation.current_navigation_for(1)
end
end
end
describe SimpleNavigation::Item do
before(:each) do
@item_container = stub(:item_container, :level => 1)
@item = SimpleNavigation::Item.new(@item_container, :my_key, 'name', 'url', {})
end
describe 'selected_by_config?' do
context 'navigation explicitly set' do
it "should return true if current matches key" do
SimpleNavigation.stub!(:current_navigation_for => :my_key)
@item.should be_selected_by_config
end
it "should return false if current does not match key" do
SimpleNavigation.stub!(:current_navigation_for => :other_key)
@item.should_not be_selected_by_config
end
end
context 'navigation not explicitly set' do
before(:each) do
SimpleNavigation.stub!(:current_navigation_for => nil)
end
it {@item.should_not be_selected_by_config}
end
end
end
describe SimpleNavigation::ItemContainer do
describe 'selected_item' do
before(:each) do
SimpleNavigation.stub!(:current_navigation_for)
@item_container = SimpleNavigation::ItemContainer.new
@item_1 = stub(:item, :selected? => false)
@item_2 = stub(:item, :selected? => false)
@item_container.instance_variable_set(:@items, [@item_1, @item_2])
end
context 'navigation explicitely set' do
before(:each) do
@item_container.stub!(:[] => @item_1)
end
it "should return the explicitely selected item" do
@item_container.selected_item.should == @item_1
end
end
context 'navigation not explicitely set' do
before(:each) do
@item_container.stub!(:[] => nil)
end
context 'no item selected' do
it "should return nil" do
@item_container.selected_item.should be_nil
end
end
context 'one item selected' do
before(:each) do
@item_1.stub!(:selected? => true)
end
it "should return the selected item" do
@item_container.selected_item.should == @item_1
end
end
end
end
end
end
simple-navigation-3.11.0/spec/spec_helper.rb 0000644 0001756 0001756 00000005641 12202013367 020076 0 ustar synrg synrg ENV["RAILS_ENV"] = "test"
require 'rubygems'
require 'rspec'
require 'json_spec'
require 'action_controller'
module Rails
module VERSION
MAJOR = 2
end
end unless defined? Rails
$:.unshift File.dirname(__FILE__)
$:.unshift File.join(File.dirname(__FILE__), '../lib')
require 'simple_navigation'
# SimpleNavigation.root = './'
RAILS_ROOT = './' unless defined?(RAILS_ROOT)
RAILS_ENV = 'test' unless defined?(RAILS_ENV)
RSpec.configure do |config|
# == Mock Framework
#
# If you prefer to use mocha, flexmock or RR, uncomment the appropriate line:
#
# config.mock_with :mocha
# config.mock_with :flexmock
# config.mock_with :rr
config.mock_with :rspec
config.include JsonSpec::Helpers
end
# spec helper methods
def sub_items
[
[:subnav1, 'subnav1', 'subnav1_url'],
[:subnav2, 'subnav2', 'subnav2_url']
]
end
def primary_items
[
[:users, 'users', 'first_url', {:id => 'my_id', :link => {:id => 'my_link_id'}}],
[:invoices, 'invoices', 'second_url'],
[:accounts, 'accounts', 'third_url', {:style => 'float:right', :link => {:style => 'float:left'}}],
[:miscellany, 'miscellany']
]
end
def primary_container
containers.first
end
def containers
container = SimpleNavigation::ItemContainer.new(1)
container.dom_id = 'nav_dom_id'
container.dom_class = 'nav_dom_class'
@items = primary_items.map {|params| SimpleNavigation::Item.new(container, *params)}
@items.each {|i| i.stub!(:selected? => false, :selected_by_condition? => false)}
container.instance_variable_set(:@items, @items)
sub_container = subnav_container
primary_item(:invoices) {|item| item.instance_variable_set(:@sub_navigation, sub_container)}
[container,sub_container]
end
def primary_item(key)
item = @items.find {|i| i.key == key}
block_given? ? yield(item) : item
end
def sub_item(key)
primary_item(:invoices).instance_variable_get(:@sub_navigation).items.find { |i| i.key == key}
end
def select_item(key)
if(key == :subnav1)
select_item(:invoices)
primary_item(:invoices) do |item|
item.instance_variable_get(:@sub_navigation).items.find { |i| i.key == key}.stub!(:selected? => true, :selected_by_condition? => true)
end
else
primary_item(key) {|item| item.stub!(:selected? => true) unless item.frozen?}
end
end
def subnav_container
container = SimpleNavigation::ItemContainer.new(2)
items = sub_items.map {|params| SimpleNavigation::Item.new(container, *params)}
items.each {|i| i.stub!(:selected? => false, :selected_by_condition? => false)}
container.instance_variable_set(:@items, items)
container
end
def setup_renderer_for(renderer_class, framework, options)
setup_adapter_for framework
@renderer = renderer_class.new(options)
end
def setup_adapter_for(framework)
adapter = case framework
when :rails
SimpleNavigation::Adapters::Rails.new(stub(:context, :view_context => ActionView::Base.new))
end
SimpleNavigation.stub!(:adapter => adapter)
adapter
end
simple-navigation-3.11.0/VERSION 0000644 0001756 0001756 00000000006 12202013367 015364 0 ustar synrg synrg 3.11.0 simple-navigation-3.11.0/Rakefile 0000644 0001756 0001756 00000003614 12202013367 015771 0 ustar synrg synrg require 'rake'
require 'rspec/core/rake_task'
require 'rdoc/task'
desc 'Default: run specs.'
task :default => :spec
desc 'Run the specs'
RSpec::Core::RakeTask.new(:spec) do |t|
t.rspec_opts = ['--colour --format progress']
end
namespace :spec do
desc "Run all specs with RCov"
RSpec::Core::RakeTask.new(:rcov) do |t|
t.rspec_opts = ['--colour --format progress']
t.rcov = true
t.rcov_opts = ['--exclude', 'spec,/Users/']
end
end
desc 'Generate documentation for the simple_navigation plugin.'
RDoc::Task.new(:rdoc) do |rdoc|
rdoc.rdoc_dir = 'rdoc'
rdoc.title = 'SimpleNavigation'
rdoc.options << '--line-numbers' << '--inline-source'
rdoc.rdoc_files.include('README')
rdoc.rdoc_files.include('lib/**/*.rb')
end
begin
require 'jeweler'
Jeweler::Tasks.new do |gemspec|
gemspec.name = "simple-navigation"
gemspec.summary = "simple-navigation is a ruby library for creating navigations (with multiple levels) for your Rails2, Rails3, Sinatra or Padrino application."
gemspec.email = "andreas.schacke@gmail.com"
gemspec.homepage = "http://github.com/andi/simple-navigation"
gemspec.description = "With the simple-navigation gem installed you can easily create multilevel navigations for your Rails, Sinatra or Padrino applications. The navigation is defined in a single configuration file. It supports automatic as well as explicit highlighting of the currently active navigation through regular expressions."
gemspec.authors = ["Andi Schacke", "Mark J. Titorenko"]
gemspec.rdoc_options = ["--inline-source", "--charset=UTF-8"]
gemspec.files = FileList["[A-Z]*", "{lib,spec,rails,generators}/**/*"] - FileList["**/*.log", "Gemfile.lock"]
gemspec.rubyforge_project = 'andi'
end
Jeweler::GemcutterTasks.new
rescue LoadError => e
puts "Jeweler not available (#{e}). Install it with: sudo gem install technicalpickles-jeweler -s http://gems.github.com"
end