fontawesome_cdn 0.2.0 → 0.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/CHANGELOG.md +26 -0
- data/README.md +65 -16
- data/lib/fontawesome_cdn/helpers/icon.rb +71 -0
- data/lib/fontawesome_cdn/helpers/include_font_awesome.rb +71 -0
- data/lib/fontawesome_cdn/railtie.rb +0 -1
- data/lib/fontawesome_cdn/version.rb +1 -1
- data/lib/fontawesome_cdn.rb +19 -1
- metadata +12 -6
- data/lib/fontawesome_cdn/helpers.rb +0 -137
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: a9b8e3919d1d4dcb36bf6a9bb3e9f5def25c525492794326c1a85a0b589eacda
|
|
4
|
+
data.tar.gz: c1ba2a30811e630037485f5a0b5e48bf182f73602e9f8ba46f4a7f64c4b00d46
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: d53ec45865ff84d2f644f74153078ad9245d9de09fedd107ba533431a25d20c05ce5ad5390886ee84a3285678db9f9b8a87d17354f7e331f5a4f1b8d303d390a
|
|
7
|
+
data.tar.gz: a28fb12557609a8f5be06f0a9a9f1dc6b4689d971a367145720eee27b1ed57c5b2d31da1cdd49d6b7e234fc894a854dac52e1bd1fd43ef3e4058d3f6c5a0fde9
|
data/CHANGELOG.md
CHANGED
|
@@ -6,6 +6,32 @@ This project follows [Semantic Versioning](https://semver.org).
|
|
|
6
6
|
|
|
7
7
|
---
|
|
8
8
|
|
|
9
|
+
## 0.3.0
|
|
10
|
+
|
|
11
|
+
### Changed
|
|
12
|
+
- Renamed the `fontawesome_cdn_stylesheet_tag` helper to `include_font_awesome` for clarity and consistency.
|
|
13
|
+
- The new `include_font_awesome` helper now supports loading **Font Awesome Kits**, including **Font Awesome Pro**.
|
|
14
|
+
|
|
15
|
+
### Added
|
|
16
|
+
- Added `kit:` option to `include_font_awesome` to load Font Awesome through the official Kit CDN:
|
|
17
|
+
```erb
|
|
18
|
+
<%= include_font_awesome kit: "YOUR-KIT-ID" %>
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
### Behavior
|
|
22
|
+
- Using a version number still loads Font Awesome Free from cdnjs:
|
|
23
|
+
```erb
|
|
24
|
+
<%= include_font_awesome "7.0.1" %>
|
|
25
|
+
```
|
|
26
|
+
- Using `kit:` injects the official Font Awesome Kit `<script>` tag.
|
|
27
|
+
- Passing both `version` and `kit:` now raises an error.
|
|
28
|
+
|
|
29
|
+
### Notes
|
|
30
|
+
- Existing functionality is preserved for layouts using CDN-based loading.
|
|
31
|
+
- This update enables developers with a **Font Awesome Pro** subscription to load their private icon kits safely and cleanly.
|
|
32
|
+
|
|
33
|
+
---
|
|
34
|
+
|
|
9
35
|
## 0.2.0
|
|
10
36
|
|
|
11
37
|
### Changed
|
data/README.md
CHANGED
|
@@ -3,13 +3,13 @@
|
|
|
3
3
|
[](https://rubygems.org/gems/fontawesome_cdn)
|
|
4
4
|
[](https://github.com/OpenCodeForge/fontawesome_cdn/actions/workflows/ci.yml)
|
|
5
5
|
|
|
6
|
-
Simple Rails helpers to load **Font Awesome via CDN
|
|
6
|
+
Simple Rails helpers to load **Font Awesome via CDN or Kit**, and render icons in views.
|
|
7
7
|
|
|
8
|
+
✅ Supports **Font Awesome Free (CDN)**
|
|
9
|
+
✅ Supports **Font Awesome Pro (Kit)**
|
|
8
10
|
✅ Compatible with **Font Awesome 7**
|
|
9
|
-
✅ Compatible with **Rails 8**
|
|
10
|
-
✅ No asset pipeline
|
|
11
|
-
✅ No JavaScript
|
|
12
|
-
|
|
11
|
+
✅ Compatible with **Rails 7 and 8**
|
|
12
|
+
✅ No asset pipeline required
|
|
13
13
|
|
|
14
14
|
---
|
|
15
15
|
|
|
@@ -27,19 +27,44 @@ bundle install
|
|
|
27
27
|
|
|
28
28
|
## 🚀 Usage
|
|
29
29
|
|
|
30
|
-
|
|
30
|
+
## 1️⃣ Load Font Awesome in your layout
|
|
31
|
+
|
|
32
|
+
### **Option A — Load from cdnjs (Font Awesome Free)**
|
|
31
33
|
|
|
32
|
-
Place
|
|
34
|
+
Place inside your `<head>`:
|
|
33
35
|
|
|
34
36
|
```erb
|
|
35
|
-
<%=
|
|
37
|
+
<%= include_font_awesome "7.0.1" %>
|
|
38
|
+
```
|
|
39
|
+
|
|
40
|
+
This generates:
|
|
41
|
+
|
|
42
|
+
```html
|
|
43
|
+
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/font-awesome/7.0.1/css/all.min.css" ...>
|
|
36
44
|
```
|
|
37
45
|
|
|
38
46
|
---
|
|
39
47
|
|
|
40
|
-
###
|
|
48
|
+
### **Option B — Load a Font Awesome Kit (supports Pro)**
|
|
41
49
|
|
|
42
|
-
|
|
50
|
+
If you have a Font Awesome Pro subscription, you can load your Kit:
|
|
51
|
+
|
|
52
|
+
```erb
|
|
53
|
+
<%= include_font_awesome kit: "YOUR-KIT-ID" %>
|
|
54
|
+
```
|
|
55
|
+
|
|
56
|
+
This generates:
|
|
57
|
+
|
|
58
|
+
```html
|
|
59
|
+
<script src="https://kit.fontawesome.com/YOUR-KIT-ID.js" crossorigin="anonymous"></script>
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
👉 Use this method for **Font Awesome Pro**
|
|
63
|
+
👉 The kit automatically loads your own selection (Pro icons, subsets, etc.)
|
|
64
|
+
|
|
65
|
+
---
|
|
66
|
+
|
|
67
|
+
## 2️⃣ Render icons in views
|
|
43
68
|
|
|
44
69
|
```erb
|
|
45
70
|
<%= icon "user" %>
|
|
@@ -48,17 +73,41 @@ Use this helper anywhere in your views or partials.
|
|
|
48
73
|
<%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
|
|
49
74
|
```
|
|
50
75
|
|
|
76
|
+
### Options
|
|
77
|
+
|
|
78
|
+
| Option | Description |
|
|
79
|
+
|--------|-------------|
|
|
80
|
+
| `style:` | `:solid`, `:regular`, `:light`, `:thin`, `:semibold` (default: `:solid`) |
|
|
81
|
+
| `pack:` | Use `:brands` when using Font Awesome Brands |
|
|
82
|
+
| `class:` | Additional CSS classes |
|
|
83
|
+
| `aria-hidden:` | Passed as-is |
|
|
84
|
+
|
|
85
|
+
---
|
|
86
|
+
|
|
87
|
+
## 🧩 Supported Font Awesome versions
|
|
88
|
+
|
|
89
|
+
These versions are loaded via **cdnjs** and include verified SRI hashes:
|
|
90
|
+
|
|
91
|
+
- **7.0.1**
|
|
92
|
+
- **7.0.0**
|
|
93
|
+
- **6.7.2**
|
|
94
|
+
|
|
51
95
|
---
|
|
52
96
|
|
|
53
|
-
##
|
|
97
|
+
## 🔐 Using Font Awesome Pro
|
|
54
98
|
|
|
55
|
-
|
|
99
|
+
To load Pro icons, create a **Kit** in your Font Awesome account:
|
|
56
100
|
|
|
57
|
-
|
|
101
|
+
1. Go to https://fontawesome.com/kits
|
|
102
|
+
2. Create a kit
|
|
103
|
+
3. Copy your Kit ID
|
|
104
|
+
4. Use:
|
|
105
|
+
|
|
106
|
+
```erb
|
|
107
|
+
<%= include_font_awesome kit: "YOUR-KIT-ID" %>
|
|
108
|
+
```
|
|
58
109
|
|
|
59
|
-
|
|
60
|
-
- ✅ **7.0.0**
|
|
61
|
-
- ✅ **6.7.2**
|
|
110
|
+
> ❗ CDNJS does **not** host Font Awesome Pro — you must use a Kit.
|
|
62
111
|
|
|
63
112
|
---
|
|
64
113
|
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FontawesomeCdn
|
|
4
|
+
# View helpers for rendering Font Awesome icons
|
|
5
|
+
module Helpers
|
|
6
|
+
ICON_STYLES = %w[solid regular light thin semibold].freeze
|
|
7
|
+
DEFAULT_ICON_STYLE = "solid"
|
|
8
|
+
|
|
9
|
+
# Main helper for displaying an icon
|
|
10
|
+
#
|
|
11
|
+
# <%= icon "user" %>
|
|
12
|
+
# <%= icon "gear", "Settings" %>
|
|
13
|
+
# <%= icon "trash-can", "Delete", style: :regular %>
|
|
14
|
+
# <%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
|
|
15
|
+
#
|
|
16
|
+
def icon(name, text = nil, **options)
|
|
17
|
+
# Allow the 2nd argument to be either text or the options hash
|
|
18
|
+
if text.is_a?(Hash)
|
|
19
|
+
options = text
|
|
20
|
+
text = nil
|
|
21
|
+
end
|
|
22
|
+
|
|
23
|
+
pack_class, style_class = build_pack_and_style_classes(options)
|
|
24
|
+
|
|
25
|
+
options[:class] = [pack_class, style_class, "fa-#{name}", options[:class]].compact.join(" ")
|
|
26
|
+
options["aria-hidden"] = true unless options.key?("aria-hidden") || options.key?(:"aria-hidden")
|
|
27
|
+
|
|
28
|
+
icon_tag = tag.i(nil, **options)
|
|
29
|
+
|
|
30
|
+
text.nil? || text.to_s.strip.empty? ? icon_tag : safe_join([icon_tag, " ", text.to_s])
|
|
31
|
+
end
|
|
32
|
+
|
|
33
|
+
private
|
|
34
|
+
|
|
35
|
+
# Classes:
|
|
36
|
+
# - style → fa-solid / fa-regular / ...
|
|
37
|
+
# - pack → fa-brands / ...
|
|
38
|
+
# Special case: brands icons don't use a style like "solid"
|
|
39
|
+
def build_pack_and_style_classes(options)
|
|
40
|
+
pack = options.delete(:pack)&.to_s
|
|
41
|
+
style = options.delete(:style)&.to_s
|
|
42
|
+
style ||= DEFAULT_ICON_STYLE unless pack == "brands"
|
|
43
|
+
|
|
44
|
+
validate_icon_style_and_pack!(style, pack)
|
|
45
|
+
|
|
46
|
+
[pack, style].compact.map { |c| "fa-#{c}" }
|
|
47
|
+
end
|
|
48
|
+
|
|
49
|
+
#
|
|
50
|
+
# Icon validation
|
|
51
|
+
#
|
|
52
|
+
def validate_icon_style_and_pack!(style, pack)
|
|
53
|
+
raise_style_used_as_pack_error(pack) if pack && ICON_STYLES.include?(pack)
|
|
54
|
+
raise_unsupported_icon_style_error(style) if style && !ICON_STYLES.include?(style)
|
|
55
|
+
end
|
|
56
|
+
|
|
57
|
+
def raise_style_used_as_pack_error(pack)
|
|
58
|
+
raise ArgumentError, <<~MSG
|
|
59
|
+
fontawesome_cdn: #{pack.inspect} is a style, not a pack.
|
|
60
|
+
Use style: #{pack.inspect} instead.
|
|
61
|
+
MSG
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def raise_unsupported_icon_style_error(style)
|
|
65
|
+
raise ArgumentError, <<~MSG
|
|
66
|
+
fontawesome_cdn: unsupported icon style #{style.inspect}.
|
|
67
|
+
Supported styles: #{ICON_STYLES.join(", ")}
|
|
68
|
+
MSG
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module FontawesomeCdn
|
|
4
|
+
# View helpers for loading Font Awesome assets (CDN or kit)
|
|
5
|
+
module Helpers
|
|
6
|
+
# Helper for loading Font Awesome
|
|
7
|
+
#
|
|
8
|
+
# <%= include_font_awesome "7.0.1" %>
|
|
9
|
+
# # => <link rel="stylesheet" ...> to cdnjs
|
|
10
|
+
#
|
|
11
|
+
# <%= include_font_awesome kit: "YOUR-KIT" %>
|
|
12
|
+
# # => <script src="https://kit.fontawesome.com/YOUR-KIT.js" crossorigin="anonymous"></script>
|
|
13
|
+
#
|
|
14
|
+
def include_font_awesome(version = nil, kit: nil, **options)
|
|
15
|
+
# Prevent ambiguous usage
|
|
16
|
+
raise ArgumentError, "Use either the version argument or the :kit option, not both" if version && kit
|
|
17
|
+
|
|
18
|
+
if kit
|
|
19
|
+
attrs = {
|
|
20
|
+
src: "#{FontawesomeCdn::KIT_BASE_URL}/#{kit}.js",
|
|
21
|
+
crossorigin: "anonymous"
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
tag.script(nil, **attrs, **options)
|
|
25
|
+
else
|
|
26
|
+
validate_fontawesome_version!(version)
|
|
27
|
+
|
|
28
|
+
attrs = {
|
|
29
|
+
rel: "stylesheet",
|
|
30
|
+
href: "#{FontawesomeCdn::CDN_BASE_URL}/#{version}/css/all.min.css",
|
|
31
|
+
integrity: FontawesomeCdn::CDN_INTEGRITY_MAP[version],
|
|
32
|
+
crossorigin: "anonymous",
|
|
33
|
+
referrerpolicy: "no-referrer"
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
tag.link(**attrs, **options)
|
|
37
|
+
end
|
|
38
|
+
end
|
|
39
|
+
|
|
40
|
+
private
|
|
41
|
+
|
|
42
|
+
#
|
|
43
|
+
# Font Awesome version validation
|
|
44
|
+
#
|
|
45
|
+
def validate_fontawesome_version!(version)
|
|
46
|
+
raise_version_required_error if version.nil?
|
|
47
|
+
raise_version_type_error(version) unless version.is_a?(String)
|
|
48
|
+
raise_version_not_supported_error(version) unless FontawesomeCdn::SUPPORTED_VERSIONS.include?(version)
|
|
49
|
+
end
|
|
50
|
+
|
|
51
|
+
def raise_version_required_error
|
|
52
|
+
raise ArgumentError, <<~MSG
|
|
53
|
+
fontawesome_cdn: Font Awesome version is required.
|
|
54
|
+
Usage: <%= include_font_awesome "7.x.x" %> or <%= include_font_awesome kit: "YOUR-KIT" %>
|
|
55
|
+
MSG
|
|
56
|
+
end
|
|
57
|
+
|
|
58
|
+
def raise_version_type_error(version)
|
|
59
|
+
raise ArgumentError, <<~MSG
|
|
60
|
+
fontawesome_cdn: version must be a String, got #{version.class}
|
|
61
|
+
MSG
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def raise_version_not_supported_error(version)
|
|
65
|
+
raise ArgumentError, <<~MSG
|
|
66
|
+
fontawesome_cdn: Font Awesome version #{version.inspect} is not supported.
|
|
67
|
+
Supported versions: #{FontawesomeCdn::SUPPORTED_VERSIONS.join(", ")}
|
|
68
|
+
MSG
|
|
69
|
+
end
|
|
70
|
+
end
|
|
71
|
+
end
|
data/lib/fontawesome_cdn.rb
CHANGED
|
@@ -1,9 +1,27 @@
|
|
|
1
1
|
# frozen_string_literal: true
|
|
2
2
|
|
|
3
3
|
require_relative "fontawesome_cdn/version"
|
|
4
|
-
|
|
4
|
+
require "fontawesome_cdn/helpers/include_font_awesome"
|
|
5
|
+
require "fontawesome_cdn/helpers/icon"
|
|
5
6
|
require "fontawesome_cdn/railtie" if defined?(Rails)
|
|
6
7
|
|
|
7
8
|
# Rails helpers for loading Font Awesome via CDN.
|
|
8
9
|
module FontawesomeCdn
|
|
10
|
+
CDN_BASE_URL = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome"
|
|
11
|
+
KIT_BASE_URL = "https://kit.fontawesome.com"
|
|
12
|
+
|
|
13
|
+
# Font Awesome versions supported by the gem
|
|
14
|
+
# SRI hashes provided by cdnjs for all.min.css
|
|
15
|
+
CDN_INTEGRITY_MAP = {
|
|
16
|
+
"7.0.1" => "sha512-2SwdPD6INVrV/lHTZbO2nodKhrnDdJK9/kg2XD1r9uGqPo1cUbujc+IYdlYdEErWNu69gVcYgdxlmVmzTWnetw==",
|
|
17
|
+
"7.0.0" => "sha512-DxV+EoADOkOygM4IR9yXP8Sb2qwgidEmeqAEmDKIOfPRQZOWbXCzLC6vjbZyy0vPisbH2SyW27+ddLVCN+OMzQ==",
|
|
18
|
+
"6.7.2" => "sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg=="
|
|
19
|
+
}.freeze
|
|
20
|
+
|
|
21
|
+
SUPPORTED_VERSIONS = CDN_INTEGRITY_MAP.keys.freeze
|
|
22
|
+
|
|
23
|
+
# Helpers exposed to the Rails views
|
|
24
|
+
module Helpers
|
|
25
|
+
# The methods are added here by the helper files
|
|
26
|
+
end
|
|
9
27
|
end
|
metadata
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
|
2
2
|
name: fontawesome_cdn
|
|
3
3
|
version: !ruby/object:Gem::Version
|
|
4
|
-
version: 0.
|
|
4
|
+
version: 0.3.0
|
|
5
5
|
platform: ruby
|
|
6
6
|
authors:
|
|
7
7
|
- OpenCodeForge
|
|
@@ -16,6 +16,9 @@ dependencies:
|
|
|
16
16
|
- - ">="
|
|
17
17
|
- !ruby/object:Gem::Version
|
|
18
18
|
version: '7.0'
|
|
19
|
+
- - "<"
|
|
20
|
+
- !ruby/object:Gem::Version
|
|
21
|
+
version: '9.0'
|
|
19
22
|
type: :runtime
|
|
20
23
|
prerelease: false
|
|
21
24
|
version_requirements: !ruby/object:Gem::Requirement
|
|
@@ -23,9 +26,12 @@ dependencies:
|
|
|
23
26
|
- - ">="
|
|
24
27
|
- !ruby/object:Gem::Version
|
|
25
28
|
version: '7.0'
|
|
29
|
+
- - "<"
|
|
30
|
+
- !ruby/object:Gem::Version
|
|
31
|
+
version: '9.0'
|
|
26
32
|
description: |
|
|
27
|
-
|
|
28
|
-
|
|
33
|
+
Provides Rails view helpers to load Font Awesome either from cdnjs or from official
|
|
34
|
+
Font Awesome Kits (including Pro), along with a clean icon helper.
|
|
29
35
|
email:
|
|
30
36
|
- contact@opencodeforge.com
|
|
31
37
|
executables: []
|
|
@@ -38,7 +44,8 @@ files:
|
|
|
38
44
|
- README.md
|
|
39
45
|
- Rakefile
|
|
40
46
|
- lib/fontawesome_cdn.rb
|
|
41
|
-
- lib/fontawesome_cdn/helpers.rb
|
|
47
|
+
- lib/fontawesome_cdn/helpers/icon.rb
|
|
48
|
+
- lib/fontawesome_cdn/helpers/include_font_awesome.rb
|
|
42
49
|
- lib/fontawesome_cdn/railtie.rb
|
|
43
50
|
- lib/fontawesome_cdn/version.rb
|
|
44
51
|
- sig/fontawesome_cdn.rbs
|
|
@@ -47,7 +54,6 @@ licenses:
|
|
|
47
54
|
- MIT
|
|
48
55
|
metadata:
|
|
49
56
|
homepage_uri: https://github.com/OpenCodeForge/fontawesome_cdn
|
|
50
|
-
source_code_uri: https://github.com/OpenCodeForge/fontawesome_cdn
|
|
51
57
|
bug_tracker_uri: https://github.com/OpenCodeForge/fontawesome_cdn/issues
|
|
52
58
|
changelog_uri: https://github.com/OpenCodeForge/fontawesome_cdn/blob/main/CHANGELOG.md
|
|
53
59
|
rubygems_mfa_required: 'true'
|
|
@@ -67,5 +73,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
|
|
|
67
73
|
requirements: []
|
|
68
74
|
rubygems_version: 3.7.2
|
|
69
75
|
specification_version: 4
|
|
70
|
-
summary: Simple Rails helpers to load Font Awesome
|
|
76
|
+
summary: Simple Rails helpers to load Font Awesome (Free or Pro Kit) via CDN.
|
|
71
77
|
test_files: []
|
|
@@ -1,137 +0,0 @@
|
|
|
1
|
-
# frozen_string_literal: true
|
|
2
|
-
|
|
3
|
-
module FontawesomeCdn
|
|
4
|
-
BASE_URL = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome"
|
|
5
|
-
|
|
6
|
-
# Font Awesome versions supported by the gem
|
|
7
|
-
# SRI hashes provided by cdnjs for all.min.css
|
|
8
|
-
INTEGRITY_MAP = {
|
|
9
|
-
"7.0.1" => "sha512-2SwdPD6INVrV/lHTZbO2nodKhrnDdJK9/kg2XD1r9uGqPo1cUbujc+IYdlYdEErWNu69gVcYgdxlmVmzTWnetw==",
|
|
10
|
-
"7.0.0" => "sha512-DxV+EoADOkOygM4IR9yXP8Sb2qwgidEmeqAEmDKIOfPRQZOWbXCzLC6vjbZyy0vPisbH2SyW27+ddLVCN+OMzQ==",
|
|
11
|
-
"6.7.2" => "sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg=="
|
|
12
|
-
}.freeze
|
|
13
|
-
|
|
14
|
-
SUPPORTED_VERSIONS = INTEGRITY_MAP.keys.freeze
|
|
15
|
-
|
|
16
|
-
ICON_STYLES = %w[solid regular light thin semibold].freeze
|
|
17
|
-
DEFAULT_ICON_STYLE = "solid"
|
|
18
|
-
|
|
19
|
-
# Helpers exposed to Rails views.
|
|
20
|
-
#
|
|
21
|
-
# Provides:
|
|
22
|
-
# - fontawesome_cdn_stylesheet_tag(version)
|
|
23
|
-
# - icon(name, text = nil, **options)
|
|
24
|
-
module Helpers
|
|
25
|
-
# Stylesheet helper for loading Font Awesome via CDN
|
|
26
|
-
#
|
|
27
|
-
# <%= fontawesome_cdn_stylesheet_tag "7.0.1" %>
|
|
28
|
-
#
|
|
29
|
-
# Raises ArgumentError if version is not supported.
|
|
30
|
-
#
|
|
31
|
-
def fontawesome_cdn_stylesheet_tag(version = nil, **options)
|
|
32
|
-
validate_fontawesome_version!(version)
|
|
33
|
-
|
|
34
|
-
href = "#{FontawesomeCdn::BASE_URL}/#{version}/css/all.min.css"
|
|
35
|
-
|
|
36
|
-
attrs = {
|
|
37
|
-
rel: "stylesheet",
|
|
38
|
-
href: href,
|
|
39
|
-
integrity: FontawesomeCdn::INTEGRITY_MAP[version],
|
|
40
|
-
crossorigin: "anonymous",
|
|
41
|
-
referrerpolicy: "no-referrer"
|
|
42
|
-
}
|
|
43
|
-
|
|
44
|
-
tag.link(**attrs, **options)
|
|
45
|
-
end
|
|
46
|
-
|
|
47
|
-
# Main helper for displaying an icon
|
|
48
|
-
#
|
|
49
|
-
# <%= icon "user" %>
|
|
50
|
-
# <%= icon "gear", "Settings" %>
|
|
51
|
-
# <%= icon "trash-can", "Delete", style: :regular %>
|
|
52
|
-
# <%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
|
|
53
|
-
#
|
|
54
|
-
def icon(name, text = nil, **options)
|
|
55
|
-
# Allow the 2nd argument to be either text or the options hash
|
|
56
|
-
if text.is_a?(Hash)
|
|
57
|
-
options = text
|
|
58
|
-
text = nil
|
|
59
|
-
end
|
|
60
|
-
|
|
61
|
-
pack_class, style_class = build_pack_and_style_classes(options)
|
|
62
|
-
options[:class] = [pack_class, style_class, "fa-#{name}", options[:class]].compact.join(" ")
|
|
63
|
-
options["aria-hidden"] = true unless options.key?("aria-hidden") || options.key?(:"aria-hidden")
|
|
64
|
-
|
|
65
|
-
icon_tag = tag.i(nil, **options)
|
|
66
|
-
|
|
67
|
-
text.nil? || text.to_s.strip.empty? ? icon_tag : safe_join([icon_tag, " ", text.to_s])
|
|
68
|
-
end
|
|
69
|
-
|
|
70
|
-
private
|
|
71
|
-
|
|
72
|
-
#
|
|
73
|
-
# Font Awesome version validation
|
|
74
|
-
#
|
|
75
|
-
def validate_fontawesome_version!(version)
|
|
76
|
-
raise_version_required_error if version.nil?
|
|
77
|
-
raise_version_type_error(version) unless version.is_a?(String)
|
|
78
|
-
raise_version_not_supported_error(version) unless FontawesomeCdn::SUPPORTED_VERSIONS.include?(version)
|
|
79
|
-
end
|
|
80
|
-
|
|
81
|
-
def raise_version_required_error
|
|
82
|
-
raise ArgumentError, <<~MSG
|
|
83
|
-
fontawesome_cdn: Font Awesome version is required.
|
|
84
|
-
Usage: <%= fontawesome_cdn_stylesheet_tag "7.x.x" %>
|
|
85
|
-
MSG
|
|
86
|
-
end
|
|
87
|
-
|
|
88
|
-
def raise_version_type_error(version)
|
|
89
|
-
raise ArgumentError, <<~MSG
|
|
90
|
-
fontawesome_cdn: version must be a String, got #{version.class}
|
|
91
|
-
MSG
|
|
92
|
-
end
|
|
93
|
-
|
|
94
|
-
def raise_version_not_supported_error(version)
|
|
95
|
-
raise ArgumentError, <<~MSG
|
|
96
|
-
fontawesome_cdn: Font Awesome version #{version.inspect} is not supported.
|
|
97
|
-
Supported versions: #{FontawesomeCdn::SUPPORTED_VERSIONS.join(", ")}
|
|
98
|
-
MSG
|
|
99
|
-
end
|
|
100
|
-
|
|
101
|
-
# Classes:
|
|
102
|
-
# - style → fa-solid / fa-regular / ...
|
|
103
|
-
# - pack → fa-brands / ...
|
|
104
|
-
# Special case: brands icons don't use a style like "solid"
|
|
105
|
-
def build_pack_and_style_classes(options)
|
|
106
|
-
pack = options.delete(:pack)&.to_s
|
|
107
|
-
style = options.delete(:style)&.to_s
|
|
108
|
-
style ||= DEFAULT_ICON_STYLE unless pack == "brands"
|
|
109
|
-
|
|
110
|
-
validate_icon_style_and_pack!(style, pack)
|
|
111
|
-
|
|
112
|
-
[pack, style].compact.map { |c| "fa-#{c}" }
|
|
113
|
-
end
|
|
114
|
-
|
|
115
|
-
#
|
|
116
|
-
# Icon validation
|
|
117
|
-
#
|
|
118
|
-
def validate_icon_style_and_pack!(style, pack)
|
|
119
|
-
raise_style_used_as_pack_error(pack) if pack && ICON_STYLES.include?(pack)
|
|
120
|
-
raise_unsupported_icon_style_error(style) if style && !ICON_STYLES.include?(style)
|
|
121
|
-
end
|
|
122
|
-
|
|
123
|
-
def raise_style_used_as_pack_error(pack)
|
|
124
|
-
raise ArgumentError, <<~MSG
|
|
125
|
-
fontawesome_cdn: #{pack.inspect} is a style, not a pack.
|
|
126
|
-
Use style: #{pack.inspect} instead.
|
|
127
|
-
MSG
|
|
128
|
-
end
|
|
129
|
-
|
|
130
|
-
def raise_unsupported_icon_style_error(style)
|
|
131
|
-
raise ArgumentError, <<~MSG
|
|
132
|
-
fontawesome_cdn: unsupported icon style #{style.inspect}.
|
|
133
|
-
Supported styles: #{ICON_STYLES.join(", ")}
|
|
134
|
-
MSG
|
|
135
|
-
end
|
|
136
|
-
end
|
|
137
|
-
end
|