Module: Agilibox::FontAwesomeHelper
- Included in:
- AllHelpers
- Defined in:
- app/helpers/agilibox/font_awesome_helper.rb
Class Method Summary collapse
-
.database ⇒ Object
rubocop:disable Rails/HelperInstanceVariable.
-
.database_path ⇒ Object
rubocop:enable Rails/HelperInstanceVariable.
- .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
rubocop:disable Rails/HelperInstanceVariable
23 24 25 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 23 def database @database ||= YAML.safe_load(database_yml).deep_symbolize_keys end |
.database_path ⇒ Object
rubocop:enable Rails/HelperInstanceVariable
28 29 30 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 28 def database_path Rails.root.join("tmp", "fa_database_#{version}.yml") end |
.database_url ⇒ Object
37 38 39 40 41 42 43 44 45 46 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 37 def database_url short_version = version[0, 5] url = "https://raw.githubusercontent.com/FortAwesome/Font-Awesome/#{short_version}/metadata/icons.yml" if short_version < "5.6.0" url = url.gsub("/metadata", "/advanced-options/metadata") end url end |
.database_yml ⇒ Object
32 33 34 35 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 32 def database_yml download_database! unless File.size?(database_path) File.read(database_path) end |
.default_fa_style_for_id(id) ⇒ Object
59 60 61 62 63 64 65 66 67 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 59 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
48 49 50 51 52 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 48 def download_database! require "open-uri" data = URI.parse(database_url).open.read File.write(database_path, data) end |
.version ⇒ Object
54 55 56 57 |
# File 'app/helpers/agilibox/font_awesome_helper.rb', line 54 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 content_tag(:span, attributes) {} end |