Skip to content

Instantly share code, notes, and snippets.

@dannyvassallo
Last active November 24, 2016 00:29
Show Gist options
  • Save dannyvassallo/8768f8ac13e4f422ec7d to your computer and use it in GitHub Desktop.
Save dannyvassallo/8768f8ac13e4f422ec7d to your computer and use it in GitHub Desktop.
Replace Rails 4 confirmation/prompts with a toast message from Dogfalo Materialize CSS. Regular repo - https://github.com/Dogfalo/materialize Sass Gem - https://github.com/mkhairi/materialize-sass Website - http://materializecss.com/
$ ->
$.rails.allowAction = (link) ->
return true unless link.attr("data-confirm")
$.rails.showConfirmDialog link
false
$.rails.confirmed = (link) ->
link.removeAttr "data-confirm"
link.trigger "click.rails"
$.rails.showConfirmDialog = (link) ->
html = undefined
message = undefined
message = link.attr("data-confirm")
html = "<div id=\"modal1\" class=\"modal\" style=\"z-index: 1003; display: block; opacity: 1; transform: scaleX(1); top: 10%;\"> <div class=\"modal-content\"><h4>" + message + "</h4></div><div class=\"modal-footer\"><a class=\"modal-action modal-close waves-effect waves-red btn-flat close\">Cancel</a><a class=\"modal-action modal-close waves-effect waves-green btn-flat confirm\">OK</a></div></div>"
$("body").append html
$("#modal1").openModal complete: ->
$("#modal1").remove()
return $("#modal1 .confirm").on("click", ->
$.rails.confirmed link
)
$("#modal1 .close").on "click", ->
$("#modal1").closeModal()
$(function() {
$.rails.allowAction = function(link) {
if (!link.attr('data-confirm')) {
return true;
}
$.rails.showConfirmDialog(link);
return false;
};
$.rails.confirmed = function(link) {
link.removeAttr('data-confirm');
return link.trigger('click.rails');
};
$.rails.showConfirmDialog = function(link) {
var html, message;
message = link.attr('data-confirm');
html = "<div id=\"modal1\" class=\"modal\" style=\"z-index: 1003; display: block; opacity: 1; transform: scaleX(1); top: 10%;\"> <div class=\"modal-content\"><h4>" + message + "</h4></div><div class=\"modal-footer\"><a class=\"modal-action modal-close waves-effect waves-red btn-flat close\">Cancel</a><a class=\"modal-action modal-close waves-effect waves-green btn-flat confirm\">OK</a></div></div>";
$('body').append(html);
$('#modal1').openModal({
complete: function() {
$('#modal1').remove();
}
});
return $('#modal1 .confirm').on('click', function() {
return $.rails.confirmed(link);
});
return $('#modal1 .close').on('click', function() {
return $('#modal1').closeModal();
});
};
});
@dannyvassallo
Copy link
Author

Add either the coffee or js file in the /app/assets/javascripts/ folder and add the line //= require _materialize-rails-confirm.js or `//= require _materialize-rails-confirm.jsto yourapplication.js`` file repectively. Your Rails 4 JS prompts will now be materialized!

@chris-gooley
Copy link

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment