Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Initial commit

  • Loading branch information...
commit 2d3c62072a8883510c5ffed01d42c9aa15dd9273 0 parents
@manuelmeurer manuelmeurer authored
7 .travis.yml
@@ -0,0 +1,7 @@
+language: ruby
+rvm:
+ - 1.9.2
+ - 1.9.3
+ - 2.0.0
+ - jruby-19mode
+ - rbx-19mode
3  Gemfile
@@ -0,0 +1,3 @@
+source 'https://rubygems.org/'
+
+gemspec
9 Guardfile
@@ -0,0 +1,9 @@
+guard 'rspec', cli: '--drb --profile', all_after_pass: false do
+ # Specs
+ watch(%r(^spec/.+_spec\.rb$))
+ watch('spec/spec_helper.rb') { 'spec' }
+ watch(%r(^spec/support/(.+)\.rb$)) { 'spec' }
+
+ # Files
+ watch(%r(^lib/(.+)\.rb$)) { |m| "spec/#{m[1]}_spec.rb" }
+end
22 LICENSE.txt
@@ -0,0 +1,22 @@
+Copyright (c) 2013 kraut computing UG (haftungsbeschränkt)
+
+MIT License
+
+Permission is hereby granted, free of charge, to any person obtaining
+a copy of this software and associated documentation files (the
+"Software"), to deal in the Software without restriction, including
+without limitation the rights to use, copy, modify, merge, publish,
+distribute, sublicense, and/or sell copies of the Software, and to
+permit persons to whom the Software is furnished to do so, subject to
+the following conditions:
+
+The above copyright notice and this permission notice shall be
+included in all copies or substantial portions of the Software.
+
+THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
+EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
+MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
+NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
+LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
+OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
+WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
41 README.md
@@ -0,0 +1,41 @@
+# BootstrapNavbar
+
+[![Gem Version](https://badge.fury.io/rb/bootstrap_navbar.png)](http://badge.fury.io/rb/bootstrap_navbar)
+[![Build Status](https://secure.travis-ci.org/krautcomputing/bootstrap_navbar.png)](http://travis-ci.org/krautcomputing/bootstrap_navbar)
+[![Dependency Status](https://gemnasium.com/krautcomputing/bootstrap_navbar.png)](https://gemnasium.com/krautcomputing/bootstrap_navbar)
+[![Code Climate](https://codeclimate.com/github/krautcomputing/bootstrap_navbar.png)](https://codeclimate.com/github/krautcomputing/bootstrap_navbar)
+
+Helpers to generate a Twitter Bootstrap style navbar
+
+## Installation
+
+Add this line to your application's Gemfile:
+
+ gem 'bootstrap_navbar'
+
+And then execute:
+
+ $ bundle
+
+Or install it yourself as:
+
+ $ gem install bootstrap_navbar
+
+## Usage
+
+### Set current_url_method
+
+BootstrapNavbar has to be able to query for the current URL, e.g. to determine if a menu item is active or not. Since the way the current URL is determined varies depending on whether you use Rails, Sinatra etc., this has to be set beforehand in some kind of initializer:
+
+```ruby
+# For Rails
+BootstrapNavbar.current_url_method = -> { '' }
+```
+
+## Contributing
+
+1. Fork it
+2. Create your feature branch (`git checkout -b my-new-feature`)
+3. Commit your changes (`git commit -am 'Add some feature'`)
+4. Push to the branch (`git push origin my-new-feature`)
+5. Create new Pull Request
5 Rakefile
@@ -0,0 +1,5 @@
+require 'rspec/core/rake_task'
+
+RSpec::Core::RakeTask.new(:spec)
+
+task default: :spec
28 bootstrap_navbar.gemspec
@@ -0,0 +1,28 @@
+# encoding: utf-8
+
+lib = File.expand_path('../lib', __FILE__)
+$LOAD_PATH.unshift(lib) unless $LOAD_PATH.include?(lib)
+
+require 'bootstrap_navbar/version'
+
+Gem::Specification.new do |gem|
+ gem.name = 'bootstrap_navbar'
+ gem.version = BootstrapNavbar::VERSION
+ gem.platform = Gem::Platform::RUBY
+ gem.authors = ['Manuel Meurer']
+ gem.email = 'manuel.meurer@gmail.com'
+ gem.summary = 'Helpers to generate a Twitter Bootstrap style navbar'
+ gem.description = 'Helpers to generate a Twitter Bootstrap style navbar'
+ gem.homepage = 'https://github.com/krautcomputing/bootstrap_navbar'
+ gem.license = 'MIT'
+
+ gem.files = `git ls-files`.split($/)
+ gem.executables = gem.files.grep(%r(^bin/)).map { |f| File.basename(f) }
+ gem.test_files = gem.files.grep(%r(^(test|spec|features)/))
+ gem.require_paths = ['lib']
+
+ gem.add_development_dependency 'rake', '>= 10.0.0'
+ gem.add_development_dependency 'rspec', '~> 2.13.0'
+ gem.add_development_dependency 'rspec-html-matchers', '~> 0.4.1'
+ gem.add_development_dependency 'guard-rspec', '~> 3.0'
+end
14 lib/bootstrap_navbar.rb
@@ -0,0 +1,14 @@
+require 'bootstrap_navbar/version'
+
+module BootstrapNavbar
+ def self.current_url_method=(value)
+ raise StandardError, '`current_url_method` must be an object that responds to `#call`' unless value.respond_to?(:call)
+ @current_url_method = value
+ end
+
+ def self.current_url_method
+ @current_url_method
+ end
+end
+
+require 'bootstrap_navbar/helpers'
143 lib/bootstrap_navbar/helpers.rb
@@ -0,0 +1,143 @@
+module BootstrapNavbar::Helpers
+ def nav_bar(options = {}, &block)
+ nav_bar_div options do
+ navbar_inner_div do
+ container_div options[:brand], options[:brand_link], options[:responsive], options[:fluid], &block
+ end
+ end
+ end
+
+ def menu_group(pull = nil, &block)
+ css_classes = %w(nav).tap do |css_classes|
+ css_classes << "pull-#{pull}" if pull
+ end
+ <<-HTML
+<ul class="#{css_classes.join(' ')}">
+ #{block.call if block_given?}
+</ul>
+HTML
+ end
+
+ def menu_item(name, path = '#', options = {})
+ css_class = path.sub(%r(/\z), '') == current_url.sub(%r(/\z), '') ? 'active' : nil
+ options_string = options.map { |k, v| %(#{k}="#{v}") }
+ <<-HTML
+<li class="#{css_class}">
+ <a href="#{path}" #{options_string}>
+ #{name}
+ </a>
+</li>
+HTML
+ end
+
+ def drop_down(name, &block)
+ <<-HTML
+<li class="dropdown">
+ <a href="#" class="dropdown-toggle", data-toggle="dropdown">
+ #{name} <b class="caret"></b>
+ </a>
+ <ul class="dropdown-menu">
+ #{block.call if block_given?}
+ </ul>
+</li>
+HTML
+ end
+
+ def drop_down_divider
+ %(<li class="divider"></li>)
+ end
+
+ def drop_down_header(text)
+ %(<li class="nav-header">#{text}</li>)
+ end
+
+ def menu_divider
+ %(<li class="divider-vertical"></li>)
+ end
+
+ def menu_text(text = nil, pull = nil, &block)
+ css_classes = %w(navbar-text).tap do |css_classes|
+ css_classes << "pull-#{pull}" if pull
+ end
+ <<-HTML
+<p class="#{css_classes.join(' ')}">
+ #{block_given? ? block.call : text}
+</p>
+HTML
+ end
+
+ def brand_link(name, url = nil)
+ %(<a href="#{url || '/'}" class="brand">#{name}</a>)
+ end
+
+ private
+
+ def nav_bar_div(options, &block)
+ position = case
+ when options.has_key?(:static)
+ "static-#{options[:static]}"
+ when options.has_key?(:fixed)
+ "fixed-#{options[:fixed]}"
+ end
+
+ css_classes = %w(navbar).tap do |css_classes|
+ css_classes << "navbar-#{position}" if position
+ css_classes << 'navbar-inverse' if options[:inverse]
+ end
+
+ <<-HTML
+<div class="#{css_classes.join(' ')}">
+ #{block.call if block_given?}
+</div>
+HTML
+ end
+
+ def navbar_inner_div(&block)
+ <<-HTML
+<div class="navbar-inner">
+ #{block.call if block_given?}
+</div>
+HTML
+ end
+
+ def container_div(brand, brand_link, responsive, fluid, &block)
+ css_class = fluid ? 'container-fluid' : 'container'
+ content = [].tap do |content|
+ content << responsive_button if responsive
+ content << brand_link(brand, brand_link) if brand || brand_link
+ content << if responsive
+ responsive_wrapper(&block)
+ else
+ block.call if block_given?
+ end
+ end
+ <<-HTML
+<div class="#{css_class}">
+ #{content.join("\n")}
+</div>
+HTML
+ end
+
+ def responsive_wrapper(&block)
+ <<-HTML
+<div class="nav-collapse collapse">
+ #{block.call if block_given?}
+</div>
+HTML
+ end
+
+ def responsive_button
+ <<-HTML
+<a class="btn btn-navbar", data-toggle="collapse" data-target=".nav-collapse">
+ <span class='icon-bar'></span>
+ <span class='icon-bar'></span>
+ <span class='icon-bar'></span>
+</div>
+HTML
+ end
+
+ def current_url
+ raise StandardError, 'current_url_method is not defined.' if BootstrapNavbar.current_url_method.nil?
+ BootstrapNavbar.current_url_method.call
+ end
+end
3  lib/bootstrap_navbar/version.rb
@@ -0,0 +1,3 @@
+module BootstrapNavbar
+ VERSION = '0.0.1'
+end
185 spec/bootstrap_navbar/helpers_spec.rb
@@ -0,0 +1,185 @@
+require 'spec_helper'
+
+describe BootstrapNavbar::Helpers do
+ subject do
+ Class.new do
+ extend BootstrapNavbar::Helpers
+ end
+ end
+
+ before do
+ BootstrapNavbar.current_url_method = -> { '' }
+ end
+
+ describe '#nav_bar' do
+ context 'div.navbar' do
+ context '"static" parameter' do
+ it 'generates the correct HTML' do
+ subject.nav_bar(static: 'top').should have_tag(:div, with: { class: 'navbar navbar-static-top' })
+ subject.nav_bar(static: 'bottom').should have_tag(:div, with: { class: 'navbar navbar-static-bottom' })
+ end
+ end
+
+ context '"fixed" parameter' do
+ it 'generates the correct HTML' do
+ subject.nav_bar(fixed: 'top').should have_tag(:div, with: { class: 'navbar navbar-fixed-top' })
+ subject.nav_bar(fixed: 'bottom').should have_tag(:div, with: { class: 'navbar navbar-fixed-bottom' })
+ end
+ end
+
+ context '"inverse" parameter' do
+ it 'generates the correct HTML' do
+ subject.nav_bar(inverse: true).should have_tag(:div, with: { class: 'navbar navbar-inverse' })
+ end
+ end
+
+ context 'no parameters' do
+ it 'generates the correct HTML' do
+ subject.nav_bar.should have_tag(:div, with: { class: 'navbar' })
+ subject.nav_bar { 'foo' }.should have_tag(:div, with: { class: 'navbar' }, content: 'foo')
+ end
+ end
+ end
+
+ context 'div.navbar-inner' do
+ it 'generates the correct HTML' do
+ subject.nav_bar.should have_tag(:div, with: { class: 'navbar-inner' })
+ end
+ end
+
+ context 'div.container' do
+ it 'generates the correct HTML' do
+ subject.nav_bar.should have_tag(:div, with: { class: 'container' })
+ end
+ end
+
+ describe 'brand and brank_link parameters' do
+ it 'generates the correct HTML' do
+ subject.nav_bar(brand: 'foo').should have_tag(:a, with: { href: '/', class: 'brand' }, content: 'foo')
+ subject.nav_bar(brand: 'foo', brand_link: 'http://google.com').should have_tag(:a, with: { href: 'http://google.com', class: 'brand' }, content: 'foo')
+ subject.nav_bar(brand_link: 'http://google.com').should have_tag(:a, with: { href: 'http://google.com', class: 'brand' })
+ end
+ end
+
+ describe '"responsive" parameter' do
+ it 'generates the correct HTML' do
+ subject.nav_bar(responsive: true).should have_tag(:a, with: { class: 'btn btn-navbar', :'data-toggle' => 'collapse', :'data-target' => '.nav-collapse' }) do
+ 3.times do
+ with_tag :span, with: { class: 'icon-bar' }
+ end
+ end
+ end
+ end
+
+ describe '"fluid" parameter' do
+ it 'generates the correct HTML' do
+ subject.nav_bar(fluid: true).should have_tag(:div, with: { class: 'container-fluid' })
+ end
+ end
+ end
+
+ describe '#menu_group' do
+ describe '"pull" parameter' do
+ it 'generates the correct HTML' do
+ subject.menu_group('right').should have_tag(:ul, with: { class: 'nav pull-right' })
+ end
+ end
+
+ context 'no parameters' do
+ it 'generates the correct HTML' do
+ subject.menu_group.should have_tag(:ul, with: { class: 'nav' })
+ subject.menu_group { 'foo' }.should have_tag(:ul, with: { class: 'nav' }, content: 'foo')
+ end
+ end
+ end
+
+ describe '#menu_item' do
+ context 'with current URL' do
+ it 'generates the correct HTML' do
+ BootstrapNavbar.current_url_method = -> { '/' }
+ subject.menu_item('foo', '/').should have_tag(:li, with: { class: 'active' }) do
+ with_tag :a, with: { href: '/' }, content: 'foo'
+ end
+
+ BootstrapNavbar.current_url_method = -> { '/foo' }
+ subject.menu_item('foo', '/foo').should have_tag(:li, with: { class: 'active' }) do
+ with_tag :a, with: { href: '/foo' }, content: 'foo'
+ end
+ end
+ end
+
+ context 'without current URL' do
+ it 'generates the correct HTML' do
+ BootstrapNavbar.current_url_method = -> { '/foo' }
+ subject.menu_item('foo').should have_tag(:li, without: { class: 'active' }) do
+ with_tag :a, with: { href: '#' }, content: 'foo'
+ end
+ subject.menu_item('foo', '/').should have_tag(:li, without: { class: 'active' }) do
+ with_tag :a, with: { href: '/' }, content: 'foo'
+ end
+ subject.menu_item('foo', '/bar').should have_tag(:li, without: { class: 'active' }) do
+ with_tag :a, with: { href: '/bar' }, content: 'foo'
+ end
+
+ BootstrapNavbar.current_url_method = -> { '/' }
+ subject.menu_item('foo', '/foo').should have_tag(:li, without: { class: 'active' }) do
+ with_tag :a, with: { href: '/foo' }, content: 'foo'
+ end
+ end
+ end
+ end
+
+ describe '#drop_down' do
+ it 'generates the correct HTML' do
+ subject.drop_down('foo').should have_tag(:li, with: { class: 'dropdown' }) do
+ with_tag :a, with: { href: '#', class: 'dropdown-toggle', :'data-toggle' => 'dropdown' } do
+ with_text /foo/
+ with_tag :b, with: { class: 'caret' }
+ end
+ with_tag :ul, with: { class: 'dropdown-menu' }
+ end
+
+ subject.drop_down('foo') { 'bar' }.should have_tag(:li, with: { class: 'dropdown' }) do
+ with_tag :a, with: { href: '#', class: 'dropdown-toggle', :'data-toggle' => 'dropdown' } do
+ with_text /foo/
+ with_tag :b, with: { class: 'caret' }
+ end
+ with_tag :ul, with: { class: 'dropdown-menu' }, content: 'bar'
+ end
+ end
+ end
+
+ describe '#drop_down_divider' do
+ it 'generates the correct HTML' do
+ subject.drop_down_divider.should have_tag(:li, with: { class: 'divider' }, content: '')
+ end
+ end
+
+ describe '#drop_down_header' do
+ it 'generates the correct HTML' do
+ subject.drop_down_header('foo').should have_tag(:li, with: { class: 'nav-header' }, content: 'foo')
+ end
+ end
+
+ describe '#menu_divider' do
+ it 'generates the correct HTML' do
+ subject.menu_divider.should have_tag(:li, with: { class: 'divider-vertical' }, content: '')
+ end
+ end
+
+ describe '#menu_text' do
+ it 'generates the correct HTML' do
+ subject.menu_text('foo').should have_tag(:p, with: { class: 'navbar-text' }, content: 'foo')
+ subject.menu_text { 'foo' }.should have_tag(:p, with: { class: 'navbar-text' }, content: 'foo')
+ subject.menu_text('foo', 'right').should have_tag(:p, with: { class: 'navbar-text pull-right' }, content: 'foo')
+ subject.menu_text(nil, 'left') { 'foo' }.should have_tag(:p, with: { class: 'navbar-text pull-left' }, content: 'foo')
+ end
+ end
+
+ describe '#brand_link' do
+ it 'generates the correct HTML' do
+ subject.brand_link('foo').should have_tag(:a, with: { class: 'brand', href: '/' }, content: 'foo')
+ subject.brand_link('foo', '/foo').should have_tag(:a, with: { class: 'brand', href: '/foo' }, content: 'foo')
+ end
+ end
+end
10 spec/spec_helper.rb
@@ -0,0 +1,10 @@
+require 'bootstrap_navbar'
+require 'rspec-html-matchers'
+
+RSpec.configure do |config|
+ config.treat_symbols_as_metadata_keys_with_true_values = true
+ config.run_all_when_everything_filtered = true
+ config.filter_run :focus
+
+ config.order = 'random'
+end
Please sign in to comment.
Something went wrong with that request. Please try again.