Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

add icon and badge helpers

  • Loading branch information...
commit 3a30560e95ea102c05ad417bf3a45d5941aaa897 1 parent 9b9e81f
@mcanato authored
View
2  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'
View
37 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
+
View
37 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
+
View
8 lib/twitter-bootstrap-markup-rails/components/inline_label.rb
@@ -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
View
2  lib/twitter-bootstrap-markup-rails/helpers.rb
@@ -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'
View
34 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
+
View
26 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
+
Please sign in to comment.
Something went wrong with that request. Please try again.