Skip to content

Commit

Permalink
add icon and badge helpers
Browse files Browse the repository at this point in the history
  • Loading branch information
ilpeste committed Jul 18, 2012
1 parent 9b9e81f commit 3a30560
Show file tree
Hide file tree
Showing 7 changed files with 144 additions and 2 deletions.
2 changes: 2 additions & 0 deletions lib/twitter-bootstrap-markup-rails/components.rb
@@ -1,8 +1,10 @@
module Twitter::Bootstrap::Markup::Rails
module Components
autoload :Base, 'twitter-bootstrap-markup-rails/components/base'
autoload :Badge, 'twitter-bootstrap-markup-rails/components/badge'
autoload :Alert, 'twitter-bootstrap-markup-rails/components/alert'
autoload :InlineLabel, 'twitter-bootstrap-markup-rails/components/inline_label'
autoload :Icon, 'twitter-bootstrap-markup-rails/components/icon'
autoload :Form, 'twitter-bootstrap-markup-rails/components/form'
autoload :FormBuilder, 'twitter-bootstrap-markup-rails/components/form_builder'
autoload :Button, 'twitter-bootstrap-markup-rails/components/button'
Expand Down
37 changes: 37 additions & 0 deletions lib/twitter-bootstrap-markup-rails/components/badge.rb
@@ -0,0 +1,37 @@
module Twitter::Bootstrap::Markup::Rails::Components
class Badge < Base
attr_reader :message

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

def to_s
output_buffer << content_tag(:span, message, build_tag_options).html_safe
super
end

private
def default_options
{
:class => "badge",
:class_prefix => "badge-",
:type => nil,
:html_options => {}
}
end

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

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

37 changes: 37 additions & 0 deletions lib/twitter-bootstrap-markup-rails/components/icon.rb
@@ -0,0 +1,37 @@
module Twitter::Bootstrap::Markup::Rails::Components
class Icon < Base

def initialize(options = {})
super
end

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

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

def build_class
klass = [ options[:class] ]
klass << "#{options[:class_prefix]}#{options[:name]}" if options[:name]
klass << " icon-white" unless options[:color].equal?(:black)
classes.join(" ")
end

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

8 changes: 6 additions & 2 deletions lib/twitter-bootstrap-markup-rails/components/inline_label.rb
Expand Up @@ -22,8 +22,12 @@ def default_options
end

def build_class
classes = [options[:class]]
classes << options[:type] if options[:type]
classes = []

unless options[:class].blank?
classes << options[:class]
classes << "#{options[:class]}-#{options[:type]}" if options[:type]
end
classes.join(" ")
end

Expand Down
2 changes: 2 additions & 0 deletions lib/twitter-bootstrap-markup-rails/helpers.rb
Expand Up @@ -2,6 +2,8 @@ module Twitter::Bootstrap::Markup::Rails
module Helpers
autoload :AlertHelpers, 'twitter-bootstrap-markup-rails/helpers/alert_helpers'
autoload :InlineLabelHelpers, 'twitter-bootstrap-markup-rails/helpers/inline_label_helpers'
autoload :BadgeHelpers, 'twitter-bootstrap-markup-rails/helpers/badge_helpers'
autoload :IconHelpers, 'twitter-bootstrap-markup-rails/helpers/icon_helpers'
autoload :FormHelpers, 'twitter-bootstrap-markup-rails/helpers/form_helpers'
autoload :ButtonHelpers, 'twitter-bootstrap-markup-rails/helpers/button_helpers'
autoload :NavigationHelpers, 'twitter-bootstrap-markup-rails/helpers/navigation_helpers'
Expand Down
34 changes: 34 additions & 0 deletions lib/twitter-bootstrap-markup-rails/helpers/badge_helpers.rb
@@ -0,0 +1,34 @@
# encoding: utf-8
module Twitter::Bootstrap::Markup::Rails::Helpers
module BadgeHelpers
# Renders badge
#
# @param [String] message message to be displayed
# @param [Hash] options hash containing options (default: {}):
# :type - The String type of alert to display: success warning important notice
# :html_options - Any additional options you'd like to pass to the span tag that will be created
# for this label (for instance :"data-whatever" can be specified in :html_options).
#
# Examples
#
# bootstrap_badge_tag("Hello!")
# # => '<span class="badge">Hello!</span>'
#
# Returns HTML String for the label
def bootstrap_badge_tag(message, options = {})
Twitter::Bootstrap::Markup::Rails::Components::Badge.new(
message,
options
).to_s
end

%w(success warning important notice).each do |type|
module_eval <<-EOF
def bootstrap_badge_#{type}_tag(message, options = {})
bootstrap_badge_tag(message, options.merge({ :type => "#{type}" }))
end
EOF
end
end
end

26 changes: 26 additions & 0 deletions lib/twitter-bootstrap-markup-rails/helpers/icon_helpers.rb
@@ -0,0 +1,26 @@
# encoding: utf-8
module Twitter::Bootstrap::Markup::Rails::Helpers
module IconHelpers
# Renders badge
#
# @param [String] message message to be displayed
# @param [Hash] options hash containing options (default: {}):
# :type - The String type of alert to display: success warning important notice
# :html_options - Any additional options you'd like to pass to the span tag that will be created
# for this label (for instance :"data-whatever" can be specified in :html_options).
#
# Examples
#
# bootstrap_icon_tag(:name => 'user', :color => :white)
# # => '<i class="icon-user icon-white"></i>'
#
# Returns HTML String for the label
def bootstrap_icon_tag(options = {})
Twitter::Bootstrap::Markup::Rails::Components::Icon.new(
options
).to_s
end

end
end

0 comments on commit 3a30560

Please sign in to comment.