Module: GritterNotices::ViewHelpers

Includes:
Gritter::Helpers
Defined in:
lib/gritter_notices/view_helpers.rb

Constant Summary collapse

GRITTER_CONVERT_KEYS =
{ :info=>:notice, :alert=>:warning, :failure=>:error, :other=>:notice }.
merge Hash[*GritterNotices::KEYS.map {|k| [k,k]

Instance Method Summary collapse

Instance Method Details

#add_flashes_to_gritters(gritters, list, titles) ⇒ Object



42
43
44
45
46
47
48
49
50
51
52
53
54
55
# File 'lib/gritter_notices/view_helpers.rb', line 42

def add_flashes_to_gritters(gritters, list, titles)
  list.to_hash.each_pair do |key, message_array|
    next unless message_array
    message_array = [message_array] unless message_array.is_a? Array
    message_array.flatten.each { |message|
      key = key.to_sym
      # Превращаем системные флешки в гритеровские
      gritter_key = GRITTER_CONVERT_KEYS[key] || GRITTER_CONVERT_KEYS[:other] || key
      title = titles[gritter_key] || key.to_s
      gritters << add_gritter(message, :image => gritter_key, :title => title)
    }
  end

end

#add_notices_to_gitters(gritters, list, titles) ⇒ Object



31
32
33
34
35
36
37
38
39
40
# File 'lib/gritter_notices/view_helpers.rb', line 31

def add_notices_to_gitters(gritters, list, titles)
  list.each do |notice|
    options = notice.options
    gritter_key = GRITTER_CONVERT_KEYS[options[:level]] || GRITTER_CONVERT_KEYS[:other] || options[:level]
    options[:title] = titles[gritter_key] || options[:level].to_s  unless options[:title]
    options[:image] = gritter_key unless options[:image]
    gritters << add_gritter(notice.text, options)
    notice.mark_as_delivered
  end
end

#gritter_flash_messages(*args) ⇒ Object

Ключи понимаемые гриттером: :success, :warning, :notice, :error, :progress

Все возможные (системные): :success, :warning, :notice, :error, :alert, :info, :failure



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/gritter_notices/view_helpers.rb', line 18

def gritter_flash_messages *args
  options = args.extract_options!
  titles = gflash_titles(options)
  gritters = []

  # Собираем flash-сообщения
  add_flashes_to_gritters( gritters, flash, titles )
  defined?(current_user) and current_user.respond_to?(:gritter_notices) and
    add_notices_to_gitters( gritters, current_user.gritter_notices.fresh, titles)

  js(gritters).html_safe unless gritters.empty?
end

#move_gritter_notices_to_flashesObject



57
58
59
60
61
62
63
# File 'lib/gritter_notices/view_helpers.rb', line 57

def move_gritter_notices_to_flashes
  return unless current_user
  current_user.gritter_notices.fresh.each do |notice|
    append_flash_message notice.level, notice.text
    notice.mark_as_delivered
  end
end