nav 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
data/.gitignore ADDED
@@ -0,0 +1,7 @@
1
+ *.gem
2
+ .bundle
3
+ Gemfile.lock
4
+ pkg/*
5
+
6
+ # RubyMine
7
+ .idea
data/Gemfile ADDED
@@ -0,0 +1,4 @@
1
+ source "http://rubygems.org"
2
+
3
+ # Specify your gem's dependencies in nav.gemspec
4
+ gemspec
data/Rakefile ADDED
@@ -0,0 +1 @@
1
+ require 'bundler/gem_tasks'
@@ -0,0 +1,5 @@
1
+ module Nav
2
+
3
+ VERSION = "0.0.1"
4
+
5
+ end
data/lib/nav.rb ADDED
@@ -0,0 +1,100 @@
1
+ require_relative "nav/version"
2
+
3
+ module Nav
4
+
5
+ def nav( options = {}, &block )
6
+ builder = Builder.new( self, options )
7
+ yield( builder )
8
+ concat( builder.to_html ).html_safe if builder.actions?
9
+ end
10
+
11
+
12
+ private
13
+
14
+ class Builder
15
+
16
+ def initialize( template, options = {} )
17
+ @template, @options = template, options
18
+
19
+ @actions = []
20
+ end
21
+
22
+
23
+ def action( name, options = {}, html_options = {} )
24
+ wrapper_options = {
25
+ :current => html_options.delete(:current),
26
+ :disabled => html_options.delete(:disabled),
27
+ :force_current => html_options.delete(:force_current),
28
+ :url => options,
29
+ :prepend => html_options.delete(:prepend),
30
+ :append => html_options.delete(:append)
31
+ }
32
+
33
+ @actions << [link_to(name, options, html_options), wrapper_options, options]
34
+ end
35
+
36
+ def actions?; @actions.any?; end
37
+
38
+ def to_html
39
+ content_tag :ul, actions.html_safe, @options
40
+ end
41
+
42
+
43
+ private
44
+
45
+ def actions
46
+ @actions.collect{ |a| action_wrapper(*a) }.join
47
+ end
48
+
49
+ def action_wrapper( contents, options = {}, url_for_options = {} )
50
+ present = [contents, options, url_for_options] # the one we're dealing with
51
+ present_index = @actions.index( present )
52
+
53
+ before_present = @actions.at( present_index - 1 ) if present_index > 0
54
+ after_present = @actions.at( present_index + 1 ) if present_index < @actions.size
55
+
56
+ classes = []
57
+ classes << "first" if @actions.first == present
58
+ classes << "last" if @actions.last == present
59
+ classes << "current" if current?( *present )
60
+ classes << "disabled" if options.delete(:disabled)
61
+ classes << "before_current" if after_present && current?( *after_present )
62
+ classes << "after_current" if before_present && current?( *before_present )
63
+ classes << classes.join("_") if classes.any?
64
+
65
+ contents = options[:prepend].to_s + contents + options[:append].to_s
66
+
67
+ content_tag :li, contents.html_safe, :class => classes.join(" ")
68
+ end
69
+
70
+ def current?( contents, options = {}, url_for_options = {} )
71
+ current = options[:current]
72
+
73
+ is_current = case current
74
+ when TrueClass then true
75
+ when Regexp then request_uri.match(current).nil? ? false : true
76
+ else false
77
+ end
78
+
79
+ return true if is_current && !options[:disabled] && options[:force_current]
80
+ return true if is_current || !url_for_options.is_a?(Symbol) && @template.current_page?(url_for_options) && url_for_options != {} && !options[:disabled]
81
+
82
+ false
83
+ end
84
+
85
+ def request_uri
86
+ @request_uri or @request_uri = @template.request.request_uri
87
+ end
88
+
89
+
90
+ def content_tag( *args )
91
+ @template.content_tag( *args ).html_safe
92
+ end
93
+
94
+ def link_to( *args )
95
+ @template.link_to( *args ).html_safe
96
+ end
97
+
98
+ end
99
+
100
+ end
data/nav.gemspec ADDED
@@ -0,0 +1,23 @@
1
+ # -*- encoding: utf-8 -*-
2
+ $:.push File.expand_path("../lib", __FILE__)
3
+ require "nav/version"
4
+
5
+ Gem::Specification.new do |s|
6
+ s.name = "nav"
7
+ s.version = Nav::VERSION
8
+ s.authors = ["Rudolf Schmidt"]
9
+
10
+ s.homepage = ""
11
+ s.summary = %q{Simple nagivation builder}
12
+ s.description = %q{Simple nagivation builder}
13
+
14
+ s.rubyforge_project = "nav"
15
+
16
+ s.files = `git ls-files`.split("\n")
17
+ s.test_files = `git ls-files -- {test,spec,features}/*`.split("\n")
18
+ s.executables = `git ls-files -- bin/*`.split("\n").map{ |f| File.basename(f) }
19
+ s.require_paths = ["lib"]
20
+
21
+ s.add_dependency 'actionpack', '~> 3'
22
+ s.add_development_dependency 'rr'
23
+ end
data/rails/init.rb ADDED
@@ -0,0 +1,3 @@
1
+ require File.dirname(__FILE__) + '/../lib/nav'
2
+
3
+ ActionView::Base.send :include, Nav
data/test/nav_test.rb ADDED
@@ -0,0 +1,43 @@
1
+ require_relative 'test_helper'
2
+ puts 'awesome'
3
+
4
+ describe Nav do
5
+
6
+ # <ul id="nav">
7
+ # <li class="">before <a href="/">test</a></li>
8
+ # <li class="">before <a href="#" onclick="new Ajax.Request('/', {asynchronous:true, evalScripts:true, parameters:'authenticity_token=' + encodeURIComponent('7016ef6fd7413d836cd720a6071e22e4f14e0212')}); return false;">test</a></li>
9
+ # </ul>
10
+
11
+ before do
12
+ @view = ActionView::Base.new
13
+ @view.output_buffer = ''
14
+
15
+ stub( @view ).current_page? { false }
16
+ end
17
+
18
+ it "should display ul" do
19
+ m = @view.nav(:id => 'my-nav') { |m| m.action('my-link', '/link') }
20
+ assert_match(/<ul.*id=\"my-nav\">.*<\/ul>/, m)
21
+ end
22
+
23
+ it "should display li" do
24
+ m = @view.nav { |m| m.action('my-link', '/link') }
25
+ assert_match(/<li.*>.*<\/li>/, m)
26
+ end
27
+
28
+ it "should display link" do
29
+ m = @view.nav { |m| m.action('my-link', '/link') }
30
+ assert_match(/<a.*href=\"\/link\">my-link<\/a>/, m)
31
+ end
32
+
33
+ it "should display :prepend options before the link" do
34
+ m = @view.nav { |m| m.action('my-link', '/link', :prepend => 'something before') }
35
+ assert_match(/<li.*>something before<a/, m)
36
+ end
37
+
38
+ it "should display :append options after the link" do
39
+ m = @view.nav { |m| m.action('my-link', '/link', :append => 'something after') }
40
+ assert_match(/<\/a>something after<\/li>/, m)
41
+ end
42
+
43
+ end
@@ -0,0 +1,17 @@
1
+ require 'minitest/autorun'
2
+
3
+
4
+ begin
5
+ require 'action_view'
6
+ require 'rr'
7
+ rescue LoadError
8
+ STDERR.puts "Unable to run Nav tests."
9
+ else
10
+ require File.dirname(__FILE__) + "/../rails/init"
11
+ end
12
+
13
+
14
+ class MiniTest::Unit::TestCase
15
+ include RR::Adapters::TestUnit
16
+
17
+ end
metadata ADDED
@@ -0,0 +1,75 @@
1
+ --- !ruby/object:Gem::Specification
2
+ name: nav
3
+ version: !ruby/object:Gem::Version
4
+ version: 0.0.1
5
+ prerelease:
6
+ platform: ruby
7
+ authors:
8
+ - Rudolf Schmidt
9
+ autorequire:
10
+ bindir: bin
11
+ cert_chain: []
12
+ date: 2011-08-11 00:00:00.000000000Z
13
+ dependencies:
14
+ - !ruby/object:Gem::Dependency
15
+ name: actionpack
16
+ requirement: &21395560 !ruby/object:Gem::Requirement
17
+ none: false
18
+ requirements:
19
+ - - ~>
20
+ - !ruby/object:Gem::Version
21
+ version: '3'
22
+ type: :runtime
23
+ prerelease: false
24
+ version_requirements: *21395560
25
+ - !ruby/object:Gem::Dependency
26
+ name: rr
27
+ requirement: &21395080 !ruby/object:Gem::Requirement
28
+ none: false
29
+ requirements:
30
+ - - ! '>='
31
+ - !ruby/object:Gem::Version
32
+ version: '0'
33
+ type: :development
34
+ prerelease: false
35
+ version_requirements: *21395080
36
+ description: Simple nagivation builder
37
+ email:
38
+ executables: []
39
+ extensions: []
40
+ extra_rdoc_files: []
41
+ files:
42
+ - .gitignore
43
+ - Gemfile
44
+ - Rakefile
45
+ - lib/nav.rb
46
+ - lib/nav/version.rb
47
+ - nav.gemspec
48
+ - rails/init.rb
49
+ - test/nav_test.rb
50
+ - test/test_helper.rb
51
+ homepage: ''
52
+ licenses: []
53
+ post_install_message:
54
+ rdoc_options: []
55
+ require_paths:
56
+ - lib
57
+ required_ruby_version: !ruby/object:Gem::Requirement
58
+ none: false
59
+ requirements:
60
+ - - ! '>='
61
+ - !ruby/object:Gem::Version
62
+ version: '0'
63
+ required_rubygems_version: !ruby/object:Gem::Requirement
64
+ none: false
65
+ requirements:
66
+ - - ! '>='
67
+ - !ruby/object:Gem::Version
68
+ version: '0'
69
+ requirements: []
70
+ rubyforge_project: nav
71
+ rubygems_version: 1.8.6
72
+ signing_key:
73
+ specification_version: 3
74
+ summary: Simple nagivation builder
75
+ test_files: []