Module: Agilibox::FontAwesomeHelper

Included in:
AllHelpers
Defined in:
app/helpers/agilibox/font_awesome_helper.rb

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.databaseObject



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_pathObject



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_urlObject



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_ymlObject



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

.versionObject



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, **options)
  id = id.to_s.tr("_", "-").to_sym

  if fa_style.nil?
    fa_style = Agilibox::FontAwesomeHelper.default_fa_style_for_id(id)
  end

  css_classes = options.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 = options.merge(class: css_classes.sort.join(" ")).sort.to_h

  tag.span(**attributes)
end