Module: Spurs::Flash::Helper

Defined in:
lib/spurs/flash/helper.rb

Constant Summary collapse

@@flashes_container_class =

the default css class of the container for flash messages

"spurs_flash_messages"
@@flashes_container_id_prefix =

the prefix of the id for containers for flash messages

"spurs_flash_messages_"
@@default_messages_options =
{
    :priority => 10,
    :builder  => Spurs::Flash::Builder
}

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.default_messages_optionsObject



28
29
30
# File 'lib/spurs/flash/helper.rb', line 28

def self.default_messages_options
  @@class.default_messages_options
end

.flashes_container_classObject



8
9
10
# File 'lib/spurs/flash/helper.rb', line 8

def self.flashes_container_class
  @@flashes_container_class
end

.flashes_container_id_prefixObject



17
18
19
# File 'lib/spurs/flash/helper.rb', line 17

def self.flashes_container_id_prefix
  @@flashes_container_id_prefix
end

Instance Method Details

#loggerObject



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

def logger
  Rails.logger
end

#spurs_alert(message, options = { }) ⇒ Object



94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
# File 'lib/spurs/flash/helper.rb', line 94

def spurs_alert(message, options={ })

  ## We shouldn't really need this if code is written properly
  if message.is_a?(Array)
    raise "Message shouldn't be an array #{message}"
  end

  #merge arguments with defaults
  my_options = Spurs::Flash::default_args[:default].merge(options)

  # make sure the flavor is a symbol
  if my_options[:flavor] then
    my_options[:flavor] = my_options[:flavor].to_s.singularize.parameterize.to_sym
  end

  # attempt to find the flavor in the list of known stuff
  if Spurs::Flash::flavors.find_index(my_options[:flavor]) == nil
    # didn't find it.
    logger.warn("Unknown flash flavor \"#{my_options[:flavor]}\". Using a default instead")
    my_options[:flavor] = Spurs::Flash::default_args[:default][:flavor]
  end


  if !my_options[:title_icon]
    ico = Spurs::Flash::flavor_icons[my_options[:flavor]]
    if ico then
      my_options[:title_icon] = ico
    end
  end

  if !my_options[:title]
    t = Spurs::Flash::flavor_titles[my_options[:flavor]]
    if t then
      my_options[:title] = t
    end
  end

  if !my_options[:builder]
    raise "Null builder"
  end

  builder = my_options[:builder].new

  begin
    flash_content = builder.build_alert(message, my_options)
  rescue => e
    raise "Problem building flash message \nMessage: #{message.to_json}\nOptions: #{my_options.to_json}\nCause: #{e.message}\n\t#{e.backtrace}"
  end

  return flash_content
end

#spurs_alert_box(options = { }, &block) ⇒ Object



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
# File 'lib/spurs/flash/helper.rb', line 32

def spurs_alert_box(options={ }, &block)
  extra_class   = options[:type] ? "alert-#{options[:type]}" : ""
  alert_content = String.new
  if options[:title]
    titl = options[:title]
    if options[:title_icon]
      titl = "<i class='icon-#{options[:title_icon]}' style='margin-right: 6px'></i>".concat(titl)
    end
    alert_content.concat((:h4, titl.html_safe, :class => 'alert-heading'))
  end
  if block
    block_content = capture(nil, &block)
    alert_content.concat(block_content)
  end
  (:div, alert_content.html_safe, :class => "alert alert-block #{extra_class}")
end

#spurs_flash_helper(options = { }) ⇒ Object

Generate HTML for flash messages

Examples

spurs_flash_helper

Options

:priority - a numeric value that determines where new (usually dynamic) flash messages are placed. Defaults to 10


61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/spurs/flash/helper.rb', line 61

def spurs_flash_helper(options={ })

  my_options   = @@default_messages_options.merge(options)

  #Rails.logger.debug("FLASH >>#{flash.to_json}")

  message_hash = Hash.new()


  flash.each do |fl|
    process_flash_message(fl, message_hash)
  end
  flash.clear

  #logger.debug("MESSAGE HASH >>#{message_hash.to_json}")


  #process the message hash now
  content = String.new
  message_hash.each do |k, v_arr|
    v_arr.each do |v|
      if v == nil
        logger.warn("Flash key with no value: #{k.to_s} in flash hash #{v_arr.to_json}")
      else
        content << spurs_alert(v, :flavor => k, :builder => my_options[:builder], :title => k.to_s.singularize.tableize.to_sym.to_s.titlecase)
      end
    end
  end

  (:div, content.html_safe, :class => @@flashes_container_class, :id => "#{@@flashes_container_id_prefix}#{my_options[:priority]}")
end