Module: Agilibox::FontAwesomeHelper
- Included in:
- AllHelpers
- Defined in:
- app/helpers/agilibox/font_awesome_helper.rb
Class Method Summary collapse
- .database ⇒ Object
- .database_path ⇒ Object
- .database_url ⇒ Object
- .database_yml ⇒ Object
- .default_fa_style_for_id(id) ⇒ Object
- .download_database! ⇒ Object
- .version ⇒ Object
Instance Method Summary collapse
Class Method Details
.database ⇒ Object
22 23 24 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 22 def database @database ||= YAML.safe_load(database_yml).deep_symbolize_keys end |
.database_path ⇒ Object
26 27 28 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 26 def database_path Rails.root.join("tmp", "fa_database_#{version}.yml") end |
.database_url ⇒ Object
35 36 37 38 39 40 41 42 43 44 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 35 def database_url short_version = version.split(".")[0, 3].join(".") # 1.20.14.2 => 1.20.14 url = "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/#{short_version}/metadata/icons.yml" if Gem::Version.new(short_version) < Gem::Version.new("5.6.0") url = url.gsub("/metadata", "/advanced-options/metadata") end url end |
.database_yml ⇒ Object
30 31 32 33 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 30 def database_yml download_database! unless File.size?(database_path) File.read(database_path) end |
.default_fa_style_for_id(id) ⇒ Object
57 58 59 60 61 62 63 64 65 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 57 def default_fa_style_for_id(id) return if version.start_with?("4") if version.start_with?("5") return Agilibox::FontAwesomeHelper.database.dig(id, :styles).to_a.first end raise "invalid font-awesome-sass version" end |
.download_database! ⇒ Object
46 47 48 49 50 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 46 def download_database! require "open-uri" data = URI.parse(database_url).open.read File.write(database_path, data) end |
.version ⇒ Object
52 53 54 55 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 52 def version require "font_awesome/sass/version" FontAwesome::Sass::VERSION end |
Instance Method Details
#icon(id, fa_style: nil, size: nil, spin: false, **options) ⇒ Object
2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 2 def icon(id, fa_style: nil, size: nil, spin: false, **) id = id.to_s.tr("_", "-").to_sym if fa_style.nil? fa_style = Agilibox::FontAwesomeHelper.default_fa_style_for_id(id) end css_classes = .delete(:class).to_s.split css_classes << "icon" css_classes << "fa-#{id}" css_classes << "fa#{fa_style.to_s[0]}" css_classes << "fa-#{size}" if size css_classes << "fa-spin" if spin attributes = .merge(class: css_classes.sort.join(" ")).sort.to_h tag.span(**attributes) end |