fa_rails 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA1:
3
- metadata.gz: 426ebafa62f8215649caebd5b5a032a8e6811edb
4
- data.tar.gz: 78512e22aa49f1869bae9b3772b1b27680c4663d
3
+ metadata.gz: 0fd53c5e9810de9d7364425d8928e032741a5df3
4
+ data.tar.gz: adb052186b5ab4bedd77e27f43cc5e6d973a8915
5
5
  SHA512:
6
- metadata.gz: c51c668fbe28857e2e6f4fd4c9b1bbffa3844b0072109f66d71dfbcf6edbc1e433c84fe40cacff2c5e2f4cd4919c2771f33de261051daa58758dc43b5355be67
7
- data.tar.gz: 51980ca1a7a4be0e919c1d38eae5989bc33c0a2b394cc8b6fef765ac1d7f23a11019a3678a159744ce1c6508d294b652691bd6789a94afbd2969b006b42336c5
6
+ metadata.gz: e11ee40165dd680bc382c66cc2e9d2e62ba81c8e622a336b1a016c4f956712ed873791f6f1e190212058c9c5068ae7fc01bae148a856659ff3e2c375c47f0649
7
+ data.tar.gz: 02531dc100ff594a87528bf015dccee154d53bf5ba56b0418d2219b8bb6ab487eaa126331082b39a5bf892a189be741f5743f9da17047740934642c154d1cf58
data/Gemfile.lock CHANGED
@@ -1,7 +1,7 @@
1
1
  PATH
2
2
  remote: .
3
3
  specs:
4
- fa_rails (0.1.0)
4
+ fa_rails (0.1.2)
5
5
  file_utils (~> 1.1, >= 1.1.2)
6
6
 
7
7
  GEM
data/fa_rails.gemspec CHANGED
@@ -1,6 +1,6 @@
1
1
  Gem::Specification.new do |s|
2
2
  s.name = 'fa_rails'
3
- s.version = '0.1.1'
3
+ s.version = '0.1.2'
4
4
  s.date = '2018-07-27'
5
5
  s.summary = 'FontAwesome helper for Rails'
6
6
  s.description = 'A helper module for using FontAwesome icons in Rails.'
data/lib/fa.rb CHANGED
@@ -1,130 +1,127 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  # FontAwesome 5 (Pro) Helper
4
- class FA
5
- require 'fa/icon'
6
- require 'fa/span'
7
- require 'fa/layer'
8
-
9
- def raw
10
- #
11
- end
4
+ module FA
5
+ class Base
6
+ require 'fa/icon'
7
+ require 'fa/span'
8
+ require 'fa/layer'
9
+
10
+ def raw
11
+ #
12
+ end
12
13
 
13
- def safe
14
- safe_output(raw)
15
- end
14
+ def safe
15
+ safe_output(raw)
16
+ end
16
17
 
17
- private
18
+ private
18
19
 
19
- def safe_output(output)
20
- # html_safe: No user content
21
- output.respond_to?(:html_safe) ? output.html_safe : output
22
- end
20
+ def safe_output(output)
21
+ # html_safe: No user content
22
+ output.respond_to?(:html_safe) ? output.html_safe : output
23
+ end
23
24
 
24
- def parse_all(icons)
25
- icons.map do |icon|
26
- name = icon[:name]
27
- options = icon[:options] || {}
25
+ def parse_all(icons)
26
+ icons.map do |icon|
27
+ name = icon[:name]
28
+ options = icon[:options] || {}
28
29
 
29
- if %i[counter text].include?(name)
30
- parse_span(name, icon[:text], options)
31
- else
32
- parse_icon(name, options)
30
+ if %i[counter text].include?(name)
31
+ parse_span(name, icon[:text], options)
32
+ else
33
+ parse_icon(name, options)
34
+ end
33
35
  end
34
36
  end
35
- end
36
37
 
37
- def parse_icon(name, options = {})
38
- options = fa_options(options)
39
- parse_options(options)
40
- title = options[:title]
38
+ def parse_icon(name, options = {})
39
+ options = fa_options(options)
40
+ parse_options(options)
41
+ title = options[:title]
41
42
 
42
- @classes << "fa-#{name}"
43
- @classes << "fa-#{size_x(options[:size])}" if !!options[:size]
44
- css = @classes.flatten.join(' ')
45
- transforms = @transforms.join(' ')
43
+ @classes << "fa-#{name}"
44
+ @classes << "fa-#{size_x(options[:size])}" if !!options[:size]
45
+ css = @classes.flatten.join(' ')
46
+ transforms = @transforms.join(' ')
46
47
 
47
- "<i class='#{css}' data-fa-transform='#{transforms}' title='#{title}'></i>"
48
- end
48
+ "<i class='#{css}' data-fa-transform='#{transforms}' title='#{title}'></i>"
49
+ end
49
50
 
50
- def parse_span(type, text, options = {})
51
- options.delete(:style)
52
- options = fa_options(options)
53
- parse_options(options)
54
- pos = options.delete(:position)
55
- position = long_position(pos) if !!pos
51
+ def parse_span(type, text, options = {})
52
+ options.delete(:style)
53
+ options = fa_options(options)
54
+ parse_options(options)
55
+ pos = options.delete(:position)
56
+ position = long_position(pos) if !!pos
56
57
 
57
- @classes << "fa-layers-#{type}"
58
- @classes << (!!position ? "fa-layers-#{position}" : '')
59
- css = @classes.flatten.reject { |c| c.to_s.match?(/^fa.$/) }.join(' ')
60
- transforms = @transforms.join(' ')
58
+ @classes << "fa-layers-#{type}"
59
+ @classes << (!!position ? "fa-layers-#{position}" : '')
60
+ css = @classes.flatten.reject { |c| c.to_s.match?(/^fa.$/) }.join(' ')
61
+ transforms = @transforms.join(' ')
61
62
 
62
- "<span class='#{css}' data-fa-transform='#{transforms}'>#{text}</span>"
63
- end
63
+ "<span class='#{css}' data-fa-transform='#{transforms}'>#{text}</span>"
64
+ end
64
65
 
65
- def fa_options(options)
66
- default = { style: :solid, css: '', fa: '' }
66
+ def fa_options(options)
67
+ default = { style: :solid, css: '', fa: '' }
67
68
 
68
- default.merge(options.to_h)
69
- end
69
+ default.merge(options.to_h)
70
+ end
70
71
 
71
- def combine_grows(i, grow)
72
- if i.key?(:options) && i[:options].key?(:grow)
73
- i[:options][:grow] + grow
74
- else
75
- grow
72
+ def combine_grows(i, grow)
73
+ if i.key?(:options) && i[:options].key?(:grow)
74
+ i[:options][:grow] + grow
75
+ else
76
+ grow
77
+ end
76
78
  end
77
- end
78
79
 
79
- def combine_options(i, combined_grow)
80
- if i.key?(:options)
81
- i[:options].merge(grow: combined_grow)
82
- else
83
- { grow: combined_grow }
80
+ def combine_options(i, combined_grow)
81
+ if i.key?(:options)
82
+ i[:options].merge(grow: combined_grow)
83
+ else
84
+ { grow: combined_grow }
85
+ end
84
86
  end
85
- end
86
87
 
87
- def size_x(size)
88
- return '' unless !!size || size == 1
89
- "#{size}x"
90
- end
88
+ def size_x(size)
89
+ return '' unless !!size || size == 1
90
+ "#{size}x"
91
+ end
91
92
 
92
- def long_position(position)
93
- return 'top-right' if position == :tr
94
- return 'top-left' if position == :tl
95
- return 'bottom-right' if position == :br
96
- return 'bottom-left' if position == :bl
97
- end
93
+ def long_position(position)
94
+ return 'top-right' if position == :tr
95
+ return 'top-left' if position == :tl
96
+ return 'bottom-right' if position == :br
97
+ return 'bottom-left' if position == :bl
98
+ end
98
99
 
99
- def parse_options(options)
100
- parse_classes(options)
101
- parse_transforms(options)
102
- end
100
+ def parse_options(options)
101
+ parse_classes(options)
102
+ parse_transforms(options)
103
+ end
103
104
 
104
- def parse_classes(options)
105
- @classes = []
106
- @classes << parse_style(options[:style])
107
- @classes << options[:fa].to_s.split(' ').map { |c| "fa-#{c}" }
108
- @classes << options[:css].to_s.split(' ')
109
- end
105
+ def parse_classes(options)
106
+ @classes = []
107
+ @classes << parse_style(options[:style])
108
+ @classes << options[:fa].to_s.split(' ').map { |c| "fa-#{c}" }
109
+ @classes << options[:css].to_s.split(' ')
110
+ end
110
111
 
111
- def parse_transforms(options)
112
- @transforms = []
113
- %i[grow shrink rotate up down left right].each do |transform|
114
- if !!options[transform]
115
- @transforms << "#{transform}-#{options[transform]}"
112
+ def parse_transforms(options)
113
+ @transforms = []
114
+ %i[grow shrink rotate up down left right].each do |transform|
115
+ if !!options[transform]
116
+ @transforms << "#{transform}-#{options[transform]}"
117
+ end
116
118
  end
117
119
  end
118
- end
119
120
 
120
- def parse_style(style)
121
- return 'fas' unless %i[solid regular light brands].include?(style)
121
+ def parse_style(style)
122
+ return 'fas' unless %i[solid regular light brands].include?(style)
122
123
 
123
- 'fa' + {
124
- solid: 's',
125
- regular: 'r',
126
- light: 'l',
127
- brands: 'b'
128
- }[style]
124
+ 'fa' + { solid: 's', regular: 'r', light: 'l', brands: 'b' }[style]
125
+ end
129
126
  end
130
127
  end
data/lib/fa/icon.rb CHANGED
@@ -1,19 +1,22 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class FA::Icon < FA
4
- def initialize(fa, options = {})
5
- @options = options
6
- if fa.is_a?(Hash)
7
- @name = fa[:name]
8
- @options = fa[:options]
9
- elsif fa.is_a?(String) || fa.is_a?(Symbol)
10
- @name = fa.to_s
11
- else
12
- raise ArgumentError, 'Unexpected argument type.'
3
+ module FA
4
+ # FontAwesome 5 (Pro) Helper for generating icons
5
+ class Icon < FA::Base
6
+ def initialize(fa, options = {})
7
+ @options = options
8
+ if fa.is_a?(Hash)
9
+ @name = fa[:name]
10
+ @options = fa[:options]
11
+ elsif fa.is_a?(String) || fa.is_a?(Symbol)
12
+ @name = fa.to_s
13
+ else
14
+ raise ArgumentError, 'Unexpected argument type.'
15
+ end
13
16
  end
14
- end
15
17
 
16
- def raw
17
- parse_icon(@name, @options)
18
+ def raw
19
+ parse_icon(@name, @options)
20
+ end
18
21
  end
19
22
  end
data/lib/fa/layer.rb CHANGED
@@ -1,20 +1,23 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class FA::Layer < FA
4
- def initialize(icons = {}, title: nil, grow: 0, css: '')
5
- # Add icons to the stack bottom to top
6
- #
7
- # Note: scaling ounters does not work well with :grow, so should use the
8
- # older "fa-3x" syntax in :css instead.
9
- span_top = "<span class='icon fa-layers fa-fw #{css}' title='#{title}'>"
10
- span_bottom = '</span>'
3
+ module FA
4
+ # FontAwesome 5 (Pro) Helper for generating layered icons and spans
5
+ class Layer < FA::Base
6
+ def initialize(icons = {}, title: nil, grow: 0, css: '')
7
+ # Add icons to the stack bottom to top
8
+ #
9
+ # Note: scaling counters 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>'
11
13
 
12
- icons.each { |i| i[:options] = combine_options(i, combine_grows(i, grow)) }
14
+ icons.each { |i| i[:options] = combine_options(i, combine_grows(i, grow)) }
13
15
 
14
- @output = span_top + parse_all(icons).join + span_bottom
15
- end
16
+ @output = span_top + parse_all(icons).join + span_bottom
17
+ end
16
18
 
17
- def raw
18
- @output
19
+ def raw
20
+ @output
21
+ end
19
22
  end
20
23
  end
data/lib/fa/span.rb CHANGED
@@ -1,20 +1,28 @@
1
1
  # frozen_string_literal: true
2
2
 
3
- class FA::Span < FA
4
- def initialize(fa, text = '', options = {})
5
- @options = options
6
- if fa.is_a?(Hash)
7
- @type = fa[:type].to_sym
8
- @text = fa[:text]
9
- @options = fa[:options]
10
- elsif fa.is_a?(String) || fa.is_a?(Symbol)
11
- @type = fa.to_s
12
- else
13
- raise ArgumentError, 'Unexpected argument type.'
3
+ module FA
4
+ # FontAwesome 5 (Pro) Helper for generating spans (counters and text)
5
+ class Span < FA::Base
6
+ def initialize(fa, text = '', options = {})
7
+ if fa.is_a?(Hash)
8
+ set_options(fa[:type].to_sym, fa[:text], fa[:options])
9
+ elsif fa.is_a?(String) || fa.is_a?(Symbol)
10
+ set_options(fa.to_s, text, options)
11
+ else
12
+ raise ArgumentError, 'Unexpected argument type.'
13
+ end
14
+ end
15
+
16
+ def raw
17
+ parse_span(@type, @text, @options)
14
18
  end
15
- end
16
19
 
17
- def raw
18
- parse_span(@type, @text, @options)
20
+ private
21
+
22
+ def set_options(type, text, options)
23
+ @type = type
24
+ @text = text
25
+ @options = options
26
+ end
19
27
  end
20
28
  end
@@ -1,34 +1,36 @@
1
1
  # frozen_string_literal: true
2
2
 
3
+ require 'spec_helper'
4
+
3
5
  RSpec.describe FA do
4
6
  describe 'icon' do
5
7
  it 'should generate the correct icon from a string or symbol name' do
6
- expect(FA::Icon.new('help')).to eql(
8
+ expect(FA::Icon.new('help').safe).to eql(
7
9
  "<i class='fas fa-help' data-fa-transform='' title=''></i>"
8
10
  )
9
11
 
10
- expect(FA::Icon.new(:help)).to eql(
12
+ expect(FA::Icon.new(:help).safe).to eql(
11
13
  "<i class='fas fa-help' data-fa-transform='' title=''></i>"
12
14
  )
13
15
  end
14
16
 
15
17
  it 'should generate the correct icon from a configuration hash' do
16
18
  fa = { name: 'help', options: { style: :light, size: 2 } }
17
- expect(FA::Icon.new(fa)).to eql(
19
+ expect(FA::Icon.new(fa).safe).to eql(
18
20
  "<i class='fal fa-help fa-2x' data-fa-transform='' title=''></i>"
19
21
  )
20
22
  end
21
23
 
22
24
  it 'should raise ArgumentError for other input types' do
23
25
  [nil, [], 0].each do |fa|
24
- expect { FA::Icon.new(fa) }.to raise_error(
26
+ expect { FA::Icon.new(fa).safe }.to raise_error(
25
27
  ArgumentError, 'Unexpected argument type.'
26
28
  )
27
29
  end
28
30
  end
29
31
 
30
32
  it 'should generate the correct brand icon' do
31
- expect(FA::Icon.new(:github, style: :brands)).to eql(
33
+ expect(FA::Icon.new(:github, style: :brands).safe).to eql(
32
34
  "<i class='fab fa-github' data-fa-transform='' title=''></i>"
33
35
  )
34
36
  end
@@ -42,7 +44,7 @@ RSpec.describe FA do
42
44
  { name: 'exclamation', options: { style: :regular } }
43
45
  ]
44
46
 
45
- expect(FA::Layer.new(icons, grow: 2)).to eql(
47
+ expect(FA::Layer.new(icons, grow: 2).safe).to eql(
46
48
  "<span class='icon fa-layers fa-fw ' title=''>" \
47
49
  "<i class='fas fa-square' data-fa-transform='grow-2' title=''></i>" \
48
50
  "<i class='fas fa-circle' data-fa-transform='grow-3' title=''></i>" \
@@ -57,7 +59,7 @@ RSpec.describe FA do
57
59
  { name: :counter, text: 17, options: { position: :tl } }
58
60
  ]
59
61
 
60
- expect(FA::Layer.new(icons)).to eql(
62
+ expect(FA::Layer.new(icons).safe).to eql(
61
63
  "<span class='icon fa-layers fa-fw ' title=''>" \
62
64
  "<i class='fas fa-square' data-fa-transform='grow-0' title=''></i>" \
63
65
  "<span class='fa-layers-counter fa-layers-top-left' " \
@@ -68,14 +70,14 @@ RSpec.describe FA do
68
70
 
69
71
  describe 'span' do
70
72
  it 'should generate the correct span from a string or symbol type' do
71
- expect(FA::Span.new(:text, 'Hello')).to eql(
73
+ expect(FA::Span.new(:text, 'Hello').safe).to eql(
72
74
  "<span class='fa-layers-text ' data-fa-transform=''>Hello</span>"
73
75
  )
74
76
  end
75
77
 
76
78
  it 'should generate the correct span from a configuration hash' do
77
79
  span = { type: :text, text: 'World', options: { position: :bl } }
78
- expect(FA::Span.new(span)).to eql(
80
+ expect(FA::Span.new(span).safe).to eql(
79
81
  "<span class='fa-layers-text fa-layers-bottom-left' " \
80
82
  "data-fa-transform=''>World</span>"
81
83
  )
@@ -83,7 +85,7 @@ RSpec.describe FA do
83
85
 
84
86
  it 'should raise ArgumentError for other input types' do
85
87
  [nil, [], 0].each do |fa|
86
- expect { FA::Span.new(fa) }.to raise_error(
88
+ expect { FA::Span.new(fa).safe }.to raise_error(
87
89
  ArgumentError, 'Unexpected argument type.'
88
90
  )
89
91
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: fa_rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.1
4
+ version: 0.1.2
5
5
  platform: ruby
6
6
  authors:
7
7
  - Julian Fiander