fontawesome_cdn 0.1.0 → 0.2.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: f0227d9efe942dd8a799b7f22bf81e8e90ac47d3f9b2ffe86b1fe71ff2bb80c5
4
- data.tar.gz: 26af6254b9978d1a289ec4c2de04db8b997e895cf2589aa3f69a3133d6596dbb
3
+ metadata.gz: f6e25b39df453df95abdf3432c1a3d752c99ecb9f16a61c722a61144589b5cdb
4
+ data.tar.gz: fc6374becd7e0a3c9331b76560e342818acda650554aabb234961ca4f908299f
5
5
  SHA512:
6
- metadata.gz: 341e6dfd4d3ce75275d8a7b8ada5e53705776feb01ac925c680c9fdcca2c2909a28cc242050b3c75e1596922489edaa122eb4b64c3b983c2709a08fddefdac7c
7
- data.tar.gz: 64c3662608445300d8b8c0840af73fee0a246ccab95b94794c8574b9d740e07e608b682ad054fddbd908675c52e2053210f6b98788df462c41bb06ee3769427c
6
+ metadata.gz: 5fb96ae5eb2718f69423fcd5627e2a5d2571a23453bed4b66c742219f9b83423e9d6cd076dc21e426d829159356e24318f94bcafe10acb0d92f04fac7d8d4b6d
7
+ data.tar.gz: e679249b19bbe2acc6622fed33d9995d6806efeee854656de21c434d99e0049658fe9494f774d34e42d4924872f8b27df8d4d898a84e51543240da310d64be1e
data/CHANGELOG.md CHANGED
@@ -6,6 +6,35 @@ This project follows [Semantic Versioning](https://semver.org).
6
6
 
7
7
  ---
8
8
 
9
+ ## 0.2.0
10
+
11
+ ### Changed
12
+ - Simplified the `icon` helper API.
13
+ - The helper no longer requires passing full Font Awesome class names (`fa-solid`, `fa-regular`, `fa-brands`).
14
+ - Introduced explicit `style:` and `pack:` options (accepted as strings or symbols).
15
+
16
+ ### Before
17
+ ```erb
18
+ <%= icon "fa-solid", "user" %>
19
+ <%= icon "fa-regular", "bell" %>
20
+ <%= icon "fa-brands", "font-awesome", "Font Awesome" %>
21
+ ```
22
+
23
+ ### After
24
+ ```erb
25
+ <%= icon "user" %>
26
+ <%= icon "gear", "Settings" %>
27
+ <%= icon "trash-can", "Delete", style: :regular %>
28
+ <%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
29
+ ```
30
+
31
+ ### Notes
32
+ - `style:` defaults to `"solid"` unless `pack: "brands"` is used.
33
+ - Stricter validation prevents invalid style/pack combinations.
34
+ - Improved readability and consistency with Font Awesome's official naming.
35
+
36
+ ---
37
+
9
38
  ## 0.1.0
10
39
 
11
40
  ### Added
@@ -15,4 +44,3 @@ This project follows [Semantic Versioning](https://semver.org).
15
44
  - `icon` helper to render Font Awesome icons in Rails views
16
45
  - Support for Font Awesome 7
17
46
  - Compatibility with Rails 7 and Rails 8
18
-
data/README.md CHANGED
@@ -1,5 +1,6 @@
1
1
  # 🎨 FontawesomeCdn
2
2
 
3
+ [![Gem Version](https://img.shields.io/gem/v/fontawesome_cdn.svg)](https://rubygems.org/gems/fontawesome_cdn)
3
4
  [![CI](https://github.com/OpenCodeForge/fontawesome_cdn/actions/workflows/ci.yml/badge.svg)](https://github.com/OpenCodeForge/fontawesome_cdn/actions/workflows/ci.yml)
4
5
 
5
6
  Simple Rails helpers to load **Font Awesome via CDN** and render icons in views.
@@ -41,16 +42,17 @@ Place this helper in your layout, inside `<head>`.
41
42
  Use this helper anywhere in your views or partials.
42
43
 
43
44
  ```erb
44
- <%= icon "fa-solid", "user" %>
45
- <%= icon "fa-regular", "bell", class: "fa-2x" %>
46
- <%= icon "fa-brands", "font-awesome", "Font Awesome" %>
45
+ <%= icon "user" %>
46
+ <%= icon "gear", "Settings" %>
47
+ <%= icon "trash-can", "Delete", style: :regular %>
48
+ <%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
47
49
  ```
48
50
 
49
51
  ---
50
52
 
51
53
  ## ✅ Supported Font Awesome versions
52
54
 
53
- Font Awesome is loaded directly from **cdnjs**.
55
+ Font Awesome is loaded directly from [cdnjs](https://cdnjs.com/libraries/font-awesome).
54
56
 
55
57
  **Supported versions** (with verified SRI):
56
58
 
@@ -13,16 +13,17 @@ module FontawesomeCdn
13
13
 
14
14
  SUPPORTED_VERSIONS = INTEGRITY_MAP.keys.freeze
15
15
 
16
+ ICON_STYLES = %w[solid regular light thin semibold].freeze
17
+ DEFAULT_ICON_STYLE = "solid"
18
+
16
19
  # Helpers exposed to Rails views.
17
20
  #
18
21
  # Provides:
19
22
  # - fontawesome_cdn_stylesheet_tag(version)
20
- # - icon(style, name, text = nil, html_options = {})
23
+ # - icon(name, text = nil, **options)
21
24
  module Helpers
22
25
  # Stylesheet helper for loading Font Awesome via CDN
23
26
  #
24
- # Usage:
25
- #
26
27
  # <%= fontawesome_cdn_stylesheet_tag "7.0.1" %>
27
28
  #
28
29
  # Raises ArgumentError if version is not supported.
@@ -45,28 +46,32 @@ module FontawesomeCdn
45
46
 
46
47
  # Main helper for displaying an icon
47
48
  #
48
- # <%= icon "fa-solid", "user" %>
49
- # <%= icon "fa-regular", "bell", class: "fa-2x fa-shake" %>
50
- # <%= icon "fa-brands", "font-awesome", "Font Awesome" %>
51
- # <%= icon "fa-solid", "check", "aria-hidden": false %>
49
+ # <%= icon "user" %>
50
+ # <%= icon "gear", "Settings" %>
51
+ # <%= icon "trash-can", "Delete", style: :regular %>
52
+ # <%= icon "github", pack: :brands, class: "fa-2x fa-shake" %>
52
53
  #
53
- def icon(style, name, text = nil, html_options = {})
54
+ def icon(name, text = nil, **options)
55
+ # Allow the 2nd argument to be either text or the options hash
54
56
  if text.is_a?(Hash)
55
- html_options = text
57
+ options = text
56
58
  text = nil
57
59
  end
58
60
 
59
- classes = [style, "fa-#{name}", html_options[:class]].compact
60
- html_options[:class] = classes.join(" ")
61
- html_options["aria-hidden"] ||= true
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")
62
64
 
63
- icon_tag = tag.i(nil, **html_options)
65
+ icon_tag = tag.i(nil, **options)
64
66
 
65
- text.blank? ? icon_tag : safe_join([icon_tag, " ", text.to_s])
67
+ text.nil? || text.to_s.strip.empty? ? icon_tag : safe_join([icon_tag, " ", text.to_s])
66
68
  end
67
69
 
68
70
  private
69
71
 
72
+ #
73
+ # Font Awesome version validation
74
+ #
70
75
  def validate_fontawesome_version!(version)
71
76
  raise_version_required_error if version.nil?
72
77
  raise_version_type_error(version) unless version.is_a?(String)
@@ -92,5 +97,41 @@ module FontawesomeCdn
92
97
  Supported versions: #{FontawesomeCdn::SUPPORTED_VERSIONS.join(", ")}
93
98
  MSG
94
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
95
136
  end
96
137
  end
@@ -2,5 +2,5 @@
2
2
 
3
3
  # Current version of the FontawesomeCdn gem.
4
4
  module FontawesomeCdn
5
- VERSION = "0.1.0"
5
+ VERSION = "0.2.0"
6
6
  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.1.0
4
+ version: 0.2.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - OpenCodeForge