fa_rails 0.1.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +7 -0
- data/.gitignore +3 -0
- data/Gemfile +2 -0
- data/Gemfile.lock +44 -0
- data/README.md +18 -0
- data/Rakefile +7 -0
- data/fa_rails.gemspec +22 -0
- data/lib/font_awesome_helper.rb +157 -0
- data/spec/helpers/font_awesome_helper_spec.rb +96 -0
- data/spec/spec_helper.rb +8 -0
- metadata +136 -0
checksums.yaml
ADDED
@@ -0,0 +1,7 @@
|
|
1
|
+
---
|
2
|
+
SHA1:
|
3
|
+
metadata.gz: 0dbacdc4e0a4e68f79cfc68e8bb41711e43300e3
|
4
|
+
data.tar.gz: fec55616a7eaaa224bab094b25268b47d6b025be
|
5
|
+
SHA512:
|
6
|
+
metadata.gz: dd4692e88c2772c21655bd288302fa24f7e0dc89ed2e8ec25b0ba1c69085569ef657f563f498ad8ce18065f404b9a577047fd2c4f2603f724d2dbb9d4443d400
|
7
|
+
data.tar.gz: 25a690db403ab8e3006eca15d0bbc585a92b092a70db0293dc2ee43e6a03debdc8b980661ad8858598cbdac9fe78ffda9802f554b440d50cc1749a48e33354d0
|
data/.gitignore
ADDED
data/Gemfile
ADDED
data/Gemfile.lock
ADDED
@@ -0,0 +1,44 @@
|
|
1
|
+
PATH
|
2
|
+
remote: .
|
3
|
+
specs:
|
4
|
+
fa_rails (0.1.0)
|
5
|
+
file_utils (~> 1.1, >= 1.1.2)
|
6
|
+
|
7
|
+
GEM
|
8
|
+
remote: https://rubygems.org/
|
9
|
+
specs:
|
10
|
+
diff-lcs (1.3)
|
11
|
+
docile (1.3.1)
|
12
|
+
file_utils (1.1.2)
|
13
|
+
json (2.1.0)
|
14
|
+
rake (12.3.1)
|
15
|
+
rspec (3.7.0)
|
16
|
+
rspec-core (~> 3.7.0)
|
17
|
+
rspec-expectations (~> 3.7.0)
|
18
|
+
rspec-mocks (~> 3.7.0)
|
19
|
+
rspec-core (3.7.1)
|
20
|
+
rspec-support (~> 3.7.0)
|
21
|
+
rspec-expectations (3.7.0)
|
22
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
23
|
+
rspec-support (~> 3.7.0)
|
24
|
+
rspec-mocks (3.7.0)
|
25
|
+
diff-lcs (>= 1.2.0, < 2.0)
|
26
|
+
rspec-support (~> 3.7.0)
|
27
|
+
rspec-support (3.7.1)
|
28
|
+
simplecov (0.16.1)
|
29
|
+
docile (~> 1.1)
|
30
|
+
json (>= 1.8, < 3)
|
31
|
+
simplecov-html (~> 0.10.0)
|
32
|
+
simplecov-html (0.10.2)
|
33
|
+
|
34
|
+
PLATFORMS
|
35
|
+
ruby
|
36
|
+
|
37
|
+
DEPENDENCIES
|
38
|
+
fa_rails!
|
39
|
+
rake (~> 12.2, >= 12.2.1)
|
40
|
+
rspec (~> 3.7, >= 3.7.0)
|
41
|
+
simplecov (~> 0.15, >= 0.15.1)
|
42
|
+
|
43
|
+
BUNDLED WITH
|
44
|
+
1.16.0
|
data/README.md
ADDED
@@ -0,0 +1,18 @@
|
|
1
|
+
# FA Rails
|
2
|
+
|
3
|
+
A quick helper for using FontAwesome icons in Rails.
|
4
|
+
|
5
|
+
## Installation
|
6
|
+
|
7
|
+
**This gem is just the code for using FontAwesome in your Rails applications.**
|
8
|
+
|
9
|
+
You must still have your own FontAwesome Pro license, or install the
|
10
|
+
[Free](https://use.fontawesome.com/releases/v5.2.0/fontawesome-free-5.2.0-web.zip)
|
11
|
+
package.
|
12
|
+
|
13
|
+
Copy the
|
14
|
+
[web download files](https://fontawesome.com/releases/5.2.0/web/download) to the
|
15
|
+
appropriate locations in your app:
|
16
|
+
|
17
|
+
- `js` => `app/asets/javascripts`
|
18
|
+
- `css` => `app/assets/stylesheets`
|
data/Rakefile
ADDED
data/fa_rails.gemspec
ADDED
@@ -0,0 +1,22 @@
|
|
1
|
+
Gem::Specification.new do |s|
|
2
|
+
s.name = 'fa_rails'
|
3
|
+
s.version = '0.1.0'
|
4
|
+
s.date = '2018-07-27'
|
5
|
+
s.summary = 'FontAwesome helper for Rails'
|
6
|
+
s.description = 'A helper module for using FontAwesome icons in Rails.'
|
7
|
+
s.homepage = 'http://rubygems.org/gems/fa_rails'
|
8
|
+
s.license = 'GPL-3.0'
|
9
|
+
s.authors = ['Julian Fiander']
|
10
|
+
s.email = 'julian@fiander.one'
|
11
|
+
s.require_paths = %w[lib spec doc]
|
12
|
+
s.files = `git ls-files`.split("\n")
|
13
|
+
s.test_files = `git ls-files -- spec/*`.split("\n")
|
14
|
+
|
15
|
+
s.required_ruby_version = '~> 2.4'
|
16
|
+
|
17
|
+
s.add_runtime_dependency 'file_utils', '~> 1.1', '>= 1.1.2'
|
18
|
+
|
19
|
+
s.add_development_dependency 'rake', '~> 12.2', '>= 12.2.1'
|
20
|
+
s.add_development_dependency 'rspec', '~> 3.7', '>= 3.7.0'
|
21
|
+
s.add_development_dependency 'simplecov', '~> 0.15', '>= 0.15.1'
|
22
|
+
end
|
@@ -0,0 +1,157 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
# FontAwesome 5 (Pro) Helper
|
4
|
+
module FontAwesomeHelper
|
5
|
+
# html_safe: No user content
|
6
|
+
def fa_layer(icons = {}, title: nil, grow: 0, css: '')
|
7
|
+
# Add icons to the stack bottom to top
|
8
|
+
#
|
9
|
+
# Note: scaling ounters does not work well with :grow, so should use the
|
10
|
+
# older "fa-3x" syntax in :css instead.
|
11
|
+
span_top = "<span class='icon fa-layers fa-fw #{css}' title='#{title}'>"
|
12
|
+
span_bottom = '</span>'
|
13
|
+
|
14
|
+
icons.each { |i| i[:options] = combine_options(i, combine_grows(i, grow)) }
|
15
|
+
|
16
|
+
output = span_top + parse_all(icons).join + span_bottom
|
17
|
+
safe_output(output)
|
18
|
+
end
|
19
|
+
|
20
|
+
def fa_icon(fa, options = {})
|
21
|
+
if fa.is_a?(Hash)
|
22
|
+
name = fa[:name]
|
23
|
+
options = fa[:options]
|
24
|
+
elsif fa.is_a?(String) || fa.is_a?(Symbol)
|
25
|
+
name = fa.to_s
|
26
|
+
else
|
27
|
+
raise ArgumentError, 'Unexpected argument type.'
|
28
|
+
end
|
29
|
+
safe_output(parse_icon(name, options))
|
30
|
+
end
|
31
|
+
|
32
|
+
def fa_span(fa, text = '', options = {})
|
33
|
+
if fa.is_a?(Hash)
|
34
|
+
type = fa[:type].to_sym
|
35
|
+
text = fa[:text]
|
36
|
+
options = fa[:options]
|
37
|
+
elsif fa.is_a?(String) || fa.is_a?(Symbol)
|
38
|
+
type = fa.to_s
|
39
|
+
else
|
40
|
+
raise ArgumentError, 'Unexpected argument type.'
|
41
|
+
end
|
42
|
+
safe_output(parse_span(type, text, options))
|
43
|
+
end
|
44
|
+
|
45
|
+
private
|
46
|
+
|
47
|
+
def safe_output(output)
|
48
|
+
output.respond_to?(:html_safe) ? output.html_safe : output
|
49
|
+
end
|
50
|
+
|
51
|
+
def parse_all(icons)
|
52
|
+
icons.map do |icon|
|
53
|
+
name = icon[:name]
|
54
|
+
options = icon[:options] || {}
|
55
|
+
|
56
|
+
if %i[counter text].include?(name)
|
57
|
+
parse_span(name, icon[:text], options)
|
58
|
+
else
|
59
|
+
parse_icon(name, options)
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def parse_icon(name, options = {})
|
65
|
+
options = fa_options(options)
|
66
|
+
parse_options(options)
|
67
|
+
title = options[:title]
|
68
|
+
|
69
|
+
@classes << "fa-#{name}"
|
70
|
+
@classes << "fa-#{size_x(options[:size])}" if !!options[:size]
|
71
|
+
css = @classes.flatten.join(' ')
|
72
|
+
transforms = @transforms.join(' ')
|
73
|
+
|
74
|
+
"<i class='#{css}' data-fa-transform='#{transforms}' title='#{title}'></i>"
|
75
|
+
end
|
76
|
+
|
77
|
+
def parse_span(type, text, options = {})
|
78
|
+
options.delete(:style)
|
79
|
+
options = fa_options(options)
|
80
|
+
parse_options(options)
|
81
|
+
pos = options.delete(:position)
|
82
|
+
position = long_position(pos) if !!pos
|
83
|
+
|
84
|
+
@classes << "fa-layers-#{type}"
|
85
|
+
@classes << (!!position ? "fa-layers-#{position}" : '')
|
86
|
+
css = @classes.flatten.reject { |c| c.to_s.match?(/^fa.$/) }.join(' ')
|
87
|
+
transforms = @transforms.join(' ')
|
88
|
+
|
89
|
+
"<span class='#{css}' data-fa-transform='#{transforms}'>#{text}</span>"
|
90
|
+
end
|
91
|
+
|
92
|
+
def fa_options(options)
|
93
|
+
default = { style: :solid, css: '', fa: '' }
|
94
|
+
|
95
|
+
default.merge(options.to_h)
|
96
|
+
end
|
97
|
+
|
98
|
+
def combine_grows(i, grow)
|
99
|
+
if i.key?(:options) && i[:options].key?(:grow)
|
100
|
+
i[:options][:grow] + grow
|
101
|
+
else
|
102
|
+
grow
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def combine_options(i, combined_grow)
|
107
|
+
if i.key?(:options)
|
108
|
+
i[:options].merge(grow: combined_grow)
|
109
|
+
else
|
110
|
+
{ grow: combined_grow }
|
111
|
+
end
|
112
|
+
end
|
113
|
+
|
114
|
+
def size_x(size)
|
115
|
+
return '' unless !!size || size == 1
|
116
|
+
"#{size}x"
|
117
|
+
end
|
118
|
+
|
119
|
+
def long_position(position)
|
120
|
+
return 'top-right' if position == :tr
|
121
|
+
return 'top-left' if position == :tl
|
122
|
+
return 'bottom-right' if position == :br
|
123
|
+
return 'bottom-left' if position == :bl
|
124
|
+
end
|
125
|
+
|
126
|
+
def parse_options(options)
|
127
|
+
parse_classes(options)
|
128
|
+
parse_transforms(options)
|
129
|
+
end
|
130
|
+
|
131
|
+
def parse_classes(options)
|
132
|
+
@classes = []
|
133
|
+
@classes << parse_style(options[:style])
|
134
|
+
@classes << options[:fa].to_s.split(' ').map { |c| "fa-#{c}" }
|
135
|
+
@classes << options[:css].to_s.split(' ')
|
136
|
+
end
|
137
|
+
|
138
|
+
def parse_transforms(options)
|
139
|
+
@transforms = []
|
140
|
+
%i[grow shrink rotate up down left right].each do |transform|
|
141
|
+
if !!options[transform]
|
142
|
+
@transforms << "#{transform}-#{options[transform]}"
|
143
|
+
end
|
144
|
+
end
|
145
|
+
end
|
146
|
+
|
147
|
+
def parse_style(style)
|
148
|
+
return 'fas' unless %i[solid regular light brands].include?(style)
|
149
|
+
|
150
|
+
'fa' + {
|
151
|
+
solid: 's',
|
152
|
+
regular: 'r',
|
153
|
+
light: 'l',
|
154
|
+
brands: 'b'
|
155
|
+
}[style]
|
156
|
+
end
|
157
|
+
end
|
@@ -0,0 +1,96 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
require 'font_awesome_helper'
|
4
|
+
|
5
|
+
RSpec.describe do
|
6
|
+
include FontAwesomeHelper
|
7
|
+
|
8
|
+
describe 'icon' do
|
9
|
+
it 'should generate the correct icon from a string or symbol name' do
|
10
|
+
expect(fa_icon('help')).to eql(
|
11
|
+
"<i class='fas fa-help' data-fa-transform='' title=''></i>"
|
12
|
+
)
|
13
|
+
|
14
|
+
expect(fa_icon(:help)).to eql(
|
15
|
+
"<i class='fas fa-help' data-fa-transform='' title=''></i>"
|
16
|
+
)
|
17
|
+
end
|
18
|
+
|
19
|
+
it 'should generate the correct icon from a configuration hash' do
|
20
|
+
fa = { name: 'help', options: { style: :light, size: 2 } }
|
21
|
+
expect(fa_icon(fa)).to eql(
|
22
|
+
"<i class='fal fa-help fa-2x' data-fa-transform='' title=''></i>"
|
23
|
+
)
|
24
|
+
end
|
25
|
+
|
26
|
+
it 'should raise ArgumentError for other input types' do
|
27
|
+
[nil, [], 0].each do |fa|
|
28
|
+
expect { fa_icon(fa) }.to raise_error(
|
29
|
+
ArgumentError, 'Unexpected argument type.'
|
30
|
+
)
|
31
|
+
end
|
32
|
+
end
|
33
|
+
|
34
|
+
it 'should generate the correct brand icon' do
|
35
|
+
expect(fa_icon(:github, style: :brands)).to eql(
|
36
|
+
"<i class='fab fa-github' data-fa-transform='' title=''></i>"
|
37
|
+
)
|
38
|
+
end
|
39
|
+
end
|
40
|
+
|
41
|
+
describe 'layer' do
|
42
|
+
it 'should generate the correct layer from string or symbol names' do
|
43
|
+
icons = [
|
44
|
+
{ name: :square },
|
45
|
+
{ name: :circle, options: { grow: 1 } },
|
46
|
+
{ name: 'exclamation', options: { style: :regular } }
|
47
|
+
]
|
48
|
+
|
49
|
+
expect(fa_layer(icons, grow: 2)).to eql(
|
50
|
+
"<span class='icon fa-layers fa-fw ' title=''>" \
|
51
|
+
"<i class='fas fa-square' data-fa-transform='grow-2' title=''></i>" \
|
52
|
+
"<i class='fas fa-circle' data-fa-transform='grow-3' title=''></i>" \
|
53
|
+
"<i class='far fa-exclamation' data-fa-transform='grow-2' title=''>" \
|
54
|
+
'</i></span>'
|
55
|
+
)
|
56
|
+
end
|
57
|
+
|
58
|
+
it 'should generate the correct layer with a span' do
|
59
|
+
icons = [
|
60
|
+
{ name: :square },
|
61
|
+
{ name: :counter, text: 17, options: { position: :tl } }
|
62
|
+
]
|
63
|
+
|
64
|
+
expect(fa_layer(icons)).to eql(
|
65
|
+
"<span class='icon fa-layers fa-fw ' title=''>" \
|
66
|
+
"<i class='fas fa-square' data-fa-transform='grow-0' title=''></i>" \
|
67
|
+
"<span class='fa-layers-counter fa-layers-top-left' " \
|
68
|
+
"data-fa-transform='grow-0'>17</span></span>"
|
69
|
+
)
|
70
|
+
end
|
71
|
+
end
|
72
|
+
|
73
|
+
describe 'span' do
|
74
|
+
it 'should generate the correct span from a string or symbol type' do
|
75
|
+
expect(fa_span(:text, 'Hello')).to eql(
|
76
|
+
"<span class='fa-layers-text ' data-fa-transform=''>Hello</span>"
|
77
|
+
)
|
78
|
+
end
|
79
|
+
|
80
|
+
it 'should generate the correct span from a configuration hash' do
|
81
|
+
span = { type: :text, text: 'World', options: { position: :bl } }
|
82
|
+
expect(fa_span(span)).to eql(
|
83
|
+
"<span class='fa-layers-text fa-layers-bottom-left' " \
|
84
|
+
"data-fa-transform=''>World</span>"
|
85
|
+
)
|
86
|
+
end
|
87
|
+
|
88
|
+
it 'should raise ArgumentError for other input types' do
|
89
|
+
[nil, [], 0].each do |fa|
|
90
|
+
expect { fa_span(fa) }.to raise_error(
|
91
|
+
ArgumentError, 'Unexpected argument type.'
|
92
|
+
)
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|
96
|
+
end
|
data/spec/spec_helper.rb
ADDED
metadata
ADDED
@@ -0,0 +1,136 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: fa_rails
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.0
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Julian Fiander
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
date: 2018-07-27 00:00:00.000000000 Z
|
12
|
+
dependencies:
|
13
|
+
- !ruby/object:Gem::Dependency
|
14
|
+
name: file_utils
|
15
|
+
requirement: !ruby/object:Gem::Requirement
|
16
|
+
requirements:
|
17
|
+
- - "~>"
|
18
|
+
- !ruby/object:Gem::Version
|
19
|
+
version: '1.1'
|
20
|
+
- - ">="
|
21
|
+
- !ruby/object:Gem::Version
|
22
|
+
version: 1.1.2
|
23
|
+
type: :runtime
|
24
|
+
prerelease: false
|
25
|
+
version_requirements: !ruby/object:Gem::Requirement
|
26
|
+
requirements:
|
27
|
+
- - "~>"
|
28
|
+
- !ruby/object:Gem::Version
|
29
|
+
version: '1.1'
|
30
|
+
- - ">="
|
31
|
+
- !ruby/object:Gem::Version
|
32
|
+
version: 1.1.2
|
33
|
+
- !ruby/object:Gem::Dependency
|
34
|
+
name: rake
|
35
|
+
requirement: !ruby/object:Gem::Requirement
|
36
|
+
requirements:
|
37
|
+
- - "~>"
|
38
|
+
- !ruby/object:Gem::Version
|
39
|
+
version: '12.2'
|
40
|
+
- - ">="
|
41
|
+
- !ruby/object:Gem::Version
|
42
|
+
version: 12.2.1
|
43
|
+
type: :development
|
44
|
+
prerelease: false
|
45
|
+
version_requirements: !ruby/object:Gem::Requirement
|
46
|
+
requirements:
|
47
|
+
- - "~>"
|
48
|
+
- !ruby/object:Gem::Version
|
49
|
+
version: '12.2'
|
50
|
+
- - ">="
|
51
|
+
- !ruby/object:Gem::Version
|
52
|
+
version: 12.2.1
|
53
|
+
- !ruby/object:Gem::Dependency
|
54
|
+
name: rspec
|
55
|
+
requirement: !ruby/object:Gem::Requirement
|
56
|
+
requirements:
|
57
|
+
- - "~>"
|
58
|
+
- !ruby/object:Gem::Version
|
59
|
+
version: '3.7'
|
60
|
+
- - ">="
|
61
|
+
- !ruby/object:Gem::Version
|
62
|
+
version: 3.7.0
|
63
|
+
type: :development
|
64
|
+
prerelease: false
|
65
|
+
version_requirements: !ruby/object:Gem::Requirement
|
66
|
+
requirements:
|
67
|
+
- - "~>"
|
68
|
+
- !ruby/object:Gem::Version
|
69
|
+
version: '3.7'
|
70
|
+
- - ">="
|
71
|
+
- !ruby/object:Gem::Version
|
72
|
+
version: 3.7.0
|
73
|
+
- !ruby/object:Gem::Dependency
|
74
|
+
name: simplecov
|
75
|
+
requirement: !ruby/object:Gem::Requirement
|
76
|
+
requirements:
|
77
|
+
- - "~>"
|
78
|
+
- !ruby/object:Gem::Version
|
79
|
+
version: '0.15'
|
80
|
+
- - ">="
|
81
|
+
- !ruby/object:Gem::Version
|
82
|
+
version: 0.15.1
|
83
|
+
type: :development
|
84
|
+
prerelease: false
|
85
|
+
version_requirements: !ruby/object:Gem::Requirement
|
86
|
+
requirements:
|
87
|
+
- - "~>"
|
88
|
+
- !ruby/object:Gem::Version
|
89
|
+
version: '0.15'
|
90
|
+
- - ">="
|
91
|
+
- !ruby/object:Gem::Version
|
92
|
+
version: 0.15.1
|
93
|
+
description: A helper module for using FontAwesome icons in Rails.
|
94
|
+
email: julian@fiander.one
|
95
|
+
executables: []
|
96
|
+
extensions: []
|
97
|
+
extra_rdoc_files: []
|
98
|
+
files:
|
99
|
+
- ".gitignore"
|
100
|
+
- Gemfile
|
101
|
+
- Gemfile.lock
|
102
|
+
- README.md
|
103
|
+
- Rakefile
|
104
|
+
- fa_rails.gemspec
|
105
|
+
- lib/font_awesome_helper.rb
|
106
|
+
- spec/helpers/font_awesome_helper_spec.rb
|
107
|
+
- spec/spec_helper.rb
|
108
|
+
homepage: http://rubygems.org/gems/fa_rails
|
109
|
+
licenses:
|
110
|
+
- GPL-3.0
|
111
|
+
metadata: {}
|
112
|
+
post_install_message:
|
113
|
+
rdoc_options: []
|
114
|
+
require_paths:
|
115
|
+
- lib
|
116
|
+
- spec
|
117
|
+
- doc
|
118
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
119
|
+
requirements:
|
120
|
+
- - "~>"
|
121
|
+
- !ruby/object:Gem::Version
|
122
|
+
version: '2.4'
|
123
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
124
|
+
requirements:
|
125
|
+
- - ">="
|
126
|
+
- !ruby/object:Gem::Version
|
127
|
+
version: '0'
|
128
|
+
requirements: []
|
129
|
+
rubyforge_project:
|
130
|
+
rubygems_version: 2.6.11
|
131
|
+
signing_key:
|
132
|
+
specification_version: 4
|
133
|
+
summary: FontAwesome helper for Rails
|
134
|
+
test_files:
|
135
|
+
- spec/helpers/font_awesome_helper_spec.rb
|
136
|
+
- spec/spec_helper.rb
|