This repository has been archived by the owner on Dec 20, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
7 changed files
with
98 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
31 changes: 31 additions & 0 deletions
31
lib/twitter-bootstrap-markup-rails/helpers/icon_helpers.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|