cacheable_flash 0.3.1 → 0.3.2

Sign up to get free protection for your applications and to get access to all the features.
data/CHANGELOG CHANGED
@@ -1,3 +1,6 @@
1
+ 0.3.2 - AUG.23.2012
2
+ * Properly handles stacked flash in cookie flash according to config :append_as option [Peter Boling]
3
+
1
4
  0.3.1 - AUG.22.2012
2
5
  * Add specs for non stacking use [Peter Boling]
3
6
  * Use stackable_flash/test_helpers instead of reinventing the wheel [Peter Boling]
@@ -7,7 +7,6 @@ Gem::Specification.new do |s|
7
7
 
8
8
  s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
9
9
  s.authors = ["Peter H. Boling", "Brian Takita"]
10
- s.date = "2012-08-13"
11
10
  s.description = "Allows caching of pages with flash messages by rendering flash\nmessages from a cookie using JavaScript, instead of statically in your Rails\nview template. Flash contents are converted to JSON and placed in\na cookie by an after_filter (default) or a Rack Middleware (option)."
12
11
  s.email = "peter.boling@gmail.com"
13
12
  s.extra_rdoc_files = [
@@ -13,7 +13,7 @@ module CacheableFlash
13
13
  # By default stacking is false, but it can be turned on with:
14
14
  # CacheableFlash.configure do |config|
15
15
  # config[:stacking] = true
16
- # config[:append_as] = :br
16
+ # config[:append_as] = :br # Pick a value, set your own proc, or don't: passes the JSON'd array to the cookie
17
17
  # end
18
18
  StackableFlash.stacking = false
19
19
 
@@ -37,4 +37,9 @@ module CacheableFlash
37
37
  # because flashes are only removed from cookies when they are used.
38
38
  flash.clear
39
39
  end
40
+
41
+ # simply abstracts the StackableFlash.stacking method
42
+ def self.stacking
43
+ StackableFlash.stacking
44
+ end
40
45
  end
@@ -21,17 +21,19 @@ module CacheableFlash
21
21
  yield @config
22
22
  if @config[:stacking]
23
23
  StackableFlash.stacking = true
24
- StackableFlash::Config.configure do
25
- # by default behave like regular non-stacking flash
24
+ StackableFlash::Config.configure do |config|
25
+ # by default behave like regular non-stacking flash
26
26
  if @config[:append_as].respond_to?(:call)
27
- @config[:stack_with_proc] = @config[:append_as]
27
+ config[:stack_with_proc] = @config[:append_as]
28
28
  else
29
- @config[:stack_with_proc] = case @config[:append_as]
30
- when :br then Proc.new {|arr| arr.join('<br/>') }
31
- when :array then Proc.new {|arr| arr }
32
- when :p then Proc.new {|arr| arr.map! {|x| "<p>#{x}</p>"}.join }
33
- when :ul then Proc.new {|arr| '<ul>' + arr.map! {|x| "<li>#{x}</li>"}.join + '</ul>' }
34
- when :ol then Proc.new {|arr| '<ol>' + arr.map! {|x| "<li>#{x}</li>"}.join + '</ol>' }
29
+ config[:stack_with_proc] = case @config[:append_as]
30
+ # Escape as many strings as we can!
31
+ when :br then Proc.new {|arr| arr.map { |x| x.kind_of?(String) && !x.html_safe? ? ERB::Util.html_escape(x) : x }.join('<br/>') }
32
+ when :array then Proc.new {|arr| arr.map { |x| x.kind_of?(String) && !x.html_safe? ? ERB::Util.html_escape(x) : x } }
33
+ when :p then Proc.new {|arr| arr.map! {|x| "<p>#{x.kind_of?(String) && !x.html_safe? ? ERB::Util.html_escape(x) : x }</p>"}.join }
34
+ when :ul then Proc.new {|arr| '<ul>' + arr.map! {|x| "<li>#{x.kind_of?(String) && !x.html_safe? ? ERB::Util.html_escape(x) : x }</li>"}.join + '</ul>' }
35
+ when :ol then Proc.new {|arr| '<ol>' + arr.map! {|x| "<li>#{x.kind_of?(String) && !x.html_safe? ? ERB::Util.html_escape(x) : x }</li>"}.join + '</ol>' }
36
+ else Proc.new {|arr| arr.map { |x| x.kind_of?(String) && !x.html_safe? ? ERB::Util.html_escape(x) : x }.join('<br/>') }
35
37
  end
36
38
  end
37
39
  end
@@ -1,23 +1,33 @@
1
1
  module CacheableFlash
2
2
  module CookieFlash
3
+
4
+ # @parameters
5
+ # cookies -
6
+ # There might be crusty flash from a previous request, or set elsewhere, already in the cookie.
7
+ # Pull it out and parse it so we can preserve it.
8
+ # flash -
9
+ # This is the fresh, super-stacked (by stackable_flash gem) FlashHash from the current request.
10
+ # Needs to be added to the cookie flash.
3
11
  def cookie_flash(flash, cookies)
4
12
  cflash = (JSON.parse(cookies['flash']) if cookies['flash']) || {} rescue {}
5
- flash.each do |key, value|
6
- # Since v0.3.0 only escaping strings
7
- if value.kind_of?(String)
8
- value = ERB::Util.html_escape(value) unless value.html_safe?
9
- end
13
+
14
+ flash.each do |key, value| # key like :notice, or :error, or :sticky
15
+ # When stacking we won't be escaping anything here, because will be array, not string
16
+ value = ERB::Util.html_escape(value) if value.kind_of?(String) && !value.html_safe? # Since v0.3.0 only escaping strings
10
17
  skey = key.to_s
11
18
  # This allows any data type to be stored in the cookie; important for using an array as the value with
12
19
  # stackable_flash
13
- if cflash[skey].kind_of?(Array) # Already Stackable!!!
14
- if value.kind_of?(Array)
15
- cflash[skey] += value # Add the values from the other array, which is already a stackable flash
20
+ # The cookie flash will generally be set to a value stacked according to the :stack_with_proc of stackable_flash
21
+ # But when there is already a value for the cookie when we get here, we need to join them somehow.
22
+ stacked_value = value.respond_to?(:stack) ? value.stack : value
23
+ if cflash[skey].kind_of?(Array) # Just because it could be an array
24
+ if stacked_value.kind_of?(Array)
25
+ cflash[skey] += stacked_value
16
26
  else
17
- cflash[skey] << value
27
+ cflash[skey] << stacked_value
18
28
  end
19
29
  else
20
- cflash[skey] = value
30
+ cflash[skey] = stacked_value
21
31
  end
22
32
  end
23
33
  # I have forgotten why the gsub + matters, so NOTE: to future self: document weird shit.
@@ -15,11 +15,13 @@ module CacheableFlash
15
15
  env_flash = env[FLASH_HASH_KEY]
16
16
 
17
17
  if env_flash
18
+ # There is a flash set from this request, merge it into the cookie flash (which may already be populated)
18
19
  response = Rack::Response.new(body, status, headers)
19
20
  cookies = env["rack.cookies"] || {}
20
21
  response.set_cookie("flash", { :value => cookie_flash(env_flash, cookies), :path => "/" })
21
22
  response.finish
22
23
  else
24
+ # There is no flash set in this request but there are leftovers from previous requests still in the cookie
23
25
  request = ActionDispatch::Request.new(env)
24
26
  cflash = request.respond_to?(:cookie_jar) ?
25
27
  request.cookie_jar['flash'] :
@@ -1,3 +1,3 @@
1
1
  module CacheableFlash
2
- VERSION = "0.3.1"
2
+ VERSION = "0.3.2"
3
3
  end
@@ -39,6 +39,7 @@ describe ComparisonController do
39
39
  before(:each) do
40
40
  CacheableFlash::Config.configure do |config|
41
41
  config[:stacking] = true
42
+ config[:append_as] = :array
42
43
  end
43
44
  end
44
45
  after(:each) do
@@ -164,6 +165,7 @@ describe ComparisonController do
164
165
  before(:each) do
165
166
  CacheableFlash::Config.configure do |config|
166
167
  config[:stacking] = true
168
+ config[:append_as] = :array
167
169
  end
168
170
  end
169
171
  after(:each) do
@@ -40,6 +40,7 @@ describe DummyController do
40
40
  before(:each) do
41
41
  CacheableFlash::Config.configure do |config|
42
42
  config[:stacking] = true
43
+ config[:append_as] = :array
43
44
  end
44
45
  end
45
46
  after(:each) do
@@ -4,6 +4,7 @@ describe DummyController do
4
4
  before(:each) do
5
5
  CacheableFlash::Config.configure do |config|
6
6
  config[:stacking] = true
7
+ config[:append_as] = :array
7
8
  end
8
9
  end
9
10
  after(:each) do
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: cacheable_flash
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.3.1
4
+ version: 0.3.2
5
5
  prerelease:
6
6
  platform: ruby
7
7
  authors:
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2012-08-13 00:00:00.000000000 Z
13
+ date: 2012-08-23 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: stackable_flash