flash_gordon 0.0.5 → 0.0.6

Sign up to get free protection for your applications and to get access to all the features.
data/lib/flash_gordon.rb CHANGED
@@ -1,20 +1,14 @@
1
1
  module FlashGordon
2
2
  mattr_accessor :zones
3
- mattr_reader :default_zones
4
3
  @@zones = {}
5
4
 
6
5
  def self.setup
7
6
  yield self
8
7
  end
9
8
 
10
- def self.set_default_zones
11
- @@default_zones = Hash[@@zones]
12
- end
13
-
14
9
  def self.init_zone
15
10
  zone_list = "warning danger info success error"
16
11
  zone_list.split(" ").each{|z| @@zones[z.to_sym] = []}
17
- self.set_default_zones
18
12
  true
19
13
  end
20
14
 
@@ -3,36 +3,72 @@ module FlashGordon
3
3
  # Messages fall under different message zones, for now zones are hardcoded
4
4
  # eventually we can add custom ones via initializers
5
5
 
6
- @messages = FlashGordon.zones
6
+ @main_messages = Hash[FlashGordon.zones]
7
7
 
8
8
  def self.append(message,zone=:warning, options={})
9
9
  # Added options for future use
10
- if @messages[zone]
10
+ if @main_messages[zone]
11
11
  if message.class == Array
12
12
  message.each do |m|
13
- @messages[zone] << m
13
+ @main_messages[zone] << m
14
14
  end
15
15
  else
16
- @messages[zone] << message
16
+ @main_messages[zone] << message
17
17
  end
18
18
  else
19
- raise "Zone does not exist, please use the following zones - #{@messages.map{|k| ":#{k[0]}"}.join(", ") } or add it to the initializer."
19
+ raise "Zone does not exist, please use the following zones - #{@main_messages.map{|k| ":#{k[0]}"}.join(", ") } or add it to the initializer."
20
20
  end
21
21
  end
22
22
 
23
- def self.render(zone=:all, glue="<br/>")
23
+ def self.render(zone, glue)
24
+ messages = get_messages(zone,glue)
25
+ end
26
+
27
+ def self.reset
28
+ @main_messages = FlashGordon.reset_zones
29
+ end
30
+
31
+ def self.get_messages(zone, glue)
32
+ messages = Hash[@main_messages]
24
33
  if zone == :all
25
- @messages.each_pair do |k,v|
26
- @messages[k] = v.join(glue) unless v.empty?
34
+ messages.each_pair do |k,v|
35
+ if v.empty?
36
+ # Remove empty zone keys
37
+ messages.delete(k)
38
+ else
39
+ messages[k] = v.join(glue)
40
+ end
41
+ end
42
+ messages
43
+ elsif messages[zone]
44
+ messages.each_pair do |k,v|
45
+ if k == zone
46
+ case glue
47
+ when "br", ""
48
+ messages[k] = messages[k].join(glue)
49
+ else
50
+ messages[k] = messages[k].each_with_index.map{|m,i| ElementDecorator.decorate_message(m,glue,i)}.join("")
51
+ end
52
+ else
53
+ messages.delete(k)
54
+ end
27
55
  end
28
- @messages
29
- elsif @messages[zone]
30
- @messages[zone].join(glue)
56
+ messages
31
57
  end
32
58
  end
59
+ end
33
60
 
34
- def self.reset
35
- @messages = FlashGordon.reset_zones
61
+ class ElementDecorator
62
+ def self.decorate_message(message, glue, index)
63
+ # Splits the elements into sticky elements and sticky classes
64
+ raw = glue.split(".")
65
+ # Unsticky the element if it has an id tag stuck to it
66
+ element = raw.map{|x| x["#"] ? x[0..x.index("#")-1] : x}[0]
67
+ # Next you isolate the sticky id and classes into its own array
68
+ ids = raw.map{|x| x[x.index("#")+1..-1].gsub("%",index.to_s) if x["#"]}.compact
69
+ classes = raw[1..-1].map{|x| x["#"] ? x[0..x.index("#")-1] : x}
70
+ # Lastly decorate the message with the intended tags with classes and ids
71
+ "<#{element}#{" class='"+classes.join(" ")+"'" unless classes.empty? }#{" id='"+ids.join(" ")+"'" unless ids.empty? }>#{message}</#{element}>"
36
72
  end
37
73
  end
38
74
  end
@@ -1,17 +1,24 @@
1
1
  module FlashGordon
2
2
  module FlashHelper
3
- def render_flash
4
- FlashGordon::Flash.render.each_pair do |k,v|
5
- unless v.empty?
6
- unless flash.now[k]
7
- flash.now[k] = v unless v.empty?
8
- else
9
- flash.now[k] << ("<br/>" + v) unless v.empty?
10
- end
11
- end
3
+ @first_flash = true
4
+ def render_flash(zone=:all, glue="</br>", tag="div")
5
+ # Assigns flash messages to flash object before rendering
6
+ to_flash = assign_flash(FlashGordon::Flash.render(zone,glue),zone)
7
+ to_flash.map{|f| "<#{tag} class='alert alert-#{f[0]}'>#{f[1]}</#{tag}>"}.join.html_safe
8
+ end
9
+
10
+ def assign_flash(flashes, zone)
11
+ to_flash = {}
12
+ flashes.each_pair do |k,v|
13
+ # Checks if there is an existing value which is not from FlashGordon
14
+ # if exists, it will append the new values with <br/> tags
15
+ to_flash[k] = flash.now[k] if flash.now[k]
16
+ if to_flash[k]
17
+ to_flash[k] << ("<br/>" + v)
18
+ else
19
+ to_flash[k] = v
20
+ end
12
21
  end
13
- FlashGordon::Flash.reset
14
- flash.map{|f| "<div class='alert alert-#{f[0]}'>#{f[1]}</div>"}.join.html_safe
15
22
  end
16
23
  end
17
24
  end
@@ -1,10 +1,12 @@
1
1
  require 'flash_gordon/flash'
2
2
  require 'flash_gordon/flash_helper'
3
+ require 'flash_gordon/reset_flashes'
3
4
 
4
5
  module FlashGordon
5
6
  class Railtie < Rails::Railtie
6
7
  initializer "flash_gordon.flash_helper" do |app|
7
8
  ActionView::Base.send :include, FlashHelper
9
+ ActionController::Base.send :include, ResetFlashes
8
10
  end
9
11
  end
10
12
  end
@@ -0,0 +1,11 @@
1
+ module ResetFlashes
2
+ extend ActiveSupport::Concern
3
+
4
+ included do
5
+ append_before_filter :reset_zones
6
+ end
7
+
8
+ def reset_zones
9
+ FlashGordon::Flash.reset
10
+ end
11
+ end
@@ -1,3 +1,3 @@
1
1
  module FlashGordon
2
- VERSION = "0.0.5"
2
+ VERSION = "0.0.6"
3
3
  end
metadata CHANGED
@@ -2,14 +2,14 @@
2
2
  name: flash_gordon
3
3
  version: !ruby/object:Gem::Version
4
4
  prerelease:
5
- version: 0.0.5
5
+ version: 0.0.6
6
6
  platform: ruby
7
7
  authors:
8
8
  - Ernest Sim
9
9
  autorequire:
10
10
  bindir: bin
11
11
  cert_chain: []
12
- date: 2013-08-22 00:00:00.000000000 Z
12
+ date: 2013-08-23 00:00:00.000000000 Z
13
13
  dependencies:
14
14
  - !ruby/object:Gem::Dependency
15
15
  name: rails
@@ -56,6 +56,7 @@ files:
56
56
  - lib/flash_gordon/flash.rb
57
57
  - lib/flash_gordon/flash_helper.rb
58
58
  - lib/flash_gordon/railtie.rb
59
+ - lib/flash_gordon/reset_flashes.rb
59
60
  - lib/flash_gordon/version.rb
60
61
  - lib/flash_gordon.rb
61
62
  - lib/tasks/flash_gordon_tasks.rake
@@ -106,7 +107,8 @@ files:
106
107
  - test/flash_gordon_test.rb
107
108
  - test/test_helper.rb
108
109
  homepage: https://github.com/methodnow/flash_gordon
109
- licenses: []
110
+ licenses:
111
+ - MIT
110
112
  post_install_message:
111
113
  rdoc_options: []
112
114
  require_paths: