Skip to content
This repository has been archived by the owner on Dec 20, 2021. It is now read-only.

Commit

Permalink
add support for icons
Browse files Browse the repository at this point in the history
  • Loading branch information
bcm committed May 4, 2013
1 parent 499e480 commit d8ac735
Show file tree
Hide file tree
Showing 7 changed files with 98 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ gem 'twitter-bootstrap-markup-rails', '0.3.2.2'
* Button dropdowns
* Modal windows
* Progress bars
* Icons

Documentation
---
Expand Down
1 change: 1 addition & 0 deletions lib/twitter-bootstrap-markup-rails/components.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ module Components
autoload :Navigation, 'twitter-bootstrap-markup-rails/components/navigation'
autoload :Modal, 'twitter-bootstrap-markup-rails/components/modal'
autoload :ProgressBar, 'twitter-bootstrap-markup-rails/components/progress_bar'
autoload :Icon, 'twitter-bootstrap-markup-rails/components/icon'
end
end

36 changes: 36 additions & 0 deletions lib/twitter-bootstrap-markup-rails/components/icon.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
module Twitter::Bootstrap::Markup::Rails::Components
class Icon < Base
attr_reader :name

def initialize(name, options = {})
@name = name
super
end

def to_s
output_buffer << content_tag(:i, nil, build_tag_options).html_safe
super
end

private
def default_options
{
:class => "icon-#{name}",
:color => nil,
:html_options => {}
}
end

def build_class
classes = [options[:class]]
classes << "icon-#{options[:color]}" if options[:color]
classes.join(" ")
end

def build_tag_options
ops = {:class => build_class}
ops.reverse_merge(options[:html_options])
end
end
end

1 change: 1 addition & 0 deletions lib/twitter-bootstrap-markup-rails/engine.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ class Engine < ::Rails::Engine
include Twitter::Bootstrap::Markup::Rails::Helpers::ButtonHelpers
include Twitter::Bootstrap::Markup::Rails::Helpers::NavigationHelpers
include Twitter::Bootstrap::Markup::Rails::Helpers::ModalHelpers
include Twitter::Bootstrap::Markup::Rails::Helpers::IconHelpers
end
end
end
Expand Down
1 change: 1 addition & 0 deletions lib/twitter-bootstrap-markup-rails/helpers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ module Helpers
autoload :NavigationHelpers, 'twitter-bootstrap-markup-rails/helpers/navigation_helpers'
autoload :ModalHelpers, 'twitter-bootstrap-markup-rails/helpers/modal_helpers'
autoload :ProgressBarHelpers, 'twitter-bootstrap-markup-rails/helpers/progress_bar_helpers'
autoload :IconHelpers, 'twitter-bootstrap-markup-rails/helpers/icon_helpers'
end
end

31 changes: 31 additions & 0 deletions lib/twitter-bootstrap-markup-rails/helpers/icon_helpers.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
# encoding: utf-8
module Twitter::Bootstrap::Markup::Rails::Helpers
module IconHelpers
# Renders an icon.
#
# Includes support for alternate color sets. Bootstrap only supports black (the default) and white ("inverted")
# icons out of the box. Other color sets can be found elsewhere (e.g.
# https://github.com/twitter/bootstrap/pull/2520).
#
# Examples:
#
# bootstrap_icon("globe")
# # => '<i class="icon-globe"></i>'
#
# bootstrap_icon("briefcase", :color => "white")
# # => '<i class="icon-globe icon-white"></i>'
#
# @param [String] name the name of the icon to display (e.g. "search", "thumbs-up")
# @option options [String] :color ("black") the color set from which the icon is chosen
# @option options [Hash] :html_options ({}) passed through to +content_tag+ to set attributes on the icon tag
#
# @return [String] the HTML string
# @see http://twitter.github.io/bootstrap/base-css.html#icons
def bootstrap_icon_tag(name, options = {})
Twitter::Bootstrap::Markup::Rails::Components::Icon.new(
name,
options
).to_s
end
end
end
27 changes: 27 additions & 0 deletions spec/helpers/icon_helpers_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
require 'spec_helper'

describe Twitter::Bootstrap::Markup::Rails::Helpers::IconHelpers do
include BootstrapSpecHelper

describe "#bootstrap_icon_tag" do
before do
@output_buffer = ''
end

it "has icon class" do
concat bootstrap_icon_tag("search")
output_buffer.should have_tag("i.icon-search")
end

it "has color class" do
concat bootstrap_icon_tag("search", :color => "white")
output_buffer.should have_tag("i.icon-white")
end

it "should add html_options to the resulting tag when specified" do
concat bootstrap_icon_tag("search", :html_options => {:"data-test" => "42"})
output_buffer.should have_tag("i[data-test='42']")
end
end
end

0 comments on commit d8ac735

Please sign in to comment.