Module: Reusable::Rails::Controllers::FlashMessages

Extended by:
ActiveSupport::Concern
Defined in:
lib/reusable/rails/controllers/flash_messages.rb

Instance Method Summary collapse

Methods included from ActiveSupport::Concern

append_features, extended, included

Instance Method Details

#flash_message(type, text) ⇒ Hash

Helper for supporting multiple flash messages per type

:success, :notice, :error

Parameters:

  • the (Symbol)

    type of flash message. Common types are

  • the (String)

    message to attach to the flash type

Returns:

  • (Hash)

    all associated flash messages for this request



16
17
18
19
# File 'lib/reusable/rails/controllers/flash_messages.rb', line 16

def flash_message(type, text)
  flash[type.to_sym] ||= []
  flash[type.to_sym] << text
end

#flash_messages(options = {}) ⇒ Object

Return any flash messages added for the request, optionally can be formatted to return a JSON representation.



25
26
27
28
29
30
31
32
33
# File 'lib/reusable/rails/controllers/flash_messages.rb', line 25

def flash_messages(options={})
  if !!options[:json]
    Hash[flash.collect do |type, messages|
      [k, messages.is_a?(Array) ? messages.collect {|i| ERB::Util.h(i)} : ERB::Util.h(messages)]
    end].to_json
  else
    flash
  end
end