Class: LocaleFlash::Flash

Inherits:
Object
  • Object
show all
Defined in:
lib/locale_flash/flash.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(args = {}) ⇒ Flash

Returns a new instance of Flash.



5
6
7
8
9
10
11
# 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

Instance Attribute Details

#actionObject (readonly)

Returns the value of attribute action.



3
4
5
# File 'lib/locale_flash/flash.rb', line 3

def action
  @action
end

#controllerObject (readonly)

Returns the value of attribute controller.



3
4
5
# File 'lib/locale_flash/flash.rb', line 3

def controller
  @controller
end

#messageObject (readonly)

Returns the value of attribute message.



3
4
5
# File 'lib/locale_flash/flash.rb', line 3

def message
  @message
end

#optionsObject (readonly)

Returns the value of attribute options.



3
4
5
# File 'lib/locale_flash/flash.rb', line 3

def options
  @options
end

#typeObject (readonly)

Returns the value of attribute type.



3
4
5
# File 'lib/locale_flash/flash.rb', line 3

def type
  @type
end

Class Method Details

.from_params(type, params = {}) ⇒ Object



13
14
15
16
17
18
19
20
21
# 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

Instance Method Details

#controllersObject



49
50
51
# File 'lib/locale_flash/flash.rb', line 49

def controllers
  controller.split('/')
end

#fallbacksObject



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
# 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

#keyObject



53
54
55
# File 'lib/locale_flash/flash.rb', line 53

def key
  ['controllers', controllers.join('.'), action, 'flash', type].join('.').to_sym
end

#legacy?Boolean

Returns:

  • (Boolean)


45
46
47
# File 'lib/locale_flash/flash.rb', line 45

def legacy?
  !message.nil?
end

#to_htmlObject



75
76
77
78
79
80
81
82
# 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_paramsObject



39
40
41
42
43
# File 'lib/locale_flash/flash.rb', line 39

def to_params
  { :controller => controller,
    :action     => action,
    :options    => options }
end