Class: Dato::Utils::FaviconTagsBuilder

Inherits:
Object
  • Object
show all
Defined in:
lib/dato/utils/favicon_tags_builder.rb

Constant Summary collapse

APPLE_TOUCH_ICON_SIZES =
[57, 60, 72, 76, 114, 120, 144, 152, 180].freeze
ICON_SIZES =
[16, 32, 96, 192].freeze
WINDOWS_SIZES =
[[70, 70], [150, 150], [310, 310], [310, 150]].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(site, theme_color) ⇒ FaviconTagsBuilder

Returns a new instance of FaviconTagsBuilder.



12
13
14
15
# File 'lib/dato/utils/favicon_tags_builder.rb', line 12

def initialize(site, theme_color)
  @site = site
  @theme_color = theme_color
end

Instance Attribute Details

#siteObject (readonly)

Returns the value of attribute site.



6
7
8
# File 'lib/dato/utils/favicon_tags_builder.rb', line 6

def site
  @site
end

#theme_colorObject (readonly)

Returns the value of attribute theme_color.



6
7
8
# File 'lib/dato/utils/favicon_tags_builder.rb', line 6

def theme_color
  @theme_color
end

Instance Method Details

#build_app_name_tagObject



60
61
62
# File 'lib/dato/utils/favicon_tags_builder.rb', line 60

def build_app_name_tag
  meta_tag("application-name", site.name)
end

#build_apple_icon_tagsObject



27
28
29
30
31
32
33
34
35
36
37
# File 'lib/dato/utils/favicon_tags_builder.rb', line 27

def build_apple_icon_tags
  return unless site.favicon

  APPLE_TOUCH_ICON_SIZES.map do |size|
    link_tag(
      "apple-touch-icon",
      url(size),
      sizes: "#{size}x#{size}",
    )
  end
end

#build_color_tagsObject



64
65
66
67
68
69
70
71
# File 'lib/dato/utils/favicon_tags_builder.rb', line 64

def build_color_tags
  return unless theme_color

  [
    meta_tag("theme-color", theme_color),
    meta_tag("msapplication-TileColor", theme_color),
  ]
end

#build_icon_tagsObject



39
40
41
42
43
44
45
46
47
48
49
50
# File 'lib/dato/utils/favicon_tags_builder.rb', line 39

def build_icon_tags
  return unless site.favicon

  ICON_SIZES.map do |size|
    link_tag(
      "icon",
      url(size),
      sizes: "#{size}x#{size}",
      type: "image/#{site.favicon.format}",
    )
  end
end

#build_windows_tagsObject



52
53
54
55
56
57
58
# File 'lib/dato/utils/favicon_tags_builder.rb', line 52

def build_windows_tags
  return unless site.favicon

  WINDOWS_SIZES.map do |(w, h)|
    meta_tag("msapplication-square#{w}x#{h}logo", url(w, h))
  end
end


81
82
83
# File 'lib/dato/utils/favicon_tags_builder.rb', line 81

def link_tag(rel, href, attrs = {})
  { tag_name: "link", attributes: attrs.merge(rel: rel, href: href) }
end

#meta_tag(name, value) ⇒ Object



77
78
79
# File 'lib/dato/utils/favicon_tags_builder.rb', line 77

def meta_tag(name, value)
  { tag_name: "meta", attributes: { name: name, content: value } }
end

#meta_tagsObject



17
18
19
20
21
22
23
24
25
# File 'lib/dato/utils/favicon_tags_builder.rb', line 17

def meta_tags
  [
    build_icon_tags,
    build_apple_icon_tags,
    build_windows_tags,
    build_color_tags,
    build_app_name_tag,
  ].flatten.compact
end

#url(width, height = width) ⇒ Object



73
74
75
# File 'lib/dato/utils/favicon_tags_builder.rb', line 73

def url(width, height = width)
  site.favicon.url(w: width, h: height)
end