-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhelpers.rb
54 lines (44 loc) · 1.57 KB
/
helpers.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
# HELPERS
module Helpers
def image_tag(image, options = {})
# This helper generates an image tag
# These options are available:
# :size - image dimensions (e.g. "100x100")
# :alt - alt text for image
# :lazy - lazy load image (true or false)
# Get image filename (but remove @1x and @2x resolution suffixes)
image_name = image.gsub(/@[1-2]x/, "")
# Set image source
options[:src] = image
# Set image srcset (if image filename contains @1x)
if image.include? "@1x"
options[:srcset] = "#{image} 1x, #{image.sub("@1x", "@2x")} 2x"
end
# Set height and width
if options[:size]
options[:width] = options[:size].split("x")[0]
options[:height] = options[:size].split("x")[1]
options.delete(:size)
end
# Set fallback alt text using image name
# (only use this if image has no :alt text)
unless options[:alt]
options[:alt] = File.basename(image_name, File.extname(image_name))
end
# Disable normal image src if lazy loading image
if options[:lazy] == "true"
options["data-src"] = options[:src]
options.delete(:src)
end
# Insert attributes into image tag
attributes = options.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
attributes = " " + attributes unless attributes.empty?
"<img#{attributes}>"
end
def link_to(text, url, options = {})
# This helper generates a link tag
attributes = options.map { |k, v| "#{k}=\"#{v}\"" }.join(" ")
attributes = " " + attributes unless attributes.empty?
"<a href=\"#{url}\"#{attributes}>#{text}</a>"
end
end