Module: Mtl::Rails::ViewHelpers
- Defined in:
- lib/mtl/rails/view_helpers.rb
Overview
Assortment of Rails view helpers to simplify rendering materialize css specific components like:
-
[Icons](#label-Icons),
-
[Avatars](#label-Avatars),
-
[Nav Toolbars / Headers](#label-Navbars+-2F+Headers)
-
or [Buttons](#label-Buttons)
## Buttons
Helper methods to create flat, raised and floating material UI buttons.
## Flat buttons
A simple flat button:
“‘erb <%= mtl_button_flat ’Cancel’, users_path %> “‘
A colored flat button:
“‘erb <%= mtl_button_flat ’Delete’, user_path(@user), class: ‘red-text’, method: :delete %> “‘
## Raised button
A default raised action button:
“‘erb <%= mtl_button “Next”, next_user_path %> “`
A raised button with a sprinkle of colour:
“‘erb <%= mtl_button “Ok”, ok_path, class: ’green darken-2’ “‘
## Floating button
A round red, large floating button:
“‘erb <%= mtl_button_floating :add, new_user_path, class: ’btn-large red’ “‘
#### Reference
-
[Materialize CSS: Buttons](materializecss.com/buttons.html)
-
[Google: Buttons](material.google.com/components/buttons.html)
-
[Google: Floating Action Buttons](material.google.com/components/buttons-floating-action-button.html)
# Navbars / Headers
Provides a helper to render a nice navbar, that is supposed to work on both mobile as well as desktops. It is meant to be used with the mtl-default-layout as defined by Marco.
Include the following in every template that requires a navbar:
“‘ <%= mtl_header t(’my.title’) %> “‘
To render with a custom additional header content, this is then simply appended after the title in the layout.
“‘erb <%= mtl_header ’Dashboard’ do %>
<ul class="right">
<li>Current User: Hans</li>
<li><%= link_to 'Logout', logout_path %></li>
</ul>
<%- end %> “‘
This renders the appropriate HTML snippet, see #materialize_header for more options.
#### Reference
-
[Materialize CSS: Navbar](materializecss.com/navbar.html)
## Avatars
A simple avatar link, without a picture
“‘erb <%= mtl_avatar_link ’/profile’, ‘John Doe’ %> “‘
Render a large avatar link, with a custom user picture:
“‘erb <%= mtl_avatar_link ’/profile’, ‘John Doe’, ‘/john_doe.png’, size: :large %> “‘
An avatar link, with a custom user picture and some html options
“‘erb <%= mtl_avatar_link ’/profile’, ‘John Doe’, ‘/john_doe.png’, class: :red %> “‘
### Unlinked avatars
A simple avatar, without a picture
“‘erb <%= mtl_avatar ’John Doe’ %> “‘
An avatar, with a picture
“‘erb <%= mtl_avatar ’John Doe’, ‘/john_doe.png’ %> “‘
An avatar, with a picture and some html options
“‘erb <%= mtl_avatar ’John Doe’, ‘/john_doe.png’, class: :red %> “‘
### Avatar initials
This helper generates the initials for the avatar, based on the person name. It supports combined names or emails, and will return either a single initial, either a 2 letter.
Examples:
-
John Doe -> JD
-
John Von Doe -> JD
-
John Albert Von Doe -> JD
-
[email protected] -> J
-
[email protected] -> JD
-
[email protected] -> JD
“‘erb <%= mtl_avatar_initials ’John Doe’ %> “‘
# Icons
Renders using using Google Material Icon font:
“‘erb <%= mtl_icon :cloud # renders a “cloud” icon %> <%= mtl_icon :cloud, size: :large # renders a large cloud icon %>
<%= mtl_icon :alert, class: ‘right red’ # add custom classes %> “‘
#### Reference
-
[List of available icons](design.google.com/icons/)
-
[Materialize CSS: Icons](materializecss.com/icons.html)
# Card file for files collection
Basic usage: “‘erb <%= mtl_card_file ’Document Dolorem.jpg’, ‘/path/to/file.jpg’ %> “‘ “`html <a class=“card-panel” href=“/path/to/file.jpg” target=“_blank” title=“Document Dolorem.jpg”>
Document Dolorem.jpg
image
JPG
</a> “‘
With title option: “‘erb <%= mtl_card_file ’Document Dolorem.jpg’, ‘/path/to/file.jpg’,
title: 'foo' %>
“‘ “`html <a class=“card-panel” href=“/path/to/file.jpg” target=“_blank” title=“foo”>
foo
Document Dolorem.jpg
image
JPG
</a> “‘
With type option: “‘erb <%= mtl_card_file ’Document Dolorem.jpg’, ‘/path/to/file.jpg’,
type: 'bar' %>
“‘ “`html <a class=“card-panel” href=“/path/to/file.jpg” target=“_blank” title=“Document Dolorem.jpg”>
Document Dolorem.jpg
insert_drive_file
BAR
</a> “‘
With preview option: “‘erb <%= mtl_card_file ’Document Dolorem.jpg’, ‘/path/to/file.jpg’,
preview: '/path/to/preview.jpg' %>
“‘ “`html <a class=“card-panel card-panel-image” href=“/path/to/file.jpg” target=“_blank”
title="Document Dolorem.jpg" style="background-image: url(/path/to/preview.jpg)">
Document Dolorem.jpg
image
JPG
</a> “‘
With delete option: “‘erb <%= mtl_card_file ’Document Dolorem.jpg’, ‘/path/to/file.jpg’,
delete: '/path/to/delete/the/file' %>
“‘ “`html <a class=“card-panel” href=“/path/to/file.jpg” target=“_blank” title=“Document Dolorem.jpg”>
Document Dolorem.jpg
image
JPG
close
</a> “‘
With confirm option: “‘erb <%= mtl_card_file ’Document Dolorem.jpg’, ‘/path/to/file.jpg’,
delete: '/path/to/delete/the/file',
confirm: 'Sure?' %>
“‘ “`html <a class=“card-panel” href=“/path/to/file.jpg” target=“_blank” title=“Document Dolorem.jpg”>
Document Dolorem.jpg
image
JPG
close
</a> “‘
# Navbars
Basic usage:
“‘haml
mtl_navbar do
Hello
“‘
Fixed navbar: “‘haml
mtl_navbar fixed: true do
Hello
“‘
Extended navbar: “‘haml
mtl_navbar do |nav|
Hello
= nav.extended do
Hellobis
“‘
Extended and fixed navbar: “‘haml
mtl_navbar fixed: true do |nav|
Hello
= nav.extended do
Hellobis
“‘
Instance Method Summary collapse
-
#mtl_avatar(name, image_url = nil, options = {}) ⇒ String
Renders an avatar, for the given name, with initials and an optional picture.
-
#mtl_avatar_link(url, name, image_url = nil, options = {}) ⇒ String
Renders an avatar link, for the given url with the name’s initials.
-
#mtl_button(label, url, options = {}) ⇒ String
Renders a visually raised button, i.e.
-
#mtl_button_flat(label, url, options = {}) ⇒ String
Renders a flat button which does not visually lift like the raised buttons.
-
#mtl_button_floating(icon, url, options = {}) ⇒ String
Renders a round floating button, with an icon as it’s label.
-
#mtl_button_more(url, options = {}) ⇒ String
Renders a flat button with the more_vert icon dots in a wrapper element.
-
#mtl_card_file(filename, href, options = {}) ⇒ String
Renders a card file tag to display the file and its informations, containing icons, file name, link and an optional preview.
-
#mtl_header(title = translate('.title', default: 'Menu'), **options) { ... } ⇒ String
Renders a specialized template for the header.
-
#mtl_icon(icon, options = {}) ⇒ String
Renders an ‘<i class=“material-icons”>icon</i>` tag to display an icon.
-
#mtl_navbar(options = {}) { ... } ⇒ String
Renders a navbar, wrapping the given block as content.
Instance Method Details
#mtl_avatar(name, image_url = nil, options = {}) ⇒ String
Renders an avatar, for the given name, with initials and an optional picture
396 397 398 399 400 |
# File 'lib/mtl/rails/view_helpers.rb', line 396 def mtl_avatar(name, image_url = nil, = {}) [:class] = ['mtl-avatar', .delete(:size), [:class]].compact image = (image_url.present? ? image_tag(image_url, alt: name) : '') content_tag :span, image + mtl_avatar_initials(name), end |
#mtl_avatar_link(url, name, image_url = nil, options = {}) ⇒ String
Renders an avatar link, for the given url with the name’s initials
381 382 383 384 385 386 387 |
# File 'lib/mtl/rails/view_helpers.rb', line 381 def mtl_avatar_link(url, name, image_url = nil, = {}) [:class] = [ 'mtl-avatar', Mtl.effects, .delete(:size), [:class] ].compact image = (image_url.present? ? image_tag(image_url, alt: name) : '') link_to image + mtl_avatar_initials(name), url, end |
#mtl_button(label, url, options = {}) ⇒ String
Renders a visually raised button, i.e. the default button style.
343 344 345 346 |
# File 'lib/mtl/rails/view_helpers.rb', line 343 def (label, url, = {}) [:class] = ['btn', Mtl.effects, [:class]].compact link_to label, url, end |
#mtl_button_flat(label, url, options = {}) ⇒ String
Renders a flat button which does not visually lift like the raised buttons.
302 303 304 305 |
# File 'lib/mtl/rails/view_helpers.rb', line 302 def (label, url, = {}) [:class] = ['btn-flat', [:class]].compact label, url, end |
#mtl_button_floating(icon, url, options = {}) ⇒ String
Renders a round floating button, with an icon as it’s label.
313 314 315 316 |
# File 'lib/mtl/rails/view_helpers.rb', line 313 def (icon, url, = {}) [:class] = ['btn-floating', [:class]].compact mtl_icon(icon), url, end |
#mtl_button_more(url, options = {}) ⇒ String
Renders a flat button with the more_vert icon dots in a wrapper element. This is intended to be used to render these “more buttons” on the right, in cards or the navbar.
“‘erb <%= mtl_button_more ’#dropdown’, ‘data-mtl-dropdown’: true %> “‘
329 330 331 332 333 334 335 |
# File 'lib/mtl/rails/view_helpers.rb', line 329 def (url, = {}) wrapper_class = ['btn-more-wrapper', .delete(:wrapper_class)].compact [:class] = ['btn-more', [:class]].compact.flatten btn = mtl_icon(:more_vert, size: :tiny), url, content_tag :div, btn, class: wrapper_class end |
#mtl_card_file(filename, href, options = {}) ⇒ String
Renders a card file tag to display the file and its informations, containing icons, file name, link and an optional preview
437 438 439 |
# File 'lib/mtl/rails/view_helpers.rb', line 437 def mtl_card_file(filename, href, = {}) CardFilePresenter.new(self).render(filename, href, ) end |
#mtl_header(title = translate('.title', default: 'Menu'), **options) { ... } ⇒ String
Renders a specialized template for the header.
363 364 365 366 367 368 369 370 371 |
# File 'lib/mtl/rails/view_helpers.rb', line 363 def mtl_header(title = translate('.title', default: 'Menu'), **, &block) mtl_class = ['mtl-layout-default-header', [:class]].compact.flatten.join(' ') mtl_nav = NavbarPresenter.new(self) mtl_content = block_given? ? capture(mtl_nav, &block) : nil render file: 'mtl/header', locals: { mtl_title: title.presence, mtl_back: .fetch(:back, false), mtl_menu: .fetch(:menu, 'nav-menu'), mtl_fixed: .fetch(:fixed, false), mtl_class: mtl_class, mtl_nav: mtl_nav, mtl_content: mtl_content } end |
#mtl_icon(icon, options = {}) ⇒ String
Renders an ‘<i class=“material-icons”>icon</i>` tag to display an icon.
420 421 422 423 |
# File 'lib/mtl/rails/view_helpers.rb', line 420 def mtl_icon(icon, = {}) [:class] = [Mtl.icon_class, .delete(:size), [:class]].compact content_tag :i, icon, end |
#mtl_navbar(options = {}) { ... } ⇒ String
Renders a navbar, wrapping the given block as content. Options also allows to fix the navbar and/or include an extended navigation
450 451 452 |
# File 'lib/mtl/rails/view_helpers.rb', line 450 def ( = {}, &block) NavbarPresenter.new(self).render(, &block) end |