fontawesome_cdn 0.2.0 → 1.0.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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: f6e25b39df453df95abdf3432c1a3d752c99ecb9f16a61c722a61144589b5cdb
4
- data.tar.gz: fc6374becd7e0a3c9331b76560e342818acda650554aabb234961ca4f908299f
3
+ metadata.gz: 17a69f882df20af599d2070ad9249f1759d14fc0b14bea47c109b0a15d157d0e
4
+ data.tar.gz: 187ff7f8c50fd6edaa022217c4d526ee63f447cd46590b2f037bd76f988189e9
5
5
  SHA512:
6
- metadata.gz: 5fb96ae5eb2718f69423fcd5627e2a5d2571a23453bed4b66c742219f9b83423e9d6cd076dc21e426d829159356e24318f94bcafe10acb0d92f04fac7d8d4b6d
7
- data.tar.gz: e679249b19bbe2acc6622fed33d9995d6806efeee854656de21c434d99e0049658fe9494f774d34e42d4924872f8b27df8d4d898a84e51543240da310d64be1e
6
+ metadata.gz: c71799ae50fc7baf58f5057d58e96f047f079fff04ce4a97bc919b3769c4e3ba8a401c07cc1280980e2d0e2583246a3589c71184da9d6f736fddcf9e10dc0fda
7
+ data.tar.gz: de8a29cdc724e752652a6f116ed0010ad76eff35952a810b343b71eeaf22d87ff759937b6c9a1a9734c025e8b10ebce2d8391baac9a4dcac59ae63eb6ca749c7
data/CHANGELOG.md CHANGED
@@ -6,6 +6,68 @@ This project follows [Semantic Versioning](https://semver.org).
6
6
 
7
7
  ---
8
8
 
9
+ ## 1.0.0
10
+
11
+ This release marks the **first stable version** of FontawesomeCdn.
12
+ The public API is now considered stable and will follow Semantic Versioning.
13
+
14
+ ### Changed
15
+ - Stabilized the `icon` helper API with a fully class-based approach.
16
+ - Removed legacy `style:` and `pack:` options in favor of direct Font Awesome classes.
17
+ - Default family and style are now applied automatically via configuration.
18
+ - The `classic` family is implicit and no longer rendered as `fa-classic`.
19
+ - When using the `brands` family, no style class is automatically injected.
20
+
21
+ ### Added
22
+ - Added `fa:` option as a convenience shortcut to pass space-separated Font Awesome tokens
23
+ (automatically prefixed with `fa-`).
24
+ - Added global configuration options:
25
+ - `default_family`
26
+ - `default_style`
27
+ - `default_aria_hidden`
28
+
29
+ ### Examples
30
+ ```erb
31
+ <%= icon "user" %>
32
+ <%= icon "gear", "Settings" %>
33
+ <%= icon "bell", class: "fa-regular fa-2x fa-shake" %>
34
+ <%= icon "bell", fa: "regular 2x shake" %>
35
+ <%= icon "github", "Source code", fa: "brands", class: "link" %>
36
+ ```
37
+
38
+ ### Notes
39
+ - No validation or conflict resolution is applied when multiple styles or families are provided.
40
+ - Users are free to combine classes as needed for experimentation or advanced use cases.
41
+ - Versions prior to `1.0.0` should be considered experimental.
42
+
43
+ ---
44
+
45
+ ## 0.3.0
46
+
47
+ ### Changed
48
+ - Renamed the `fontawesome_cdn_stylesheet_tag` helper to `include_font_awesome` for clarity and consistency.
49
+ - The new `include_font_awesome` helper now supports loading **Font Awesome Kits**, including **Font Awesome Pro**.
50
+
51
+ ### Added
52
+ - Added `kit:` option to `include_font_awesome` to load Font Awesome through the official Kit CDN:
53
+ ```erb
54
+ <%= include_font_awesome kit: "YOUR-KIT-ID" %>
55
+ ```
56
+
57
+ ### Behavior
58
+ - Using a version number still loads Font Awesome Free from cdnjs:
59
+ ```erb
60
+ <%= include_font_awesome "7.0.1" %>
61
+ ```
62
+ - Using `kit:` injects the official Font Awesome Kit `<script>` tag.
63
+ - Passing both `version` and `kit:` now raises an error.
64
+
65
+ ### Notes
66
+ - Existing functionality is preserved for layouts using CDN-based loading.
67
+ - This update enables developers with a **Font Awesome Pro** subscription to load their private icon kits safely and cleanly.
68
+
69
+ ---
70
+
9
71
  ## 0.2.0
10
72
 
11
73
  ### Changed
data/README.md CHANGED
@@ -3,13 +3,13 @@
3
3
  [![Gem Version](https://img.shields.io/gem/v/fontawesome_cdn.svg)](https://rubygems.org/gems/fontawesome_cdn)
4
4
  [![CI](https://github.com/OpenCodeForge/fontawesome_cdn/actions/workflows/ci.yml/badge.svg)](https://github.com/OpenCodeForge/fontawesome_cdn/actions/workflows/ci.yml)
5
5
 
6
- Simple Rails helpers to load **Font Awesome via CDN** and render icons in views.
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,38 +27,111 @@ bundle install
27
27
 
28
28
  ## 🚀 Usage
29
29
 
30
- ### Load Font Awesome (layout)
30
+ ### 1️⃣ Load Font Awesome (in your layout)
31
+
32
+ #### **Option A — Load from cdnjs (Font Awesome Free)**
31
33
 
32
- Place this helper in your layout, inside `<head>`.
34
+ Place inside your `<head>`:
33
35
 
34
36
  ```erb
35
- <%= fontawesome_cdn_stylesheet_tag "7.0.1" %>
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
- ### Render icons (views)
48
+ #### **Option B — Load a Font Awesome Kit (supports Pro)**
41
49
 
42
- Use this helper anywhere in your views or partials.
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)
68
+
69
+ Basic usage:
43
70
 
44
71
  ```erb
45
72
  <%= icon "user" %>
46
73
  <%= icon "gear", "Settings" %>
47
- <%= icon "trash-can", "Delete", style: :regular %>
48
- <%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
74
+ <%= icon "bell", class: "fa-regular fa-2x fa-shake" %>
49
75
  ```
50
76
 
77
+ #### Using the `fa:` shortcut
78
+
79
+ For convenience, you can use the `fa:` option to pass **space-separated Font Awesome tokens**, automatically prefixed with `fa-`.
80
+
81
+ ```erb
82
+ <%= icon "bell", fa: "regular 2x shake" %>
83
+ <%= icon "github", "Source code", fa: "brands", class: "link" %>
84
+ ```
85
+
86
+ ---
87
+
88
+ ## ⚙️ Configuration
89
+
90
+ FontawesomeCdn allows you to define default icon behavior that will be **automatically applied to all icons**, unless explicitly overridden at render time.
91
+
92
+ ```rb
93
+ # config/initializers/fontawesome_cdn.rb
94
+ FontawesomeCdn.configure do |config|
95
+ config.default_family = "classic"
96
+ config.default_style = "solid"
97
+ config.default_aria_hidden = true
98
+ end
99
+ ```
100
+
101
+ By default, these settings are used to determine which Font Awesome family and style are added to each icon.
102
+ If a family or style is explicitly provided via `class:` or `fa:`, it will be used instead of the defaults.
103
+
104
+ Defaults:
105
+ - `classic` is implicit (no `fa-classic`)
106
+ - default style is injected automatically
107
+ - `brands` does not inject a style
108
+
51
109
  ---
52
110
 
53
- ## Supported Font Awesome versions
111
+ ## 🧩 Supported Font Awesome versions
112
+
113
+ These versions are loaded via **cdnjs** and include verified SRI hashes:
114
+
115
+ - **7.0.1**
116
+ - **7.0.0**
117
+ - **6.7.2**
118
+
119
+ ---
54
120
 
55
- Font Awesome is loaded directly from [cdnjs](https://cdnjs.com/libraries/font-awesome).
121
+ ## 🔐 Using Font Awesome Pro
56
122
 
57
- **Supported versions** (with verified SRI):
123
+ To load Pro icons, create a **Kit** in your Font Awesome account:
124
+
125
+ 1. Go to https://fontawesome.com/kits
126
+ 2. Create a kit
127
+ 3. Copy your Kit ID
128
+ 4. Use:
129
+
130
+ ```erb
131
+ <%= include_font_awesome kit: "YOUR-KIT-ID" %>
132
+ ```
58
133
 
59
- - **7.0.1**
60
- - ✅ **7.0.0**
61
- - ✅ **6.7.2**
134
+ > CDNJS does **not** host Font Awesome Pro — you must use a Kit.
62
135
 
63
136
  ---
64
137
 
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FontawesomeCdn
4
+ # Configuration object for FontawesomeCdn.
5
+ #
6
+ # Allows customizing default icon behavior such as:
7
+ # - icon family (classic, brands, duotone, …)
8
+ # - icon style (solid, regular, light, thin)
9
+ # - default aria-hidden attribute
10
+ #
11
+ # This configuration can be set in a Rails initializer:
12
+ #
13
+ # FontawesomeCdn.configure do |config|
14
+ # config.default_family = "classic"
15
+ # config.default_style = "solid"
16
+ # config.default_aria_hidden = true
17
+ # end
18
+ #
19
+ class Configuration
20
+ attr_accessor :default_family, :default_style, :default_aria_hidden
21
+
22
+ def initialize
23
+ @default_family = :classic
24
+ @default_style = :solid
25
+ @default_aria_hidden = true
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module FontawesomeCdn
4
+ # View helpers for rendering Font Awesome icons
5
+ module Helpers
6
+ FAMILIES = %w[
7
+ classic duotone sharp sharp-duotone brands chisel etch jelly notdog slab
8
+ thumbprint utility whiteboard
9
+ ].freeze
10
+
11
+ STYLES = %w[solid regular light thin].freeze
12
+
13
+ # Renders a Font Awesome icon
14
+ #
15
+ # Basic usage (default family and style are applied automatically):
16
+ #
17
+ # <%= icon "user" %>
18
+ # <%= icon "gear", "Settings" %>
19
+ # <%= icon "bell", class: "fa-regular fa-2x fa-shake" %>
20
+ #
21
+ # Using the `fa:` shortcut (tokens are automatically prefixed with `fa-`):
22
+ #
23
+ # <%= icon "bell", fa: "regular 2x shake" %>
24
+ # <%= icon "github", "Source code", fa: "brands", class: "link" %>
25
+ # <%= icon "alien", fa: "duotone light" %>
26
+ #
27
+ # Accessibility:
28
+ #
29
+ # <%= icon "user", aria-hidden: false %>
30
+ #
31
+ # Notes:
32
+ # - Default family and style are applied unless explicitly overridden.
33
+ # - No validation or conflict resolution is performed on classes.
34
+ # - Users are free to combine multiple styles or families if needed.
35
+ #
36
+ def icon(name, text = nil, **options)
37
+ # Allow the 2nd argument to be either text or the options hash
38
+ if text.is_a?(Hash)
39
+ options = text
40
+ text = nil
41
+ end
42
+
43
+ aria_hidden =
44
+ if options.key?(:"aria-hidden")
45
+ options.delete(:"aria-hidden")
46
+ else
47
+ FontawesomeCdn.configuration.default_aria_hidden
48
+ end
49
+
50
+ class_tokens = options.delete(:class).to_s.split
51
+ fa_tokens = options.delete(:fa).to_s.split.map { |t| "fa-#{t}" }
52
+
53
+ tokens = (class_tokens + fa_tokens).uniq
54
+
55
+ family = tokens
56
+ .map { |t| t.delete_prefix("fa-") }
57
+ .find { |t| FAMILIES.include?(t) } || FontawesomeCdn.configuration.default_family.to_s
58
+
59
+ style = tokens
60
+ .map { |t| t.delete_prefix("fa-") }
61
+ .find { |t| STYLES.include?(t) } || FontawesomeCdn.configuration.default_style.to_s
62
+
63
+ # remove family + style from tokens so they can't reappear later
64
+ tokens.delete("fa-#{family}")
65
+ tokens.delete("fa-#{style}")
66
+
67
+ classes = []
68
+ classes << "fa-#{family}" unless family == "classic"
69
+ classes << "fa-#{style}" unless family == "brands"
70
+ classes << "fa-#{name}"
71
+ classes.concat(tokens)
72
+
73
+ icon_tag = content_tag(:i, nil, class: classes.uniq.join(" "), "aria-hidden": aria_hidden)
74
+ return icon_tag if text.nil? || text.to_s.empty?
75
+
76
+ safe_join([icon_tag, ERB::Util.html_escape(text.to_s)], " ")
77
+ end
78
+
79
+ private
80
+
81
+ def extract_family_and_style(tokens)
82
+ family = tokens.find { |t| FAMILIES.include?(t) }
83
+ style = tokens.find { |t| STYLES.include?(t) }
84
+ [family, style]
85
+ end
86
+ end
87
+ 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
@@ -1,7 +1,6 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require "rails/railtie"
4
- require "fontawesome_cdn/helpers"
5
4
 
6
5
  module FontawesomeCdn
7
6
  # Railtie for loading FontawesomeCdn helpers into Rails.
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Current version of the FontawesomeCdn gem.
4
4
  module FontawesomeCdn
5
- VERSION = "0.2.0"
5
+ VERSION = "1.0.0"
6
6
  end
@@ -1,9 +1,39 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  require_relative "fontawesome_cdn/version"
4
+ require_relative "fontawesome_cdn/configuration"
4
5
 
6
+ require "fontawesome_cdn/helpers/include_font_awesome"
7
+ require "fontawesome_cdn/helpers/icon"
5
8
  require "fontawesome_cdn/railtie" if defined?(Rails)
6
9
 
7
10
  # Rails helpers for loading Font Awesome via CDN.
8
11
  module FontawesomeCdn
12
+ CDN_BASE_URL = "https://cdnjs.cloudflare.com/ajax/libs/font-awesome"
13
+ KIT_BASE_URL = "https://kit.fontawesome.com"
14
+
15
+ # Font Awesome versions supported by the gem
16
+ # SRI hashes provided by cdnjs for all.min.css
17
+ CDN_INTEGRITY_MAP = {
18
+ "7.0.1" => "sha512-2SwdPD6INVrV/lHTZbO2nodKhrnDdJK9/kg2XD1r9uGqPo1cUbujc+IYdlYdEErWNu69gVcYgdxlmVmzTWnetw==",
19
+ "7.0.0" => "sha512-DxV+EoADOkOygM4IR9yXP8Sb2qwgidEmeqAEmDKIOfPRQZOWbXCzLC6vjbZyy0vPisbH2SyW27+ddLVCN+OMzQ==",
20
+ "6.7.2" => "sha512-Evv84Mr4kqVGRNSgIGL/F/aIDqQb7xQ2vcrdIwxfjThSH8CSR7PBEakCr51Ck+w+/U6swU2Im1vVX0SVk9ABhg=="
21
+ }.freeze
22
+
23
+ SUPPORTED_VERSIONS = CDN_INTEGRITY_MAP.keys.freeze
24
+
25
+ class << self
26
+ def configuration
27
+ @configuration ||= Configuration.new
28
+ end
29
+
30
+ def configure
31
+ yield(configuration)
32
+ end
33
+ end
34
+
35
+ # Helpers exposed to the Rails views
36
+ module Helpers
37
+ # The methods are added here by the helper files
38
+ end
9
39
  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.2.0
4
+ version: 1.0.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
- Tiny Rails helpers to load Font Awesome via CDN and render icons.
28
- No asset pipeline integration, no JavaScript, no magic.
33
+ FontawesomeCdn provides simple Rails helpers to load Font Awesome (Free or Pro) via CDN or Kit,
34
+ and render icons using a flexible, class-based API.
29
35
  email:
30
36
  - contact@opencodeforge.com
31
37
  executables: []
@@ -38,7 +44,9 @@ files:
38
44
  - README.md
39
45
  - Rakefile
40
46
  - lib/fontawesome_cdn.rb
41
- - lib/fontawesome_cdn/helpers.rb
47
+ - lib/fontawesome_cdn/configuration.rb
48
+ - lib/fontawesome_cdn/helpers/icon.rb
49
+ - lib/fontawesome_cdn/helpers/include_font_awesome.rb
42
50
  - lib/fontawesome_cdn/railtie.rb
43
51
  - lib/fontawesome_cdn/version.rb
44
52
  - sig/fontawesome_cdn.rbs
@@ -47,7 +55,6 @@ licenses:
47
55
  - MIT
48
56
  metadata:
49
57
  homepage_uri: https://github.com/OpenCodeForge/fontawesome_cdn
50
- source_code_uri: https://github.com/OpenCodeForge/fontawesome_cdn
51
58
  bug_tracker_uri: https://github.com/OpenCodeForge/fontawesome_cdn/issues
52
59
  changelog_uri: https://github.com/OpenCodeForge/fontawesome_cdn/blob/main/CHANGELOG.md
53
60
  rubygems_mfa_required: 'true'
@@ -67,5 +74,5 @@ required_rubygems_version: !ruby/object:Gem::Requirement
67
74
  requirements: []
68
75
  rubygems_version: 3.7.2
69
76
  specification_version: 4
70
- summary: Simple Rails helpers to load Font Awesome from a CDN.
77
+ summary: Rails helpers to load Font Awesome via CDN or Kit and render icons in views
71
78
  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