Class: MaterialIcon

Inherits:
Object
  • Object
show all
Includes:
ActionView::Helpers::TagHelper
Defined in:
app/models/material_icon.rb

Overview

This class help the creation of material icons in the UI.

Instance Method Summary collapse

Dynamic Method Handling

This class handles dynamic methods through the method_missing method

#method_missing(name) ⇒ Object

Undefined method will ref to the icon.



9
10
11
12
# File 'app/models/material_icon.rb', line 9

def method_missing(name)
  @icon = clear_icon(name)
  self
end

Instance Method Details

#css_class(css_class = '') ⇒ Object

Add a CSS class to :i tag

Paremeters:

css_class

String with CSS classes

Returns:

MaterialIcon instance



52
53
54
55
# File 'app/models/material_icon.rb', line 52

def css_class(css_class = '')
  @css_class = " #{css_class}"
  self
end

#html(html = {}) ⇒ Object

Add HTML options to :i tag.

Paremeters:

html

Hash with options to add to :i tag. For example:

{ data: { id: 1 } }

Returns:

MaterialIcon instance



83
84
85
86
# File 'app/models/material_icon.rb', line 83

def html(html = {})
  @html = html
  self
end

#resetObject

Reset will set all variables to nil



17
18
19
20
# File 'app/models/material_icon.rb', line 17

def reset
  @icon, @rotation, @size, @html, @style, @css_class = [nil] * 6
  self
end

#style(style = '') ⇒ Object

Add CSS properties to :i tag

Paremeters:

style

String with CSS rules

Returns:

MaterialIcon instance



67
68
69
70
# File 'app/models/material_icon.rb', line 67

def style(style = '')
  @style = style
  self
end

#to_sObject

Create the HTML code for the icon. This method check if selected mode is unicode or ligatures.

Returns:

Safe string



102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
# File 'app/models/material_icon.rb', line 102

def to_s
  # Sanitize html
  @html = @html.nil? || !@html.is_a?(Hash) ? {} : @html

  # Create the icon
  if unicode?
    (:i, '',
                @html.merge(
                  style: @style,
                  class: "material-icons #{@icon}#{@size}#{@rotation}#{@css_class}"))
  else
    (:i, "#{@icon}",
                @html.merge(
                  style: @style,
                  class: "material-icons#{@size}#{@rotation}#{@css_class}"))
  end
end

#unicode?Boolean

Check based on rails config if the selected mode is unicode

Returns:

  • (Boolean)


91
92
93
# File 'app/models/material_icon.rb', line 91

def unicode?
  MaterialIcons.unicode
end