Class: Formstrap::ThumbnailView

Inherits:
ViewModel show all
Defined in:
app/models/formstrap/thumbnail_view.rb

Instance Method Summary collapse

Methods inherited from ViewModel

#attributes, #initialize, #to_hash

Constructor Details

This class inherits a constructor from ViewModel

Dynamic Method Handling

This class handles dynamic methods through the method_missing method in the class ViewModel

Instance Method Details

#blobObject



25
26
27
# File 'app/models/formstrap/thumbnail_view.rb', line 25

def blob
  file.is_a?(ActiveStorage::Blob) ? file : file&.blob
end

#class_namesObject



3
4
5
6
7
# File 'app/models/formstrap/thumbnail_view.rb', line 3

def class_names
  class_names = [@class]
  class_names << "img-thumbnail formstrap-thumbnail"
  class_names.join(" ")
end

#default_variant_optionsObject



45
46
47
48
49
50
51
# File 'app/models/formstrap/thumbnail_view.rb', line 45

def default_variant_options
  {
    saver: {
      quality: 80
    }
  }
end

#heightObject



17
18
19
20
21
22
23
# File 'app/models/formstrap/thumbnail_view.rb', line 17

def height
  if is_defined?(:height)
    @height
  else
    @height || 100
  end
end

#icon?Boolean

Returns:

  • (Boolean)


58
59
60
# File 'app/models/formstrap/thumbnail_view.rb', line 58

def icon?
  is_defined?(:icon)
end

#icon_nameObject



63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# File 'app/models/formstrap/thumbnail_view.rb', line 63

def icon_name
  return icon if icon
  type_map = {
    image: %w[image/bmp image/gif image/vnd.microsoft.icon image/jpeg image/png image/svg+xml image/tiff image/webp],
    play: %w[video/mp4 video/mpeg video/ogg video/mp2t video/webm video/3gpp video/3gpp2],
    music: %w[audio/aac audio/midi audio/x-midi audio/mpeg audio/ogg audio/opus audio/wav audio/webm audio/3gpp audio/3gpp2],
    word: %w[application/msword application/vnd.openxmlformats-officedocument.wordprocessingml.document],
    ppt: %w[application/vnd.ms-powerpoint application/vnd.openxmlformats-officedocument.presentationml.presentation],
    excel: %w[application/vnd.ms-excel application/vnd.openxmlformats-officedocument.spreadsheetml.sheet],
    slides: %w[application/vnd.oasis.opendocument.presentation],
    spreadsheet: %w[application/vnd.oasis.opendocument.spreadsheet],
    richtext: %w[application/vnd.oasis.opendocument.text],
    zip: %w[application/zip application/x-7z-compressed application/x-bzip application/x-bzip2 application/gzip application/vnd.rar],
    pdf: %w[application/pdf]
  }

  type = type_map.find { |key, mime_types| mime_types.include?(mime_type) }
  if type
    ["file", "earmark", type.first].compact.join("-")
  else
    "question"
  end
end

#inline_svg(options = {}) ⇒ Object



91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
# File 'app/models/formstrap/thumbnail_view.rb', line 91

def inline_svg(options = {})
  blob.open do |file|
    content = file.read
    doc = Nokogiri::HTML::DocumentFragment.parse content
    svg = doc.at_css "svg"

    # for security
    doc.search("script").each do |src|
      src.remove
    end

    options.each { |attr, value| svg[attr.to_s] = value }

    doc.to_html.html_safe
  end
end

#mime_typeObject



29
30
31
# File 'app/models/formstrap/thumbnail_view.rb', line 29

def mime_type
  blob&.content_type
end

#svg?Boolean

Returns:

  • (Boolean)


87
88
89
# File 'app/models/formstrap/thumbnail_view.rb', line 87

def svg?
  blob&.content_type == "image/svg+xml"
end

#variable?Boolean

Is it possible to create a ActiveStorage::Variant?

Returns:

  • (Boolean)


54
55
56
# File 'app/models/formstrap/thumbnail_view.rb', line 54

def variable?
  blob&.variable?
end

#variant_optionsObject



33
34
35
36
37
38
39
40
41
42
43
# File 'app/models/formstrap/thumbnail_view.rb', line 33

def variant_options
  if width.nil? || height.nil?
    default_variant_options.merge({
      resize_to_fit: [width, height]
    })
  else
    default_variant_options.merge({
      resize_to_fill: [width, height, crop: :attention]
    })
  end
end

#widthObject



9
10
11
12
13
14
15
# File 'app/models/formstrap/thumbnail_view.rb', line 9

def width
  if is_defined?(:width)
    @width
  else
    @width || 100
  end
end