Module: Ramaze::Helper::Message

Defined in:
lib/zen/helper/message.rb

Overview

The Mesage helper is a Ruby implementation of the Codeigniter library “Message” (located here: https://github.com/isset/codeigniter-message). This helper was taken from another project of mine which can be found here: https://github.com/yorickpeterse/stumpert/

Usage

Basic usage is as following:

message(:error, "Bummer, something went wrong!")

In your layout you’d do the following:

display_messages

This will create and return the HTML for all the messages.

Since:

Instance Method Summary (collapse)

Instance Method Details

- (Object) display_messages(types = [:info, :error, :success])

Renders all the messages for the specified types.

Parameters:

  • types (Array) (defaults to: [:info, :error, :success])

    Array containing all the messages to render.

Since:

  • 26-05-2011



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/zen/helper/message.rb', line 51

def display_messages(types = [:info, :error, :success])
  gestalt = ::Ramaze::Gestalt.new

  return if flash[:messages].nil?
  
  gestalt.div(:id => 'message_container', :class => 'container') do
    # Render each individual group
    types.each do |type|
      if type.respond_to?(:to_sym)
        type = type.to_sym
      end

      if flash[:messages].key?(type)
        gestalt.div(:class => "message #{type}") do
          # Render all the messages
          flash[:messages][type].each_with_index do |message, index|
            flash[:messages][type].delete_at(index)
            gestalt.p { message }
          end
        end
      end
    end
  end

  return gestalt.to_s
end

- (Object) message(type, message)

Adds a new message to the list for the given type.

Parameters:

  • type (Symbol/String)

    The type of message to store (e.g. “error”).

  • message (String)

    The message to display.

Since:

  • 26-05-2011



35
36
37
38
39
40
41
42
43
# File 'lib/zen/helper/message.rb', line 35

def message(type, message)
  if type.respond_to?(:to_sym)
    type = type.to_sym
  end

  flash[:messages]       ||= {}
  flash[:messages][type] ||= []
  flash[:messages][type].push(message)
end