Search code examples
ruby-on-railsgritter

Rails, Gritter - remove default title


I use Gritter notifications in Rails 5 app and can't find a way to remove default title of the notification popup. I add Gritter as bellow:

<%= js add_gritter(flash[:notice], title: 'I dont need you', sticky: false) %>

Tried:

<%= js add_gritter(flash[:notice], title: false, sticky: false) %>
<%= js add_gritter(flash[:notice], title: nil, sticky: false) %>
<%= js add_gritter(flash[:notice], title: '', sticky: false) %>
<%= js add_gritter(flash[:notice], title: ' ', sticky: false) %>

And still popup appears with the default title - "Notification". Tried to search in the whole app's project "Notification" or "gritter", but nothing related was found.

How to get rid of it?


Solution

  • The add_gritter method in the gem sets the options[:title] as "Notification" if options[:title].blank? returns true.

    The "dirty" option is to define it again, with a hash of options instead *args, and to render the title if it was passed as an option argument, like:

    def add_gritter(text, options={})
      if %w(success warning error notice progress).include?(options[:image].to_s)
        options[:image] = image_path("#{options[:image]}#{options[:image].to_s == 'progress' ? '.gif' : '.png'}") 
      end
      notification = Array.new
      notification.push("jQuery(function(){") if options[:nodom_wrap].blank?
      notification.push("jQuery.gritter.add({")
      notification.push("image:'#{options[:image]}',") if options[:image].present?
      notification.push("sticky:#{options[:sticky]},") if options[:sticky].present?
      notification.push("time:#{options[:time]},") if options[:time].present?
      notification.push("class_name:'#{options[:class_name]}',") if options[:class_name].present?
      notification.push("before_open:function(e){#{options[:before_open]}},") if options[:before_open].present?
      notification.push("after_open:function(e){#{options[:after_open]}},") if options[:after_open].present?
      notification.push("before_close:function(e){#{options[:before_close]}},") if options[:before_close].present?
      notification.push("after_close:function(e){#{options[:after_close]}},") if options[:after_close].present?
      notification.push("on_click:function(e){#{options[:on_click]}},") if options[:on_click].present?
      notification.push("title:'#{escape_javascript(options[:title])}',") if options[:title].blank? # Here
      notification.push("text:'#{escape_javascript(text)}'")
      notification.push("});")
      notification.push("});") if options[:nodom_wrap].blank?
      text.present? ? notification.join.html_safe : nil
    end
    

    But the gritter.js file has an if to check if the title has any content, so you should have to deal with your own and edit it, just to check for the text, like:

    //We might have some issues if we don't have a title or text!
    if (!params.text) {
      throw 'You need to fill out the text parameter.';
    }