Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

added view helper to create <i> tag with icon classes

  • Loading branch information...
commit 270a7ed2c76a7a852db6aac35815aa5f08286bf1 1 parent 9d325cf
@kristianmandrup authored
View
2  Gemfile
@@ -1,4 +1,4 @@
-source "http://rubygems.org"
+source :rubygems
gem 'rails', '>= 3.1'
gem 'compass', '>= 0.11'
View
15 README.md
@@ -26,6 +26,21 @@ On IE7
The assets contains the `font-awesome` stylesheet in: LESS, SASS, SCSS and CSS.
The fonts are available in `assets/fonts`.
+## View helpers
+
+```ruby
+awesome_icon name, *args, &block
+```
+
+Usage examples:
+
+```ruby
+awesome_icon(:ok).should == "<i class=\"icon-ok\"></i>"
+awesome_icon(:ok, size: :large).should == "<i class=\"icon-ok icon-large\"></i>"
+awesome_icon(:ok, 'Okay :)').should == "<i class=\"icon-ok\"></i>Okay :)"
+awesome_icon(:ok) { 'Okay :)'}.should == "<i class=\"icon-ok\"></i>Okay :)"
+```
+
## Version
Font Awesome 2.0 is currently included. Enjoy :)
View
1  lib/font_awesome-sass-rails.rb
@@ -1,3 +1,4 @@
+require 'font_awesome-sass/view_helper'
require 'font_awesome-sass/version'
require 'font_awesome-sass/engine' if defined?(::Rails::Engine)
View
3  lib/font_awesome-sass/engine.rb
@@ -1,6 +1,9 @@
+require 'font_awesome-sass/view_helper'
+
module FontAwesomeSass
module Rails
class Engine < ::Rails::Engine
+ ActionView::Base.send :include, ::FontAwesomeSass::ViewHelper
end
end
end
View
16 lib/font_awesome-sass/view_helper.rb
@@ -0,0 +1,16 @@
+module FontAwesomeSass
+ module ViewHelper
+ def awesome_icon name, *args, &block
+ options = args.extract_options!
+ size = options[:size] if options
+ content = args.first unless args.blank?
+ content ||= capture(&block) if block_given?
+ content ||= ''
+
+ clazz = "icon-#{name}"
+ clazz << " icon-#{size}" if size.to_s == 'large'
+
+ content_tag(:i, nil, :class => clazz) + content.html_safe
+ end
+ end
+end
View
31 spec/font_awesome_sass/view_helper_spec.rb
@@ -0,0 +1,31 @@
+require 'spec_helper'
+
+describe FontAwesomeSass::ViewHelper do
+ include ControllerTestHelpers,
+ FontAwesomeSass::ViewHelper
+
+ describe ".awesome_icon :ok, 'Okay :)'" do
+ specify do
+ awesome_icon(:ok).should == "<i class=\"icon-ok\"></i>"
+ end
+ end
+
+ describe ".awesome_icon :ok, 'Okay :)'" do
+ specify do
+ awesome_icon(:ok, size: :large).should == "<i class=\"icon-ok icon-large\"></i>"
+ end
+ end
+
+ # <i class="icon-ok"></i>Okay :)
+ describe ".awesome_icon :ok, 'Okay :)'" do
+ specify do
+ awesome_icon(:ok, 'Okay :)').should == "<i class=\"icon-ok\"></i>Okay :)"
+ end
+ end
+
+ describe ".awesome_icon(:ok) {'Okay :)'}" do
+ specify do
+ awesome_icon(:ok) { 'Okay :)'}.should == "<i class=\"icon-ok\"></i>Okay :)"
+ end
+ end
+end
View
41 spec/spec_helper.rb
@@ -1,5 +1,42 @@
+# coding: utf-8
+require 'rubygems'
+require 'rails'
+require 'json'
+require 'active_support'
+require 'action_pack'
+require 'action_view'
+require 'action_controller'
+require 'action_view/template'
+
require 'rspec'
-require 'font-awesome-rails'
+require 'font_awesome-sass-rails'
+
+module ControllerTestHelpers
+
+ def self.included(base)
+ base.class_eval do
+
+ include ActionView::Helpers,
+ ActionView::Helpers::CaptureHelper,
+ ActionView::Helpers::JavaScriptHelper,
+ ActionView::Helpers::AssetTagHelper
+
+ # allow tabs.create to run by stubbing an output_buffer
+ attr_accessor :output_buffer
+ @output_buffer = ""
+
+ # stub content_for for testing
+ def content_for(name, content = nil, &block)
+ # this doesn't exist, and causes errors
+ @_content_for = {} unless defined? @_content_for
+ # we've got to initialize this, so we can concat to it
+ @_content_for[name] = '' if @_content_for[name].nil?
+ # now the rest is the same as in rails
+ content = capture(&block) if block_given?
+ @_content_for[name] << content if content
+ @_content_for[name] unless content
+ end
+ end
+ end
-RSpec.configure do |config|
end
Please sign in to comment.
Something went wrong with that request. Please try again.