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

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



71
72
73
74
# File 'app/models/material_icon.rb', line 71

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



102
103
104
105
# File 'app/models/material_icon.rb', line 102

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

#resetObject

Reset will set all variables to nil



11
12
13
14
# File 'app/models/material_icon.rb', line 11

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

#shape(name) ⇒ Object

Set the shape of the icon you want to use

Paremeters:

name

String or symbol with icon name

Returns:

MaterialIcon instance



56
57
58
59
# File 'app/models/material_icon.rb', line 56

def shape(name)
  @icon = name.to_s
  self
end

#style(css_style = '') ⇒ Object

Add CSS properties to :i tag

Paremeters:

style

String with CSS rules

Returns:

MaterialIcon instance



86
87
88
89
# File 'app/models/material_icon.rb', line 86

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

#to_aryObject

Some methods require to_ary to be defined. We return nil in this method to avoid this class to be threated as an Array

Returns

Nil



135
136
137
# File 'app/models/material_icon.rb', line 135

def to_ary
  nil
end

#to_sObject Also known as: to_str

Create the HTML code for the icon

Returns:

Safe string



113
114
115
116
117
118
119
120
121
122
# File 'app/models/material_icon.rb', line 113

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

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