class LocaleFlash::Flash

Attributes

action[R]
controller[R]
message[R]
options[R]
type[R]

Public Class Methods

from_params(type, params={}) click to toggle source
# File lib/locale_flash/flash.rb, line 13
def self.from_params(type, params={})
  if params_hash_stringkeys? params
    build_from_hash_stringkeys type, params
  elsif params.is_a?(Hash) && params[:controller] && params[:action]
    new(params.merge(:type => type))
  else
    new(:type => type, :message => params.to_s)
  end
end
new(args={}) click to toggle source
# File lib/locale_flash/flash.rb, line 5
def initialize(args={})
  @type       = args[:type]
  @controller = args[:controller]
  @action     = args[:action]
  @options    = args[:options] || {}
  @message    = args[:message]
end

Private Class Methods

build_from_hash_stringkeys(type, params) click to toggle source
# File lib/locale_flash/flash.rb, line 23
                     def self.build_from_hash_stringkeys(type, params)
  new(
    :type       => type,
    :controller => params["controller"],
    :action     => params["action"],
    :options    => Hash[params["options"].map do |key, value|
      [key.to_sym, value]
    end]
  )
end
params_hash_stringkeys?(params) click to toggle source
# File lib/locale_flash/flash.rb, line 34
                     def self.params_hash_stringkeys?(params)
  return false unless params.kind_of? Hash
  %w[controller action].all? { |key| params.key? key }
end

Public Instance Methods

controllers() click to toggle source
# File lib/locale_flash/flash.rb, line 49
def controllers
  controller.split('/')
end
fallbacks() click to toggle source
# File lib/locale_flash/flash.rb, line 57
def fallbacks
  if controllers.size > 1
    defaults = []
    controllers.each_with_index do |current, i|
      current    = controllers[0, (controllers.size - i)].join('.')
      parent     = controllers[0, (controllers.size - i - 1)].join('.')
      defaults << ['controllers', current, 'flash', type]
      defaults << ['controllers', parent, 'flash', action, type] unless parent.empty?
    end
  else
    defaults = [['controllers', controller, 'flash', type]]
  end

  defaults << ['controllers', 'flash', action, type]
  defaults << ['controllers', 'flash', type]
  defaults.map { |i| i.join('.').to_sym }
end
key() click to toggle source
# File lib/locale_flash/flash.rb, line 53
def key
  ['controllers', controllers.join('.'), action, 'flash', type].join('.').to_sym
end
legacy?() click to toggle source
# File lib/locale_flash/flash.rb, line 45
def legacy?
  !message.nil?
end
to_html() click to toggle source
# File lib/locale_flash/flash.rb, line 75
def to_html
  str = if legacy?
    message
  else
    I18n.t(key, options.merge(:default => fallbacks))
  end
  LocaleFlash::Config.template.call(type, str)
end
to_params() click to toggle source
# File lib/locale_flash/flash.rb, line 39
def to_params
  { :controller => controller,
    :action     => action,
    :options    => options }
end