weather-icons-rails 0.0.1

Sign up to get free protection for your applications and to get access to all the features.
@@ -0,0 +1,92 @@
1
+ module FontAwesome
2
+ module Rails
3
+ module IconHelper
4
+ # Creates an icon tag given an icon name and possible icon
5
+ # modifiers.
6
+ #
7
+ # Examples
8
+ #
9
+ # fa_icon "camera-retro"
10
+ # # => <i class="fa fa-camera-retro"></i>
11
+ #
12
+ # fa_icon "camera-retro", text: "Take a photo"
13
+ # # => <i class="fa fa-camera-retro"></i> Take a photo
14
+ #
15
+ # fa_icon "camera-retro 2x"
16
+ # # => <i class="fa fa-camera-retro fa-2x"></i>
17
+ # fa_icon ["camera-retro", "4x"]
18
+ # # => <i class="fa fa-camera-retro fa-4x"></i>
19
+ # fa_icon "spinner spin lg"
20
+ # # => <i class="fa fa-spinner fa-spin fa-lg">
21
+ #
22
+ # fa_icon "quote-left 4x", class: "pull-left"
23
+ # # => <i class="fa fa-quote-left fa-4x pull-left"></i>
24
+ #
25
+ # fa_icon "user", data: { id: 123 }
26
+ # # => <i class="fa fa-user" data-id="123"></i>
27
+ #
28
+ # content_tag(:li, fa_icon("check li", text: "Bulleted list item"))
29
+ # # => <li><i class="fa fa-check fa-li"></i> Bulleted list item</li>
30
+ # def fa_icon(names = "flag", options = {})
31
+ # classes = ["fa"]
32
+ # classes.concat Private.icon_names(names)
33
+ # classes.concat Array(options.delete(:class))
34
+ # text = options.delete(:text)
35
+ # icon = content_tag(:i, nil, options.merge(:class => classes))
36
+ # Private.icon_join(icon, text)
37
+ # end
38
+
39
+ # Creates an stack set of icon tags given a base icon name, a main icon
40
+ # name, and possible icon modifiers.
41
+ #
42
+ # Examples
43
+ #
44
+ # fa_stacked_icon "twitter", base: "square-o"
45
+ # # => <span class="fa-stack">
46
+ # # => <i class="fa fa-square-o fa-stack-2x"></i>
47
+ # # => <i class="fa fa-twitter fa-stack-1x"></i>
48
+ # # => </span>
49
+ #
50
+ # fa_stacked_icon "terminal inverse", base: "square", class: "pull-right", text: "Hi!"
51
+ # # => <span class="fa-stack pull-right">
52
+ # # => <i class="fa fa-square fa-stack-2x"></i>
53
+ # # => <i class="fa fa-terminal fa-inverse fa-stack-1x"></i>
54
+ # # => </span> Hi!
55
+ #
56
+ # fa_stacked_icon "camera", base: "ban-circle", reverse: true
57
+ # # => <span class="fa-stack">
58
+ # # => <i class="fa fa-camera fa-stack-1x"></i>
59
+ # # => <i class="fa fa-ban-circle fa-stack-2x"></i>
60
+ # # => </span>
61
+ # def fa_stacked_icon(names = "flag", options = {})
62
+ # classes = Private.icon_names("stack").concat(Array(options.delete(:class)))
63
+ # base_names = Private.array_value(options.delete(:base) || "square-o").push("stack-2x")
64
+ # names = Private.array_value(names).push("stack-1x")
65
+ # base = fa_icon(base_names, options.delete(:base_options) || {})
66
+ # icon = fa_icon(names, options.delete(:icon_options) || {})
67
+ # icons = [base, icon]
68
+ # icons.reverse! if options.delete(:reverse)
69
+ # text = options.delete(:text)
70
+ # stacked_icon = content_tag(:span, safe_join(icons), options.merge(:class => classes))
71
+ # Private.icon_join(stacked_icon, text)
72
+ # end
73
+
74
+ module Private
75
+ extend ActionView::Helpers::OutputSafetyHelper
76
+
77
+ def self.icon_join(icon, text)
78
+ return icon if text.blank?
79
+ safe_join([icon, ERB::Util.html_escape(text)], " ")
80
+ end
81
+
82
+ def self.icon_names(names = [])
83
+ array_value(names).map { |n| "fa-#{n}" }
84
+ end
85
+
86
+ def self.array_value(value = [])
87
+ value.is_a?(Array) ? value : value.to_s.split(/\s+/)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,92 @@
1
+ module FontAwesome
2
+ module Rails
3
+ module IconHelper
4
+ # Creates an icon tag given an icon name and possible icon
5
+ # modifiers.
6
+ #
7
+ # Examples
8
+ #
9
+ # fa_icon "camera-retro"
10
+ # # => <i class="fa fa-camera-retro"></i>
11
+ #
12
+ # fa_icon "camera-retro", text: "Take a photo"
13
+ # # => <i class="fa fa-camera-retro"></i> Take a photo
14
+ #
15
+ # fa_icon "camera-retro 2x"
16
+ # # => <i class="fa fa-camera-retro fa-2x"></i>
17
+ # fa_icon ["camera-retro", "4x"]
18
+ # # => <i class="fa fa-camera-retro fa-4x"></i>
19
+ # fa_icon "spinner spin lg"
20
+ # # => <i class="fa fa-spinner fa-spin fa-lg">
21
+ #
22
+ # fa_icon "quote-left 4x", class: "pull-left"
23
+ # # => <i class="fa fa-quote-left fa-4x pull-left"></i>
24
+ #
25
+ # fa_icon "user", data: { id: 123 }
26
+ # # => <i class="fa fa-user" data-id="123"></i>
27
+ #
28
+ # content_tag(:li, fa_icon("check li", text: "Bulleted list item"))
29
+ # # => <li><i class="fa fa-check fa-li"></i> Bulleted list item</li>
30
+ def fa_icon(names = "flag", options = {})
31
+ classes = ["fa"]
32
+ classes.concat Private.icon_names(names)
33
+ classes.concat Array(options.delete(:class))
34
+ text = options.delete(:text)
35
+ icon = content_tag(:i, nil, options.merge(:class => classes))
36
+ Private.icon_join(icon, text)
37
+ end
38
+
39
+ # Creates an stack set of icon tags given a base icon name, a main icon
40
+ # name, and possible icon modifiers.
41
+ #
42
+ # Examples
43
+ #
44
+ # fa_stacked_icon "twitter", base: "square-o"
45
+ # # => <span class="fa-stack">
46
+ # # => <i class="fa fa-square-o fa-stack-2x"></i>
47
+ # # => <i class="fa fa-twitter fa-stack-1x"></i>
48
+ # # => </span>
49
+ #
50
+ # fa_stacked_icon "terminal inverse", base: "square", class: "pull-right", text: "Hi!"
51
+ # # => <span class="fa-stack pull-right">
52
+ # # => <i class="fa fa-square fa-stack-2x"></i>
53
+ # # => <i class="fa fa-terminal fa-inverse fa-stack-1x"></i>
54
+ # # => </span> Hi!
55
+ #
56
+ # fa_stacked_icon "camera", base: "ban-circle", reverse: true
57
+ # # => <span class="fa-stack">
58
+ # # => <i class="fa fa-camera fa-stack-1x"></i>
59
+ # # => <i class="fa fa-ban-circle fa-stack-2x"></i>
60
+ # # => </span>
61
+ def fa_stacked_icon(names = "flag", options = {})
62
+ classes = Private.icon_names("stack").concat(Array(options.delete(:class)))
63
+ base_names = Private.array_value(options.delete(:base) || "square-o").push("stack-2x")
64
+ names = Private.array_value(names).push("stack-1x")
65
+ base = fa_icon(base_names, options.delete(:base_options) || {})
66
+ icon = fa_icon(names, options.delete(:icon_options) || {})
67
+ icons = [base, icon]
68
+ icons.reverse! if options.delete(:reverse)
69
+ text = options.delete(:text)
70
+ stacked_icon = content_tag(:span, safe_join(icons), options.merge(:class => classes))
71
+ Private.icon_join(stacked_icon, text)
72
+ end
73
+
74
+ module Private
75
+ extend ActionView::Helpers::OutputSafetyHelper
76
+
77
+ def self.icon_join(icon, text)
78
+ return icon if text.blank?
79
+ safe_join([icon, ERB::Util.html_escape(text)], " ")
80
+ end
81
+
82
+ def self.icon_names(names = [])
83
+ array_value(names).map { |n| "fa-#{n}" }
84
+ end
85
+
86
+ def self.array_value(value = [])
87
+ value.is_a?(Array) ? value : value.to_s.split(/\s+/)
88
+ end
89
+ end
90
+ end
91
+ end
92
+ end
@@ -0,0 +1,4 @@
1
+ require "weather-icons-rails/version"
2
+ require "weather-icons-rails/engine" if defined?(::Rails)
3
+ class WeatherIconsRails
4
+ end
@@ -0,0 +1,4 @@
1
+ module WeatherIconsRails
2
+ class Engine < ::Rails::Engine
3
+ end
4
+ end
@@ -0,0 +1,3 @@
1
+ module WeatherIconsRails
2
+ VERSION = "0.0.1"
3
+ end
@@ -0,0 +1 @@
1
+ @import font-awesome
@@ -0,0 +1 @@
1
+ @import "font-awesome";
@@ -0,0 +1,3 @@
1
+ /*
2
+ *= require font-awesome
3
+ */
@@ -0,0 +1,2 @@
1
+ class PagesController < ActionController::Base
2
+ end
@@ -0,0 +1,3 @@
1
+ <%= fa_icon "flag" %>
2
+
3
+ <%= fa_stacked_icon "twitter", :base => "check-empty" %>
@@ -0,0 +1,4 @@
1
+ # This file is used by Rack-based servers to start the application.
2
+
3
+ require ::File.expand_path('../config/environment', __FILE__)
4
+ run Dummy::Application
@@ -0,0 +1,18 @@
1
+ require File.expand_path('../boot', __FILE__)
2
+
3
+ # require "rails/all"
4
+ require "sprockets/railtie"
5
+
6
+ Bundler.require(:default, :development)
7
+
8
+ module Dummy
9
+ class Application < Rails::Application
10
+ config.encoding = "utf-8"
11
+ config.assets.enabled = true
12
+ config.assets.version = '1.0'
13
+
14
+ # replacement for environments/*.rb
15
+ config.active_support.deprecation = :stderr
16
+ config.eager_load = false
17
+ end
18
+ end
@@ -0,0 +1,10 @@
1
+ require 'rubygems'
2
+ gemfile = File.expand_path('../../../../Gemfile', __FILE__)
3
+
4
+ if File.exist?(gemfile)
5
+ ENV['BUNDLE_GEMFILE'] = gemfile
6
+ require 'bundler'
7
+ Bundler.setup
8
+ end
9
+
10
+ $:.unshift File.expand_path('../../../../lib', __FILE__)
@@ -0,0 +1,5 @@
1
+ # Load the rails application
2
+ require File.expand_path('../application', __FILE__)
3
+
4
+ # Initialize the rails application
5
+ Dummy::Application.initialize!
@@ -0,0 +1,8 @@
1
+ # Be sure to restart your server when you modify this file.
2
+
3
+ # Your secret key for verifying the integrity of signed cookies.
4
+ # If you change this key, all old signed cookies will become invalid!
5
+ # Make sure the secret is at least 30 characters and all random,
6
+ # no regular words or you'll be exposed to dictionary attacks.
7
+ Dummy::Application.config.secret_token = 'deadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeefdeadbeef'
8
+ Dummy::Application.config.secret_key_base = 'deadbeef' if Dummy::Application.config.respond_to?(:secret_key_base)
@@ -0,0 +1,3 @@
1
+ Dummy::Application.routes.draw do
2
+ get "/icons", :to => "pages#icons"
3
+ end
@@ -0,0 +1,65 @@
1
+ require 'test_helper'
2
+
3
+ class FontAwesomeRailsTest < ActionDispatch::IntegrationTest
4
+ teardown { clean_sprockets_cache }
5
+
6
+ test "engine is loaded" do
7
+ assert_equal ::Rails::Engine, FontAwesome::Rails::Engine.superclass
8
+ end
9
+
10
+ test "fonts are served" do
11
+ get "/assets/fontawesome-webfont.eot"
12
+ assert_response :success
13
+ get "/assets/fontawesome-webfont.ttf"
14
+ assert_response :success
15
+ get "/assets/fontawesome-webfont.woff"
16
+ assert_response :success
17
+ end
18
+
19
+ test "stylesheets are served" do
20
+ get "/assets/font-awesome.css"
21
+ assert_font_awesome(response)
22
+ end
23
+
24
+ test "stylesheets contain asset pipeline references to fonts" do
25
+ get "/assets/font-awesome.css"
26
+ assert_match "/assets/fontawesome-webfont.eot", response.body
27
+ assert_match "/assets/fontawesome-webfont.eot?#iefix", response.body
28
+ assert_match "/assets/fontawesome-webfont.woff", response.body
29
+ assert_match "/assets/fontawesome-webfont.ttf", response.body
30
+ assert_match "/assets/fontawesome-webfont.svg#fontawesomeregular", response.body
31
+ end
32
+
33
+ test "stylesheet is available in a css sprockets require" do
34
+ get "/assets/sprockets-require.css"
35
+ assert_font_awesome(response)
36
+ end
37
+
38
+ test "stylesheet is available in a sass import" do
39
+ get "/assets/sass-import.css"
40
+ assert_font_awesome(response)
41
+ end
42
+
43
+ test "stylesheet is available in a scss import" do
44
+ get "/assets/scss-import.css"
45
+ assert_font_awesome(response)
46
+ end
47
+
48
+ test "helpers should be available in the view" do
49
+ get "/icons"
50
+ assert_response :success
51
+ assert_select "i.fa.fa-flag"
52
+ assert_select "span.fa-stack"
53
+ end
54
+
55
+ private
56
+
57
+ def clean_sprockets_cache
58
+ FileUtils.rm_rf File.expand_path("../dummy/tmp", __FILE__)
59
+ end
60
+
61
+ def assert_font_awesome(response)
62
+ assert_response :success
63
+ assert_match(/font-family:\s*'FontAwesome';/, response.body)
64
+ end
65
+ end
@@ -0,0 +1,113 @@
1
+ require 'test_helper'
2
+
3
+ class FontAwesome::Rails::IconHelperTest < ActionView::TestCase
4
+
5
+ test "#fa_icon with no args should render a flag icon" do
6
+ assert_icon i("fa fa-flag")
7
+ end
8
+
9
+ test "#fa_icon should render different individual icons" do
10
+ assert_icon i("fa fa-flag"), "flag"
11
+ assert_icon i("fa fa-camera-retro"), "camera-retro"
12
+ assert_icon i("fa fa-cog"), "cog"
13
+ assert_icon i("fa fa-github"), "github"
14
+ end
15
+
16
+ test "#fa_icon should render icons with multiple modifiers" do
17
+ assert_icon i("fa fa-pencil fa-fixed-width"), "pencil fixed-width"
18
+ assert_icon i("fa fa-flag fa-4x"), "flag 4x"
19
+ assert_icon i("fa fa-refresh fa-2x fa-spin"), "refresh 2x spin"
20
+ end
21
+
22
+ test "#fa_icon should render icons with array modifiers" do
23
+ assert_icon i("fa fa-flag"), ["flag"]
24
+ assert_icon i("fa fa-check fa-li"), ["check", "li"]
25
+ assert_icon i("fa fa-flag fa-4x"), ["flag", "4x"]
26
+ assert_icon i("fa fa-refresh fa-2x fa-spin"), ["refresh", "2x", "spin"]
27
+ end
28
+
29
+ test "#fa_icon should incorporate additional class styles" do
30
+ assert_icon i("fa fa-flag pull-right"), "flag", :class => "pull-right"
31
+ assert_icon i("fa fa-flag fa-2x pull-right"), ["flag", "2x"], :class => ["pull-right"]
32
+ assert_icon i("fa fa-check fa-li pull-right special"), "check li", :class => "pull-right special"
33
+ assert_icon i("fa fa-check pull-right special"), "check", :class => ["pull-right", "special"]
34
+ end
35
+
36
+ test "#fa_icon should incorporate a text suffix" do
37
+ assert_icon "#{i("fa fa-camera-retro")} Take a photo", "camera-retro", :text => "Take a photo"
38
+ end
39
+
40
+ test "#fa_icon should html escape text" do
41
+ assert_icon "#{i("fa fa-camera-retro")} &lt;script&gt;&lt;/script&gt;", "camera-retro", :text => "<script></script>"
42
+ end
43
+
44
+ test "#fa_icon should not html escape safe text" do
45
+ assert_icon "#{i("fa fa-camera-retro")} <script></script>", "camera-retro", :text => "<script></script>".html_safe
46
+ end
47
+
48
+ test "#fa_icon should pull it all together" do
49
+ assert_icon "#{i("fa fa-camera-retro pull-right")} Take a photo", "camera-retro", :text => "Take a photo", :class => "pull-right"
50
+ end
51
+
52
+ test "#fa_icon should pass all other options through" do
53
+ assert_icon %(<i class="fa fa-user" data-id="123"></i>), "user", :data => { :id => 123 }
54
+ end
55
+
56
+ test "#fa_stacked_icon with no args should render a flag icon" do
57
+ expected = %(<span class="fa-stack">#{i("fa fa-square-o fa-stack-2x")}#{i("fa fa-flag fa-stack-1x")}</span>)
58
+ assert_stacked_icon expected
59
+ end
60
+
61
+ test "#fa_stacked_icon should render a stacked icon" do
62
+ expected = %(<span class="fa-stack">#{i("fa fa-square-o fa-stack-2x")}#{i("fa fa-twitter fa-stack-1x")}</span>)
63
+ assert_stacked_icon expected, "twitter", :base => "square-o"
64
+ expected = %(<span class="fa-stack">#{i("fa fa-square fa-stack-2x")}#{i("fa fa-terminal fa-inverse fa-stack-1x")}</span>)
65
+ assert_stacked_icon expected, ["terminal", "inverse"], :base => ["square"]
66
+ end
67
+
68
+ test "#fa_stacked_icon should incorporate additional class styles" do
69
+ expected = %(<span class="fa-stack pull-right">#{i("fa fa-square-o fa-stack-2x")}#{i("fa fa-twitter fa-stack-1x")}</span>)
70
+ assert_stacked_icon expected, "twitter", :base => "square-o", :class => "pull-right"
71
+ end
72
+
73
+ test "#fa_stacked_icon should reverse the stack" do
74
+ expected = %(<span class="fa-stack">#{i("fa fa-facebook fa-stack-1x")}#{i("fa fa-ban fa-stack-2x")}</span>)
75
+ assert_stacked_icon expected, "facebook", :base => "ban", :reverse => "true"
76
+ end
77
+
78
+ test "#fa_stacked_icon should html escape text" do
79
+ expected = %(<span class="fa-stack">#{i("fa fa-check-empty fa-stack-2x")}#{i("fa fa-twitter fa-stack-1x")}</span> &lt;script&gt;)
80
+ assert_stacked_icon expected, "twitter", :base => "check-empty", :text => "<script>"
81
+ end
82
+
83
+ test "#fa_stacked_icon should not html escape safe text" do
84
+ expected = %(<span class="fa-stack">#{i("fa fa-square-o fa-stack-2x")}#{i("fa fa-twitter fa-stack-1x")}</span> <script>)
85
+ assert_stacked_icon expected, "twitter", :base => "square-o", :text => "<script>".html_safe
86
+ end
87
+
88
+ test "#fa_stacked_icon should accept options for base and main icons" do
89
+ expected = %(<span class="fa-stack">#{i("fa fa-camera fa-stack-1x text-info")}#{i("fa fa-ban fa-stack-2x text-error")}</span>)
90
+ assert_stacked_icon expected, "camera", :base => "ban", :reverse => true, :base_options => { :class => "text-error" }, :icon_options => { :class => "text-info" }
91
+ end
92
+
93
+ test "#fa_stacked_icon should pass all other options through" do
94
+ expected = %(<span class="fa-stack" data-id="123">#{i("fa fa-square-o fa-stack-2x")}#{i("fa fa-user fa-stack-1x")}</span>)
95
+ assert_stacked_icon expected, "user", :base => "square-o", :data => { :id => 123 }
96
+ end
97
+
98
+ private
99
+
100
+ def assert_icon(expected, *args)
101
+ message = "`fa_icon(#{args.inspect[1...-1]})` should return `#{expected}`"
102
+ assert_dom_equal expected, fa_icon(*args), message
103
+ end
104
+
105
+ def assert_stacked_icon(expected, *args)
106
+ message = "`fa_stacked_icon(#{args.inspect[1...-1]})` should return `#{expected}`"
107
+ assert_dom_equal expected, fa_stacked_icon(*args), message
108
+ end
109
+
110
+ def i(classes)
111
+ %(<i class="#{classes}"></i>)
112
+ end
113
+ end