Permalink
Browse files

- Flash on the Rails side is cleared when written to the cookie

- Uses existing cookie flash value
- Using Scriptaculous cookie.js library

git-svn-id: svn+ssh://rubyforge.org/var/svn/pivotalrb/cacheable_flash/trunk@591 af276e61-6b34-4dac-905b-574b5f35ef33
  • Loading branch information...
1 parent 3ba0a73 commit d2cb0aed2be000ede6f49738f6ce255080645b8e btakita committed Jul 30, 2007
View
@@ -1,2 +1,6 @@
+- Flash on the Rails side is cleared when written to the cookie
+- Uses existing cookie flash value
+- Using Scriptaculous cookie.js library
+
0.1.1
- Added cookies.js
View
@@ -13,9 +13,9 @@
cp "#{dir}/json.js", rails_javascripts_dir
end
-if File.exists?("#{rails_javascripts_dir}/cookies.js")
- puts "#{rails_javascripts_dir}/cookies.js already exists"
+if File.exists?("#{rails_javascripts_dir}/cookie.js")
+ puts "#{rails_javascripts_dir}/cookie.js already exists"
else
- puts "copying cookies.js to #{rails_javascripts_dir}"
- cp "#{dir}/cookies.js", rails_javascripts_dir
+ puts "copying cookie.js to #{rails_javascripts_dir}"
+ cp "#{dir}/cookie.js", rails_javascripts_dir
end
View
@@ -0,0 +1,49 @@
+// From http://wiki.script.aculo.us/scriptaculous/show/Cookie
+var Cookie = {
+ set: function(name, value, daysToExpire) {
+ var expire = '';
+ if(!daysToExpire) daysToExpire = 365;
+ var d = new Date();
+ d.setTime(d.getTime() + (86400000 * parseFloat(daysToExpire)));
+ expire = 'expires=' + d.toGMTString();
+ var path = "path=/"
+ var cookieValue = escape(name) + '=' + escape(value || '') + '; ' + path + '; ' + expire + ';';
+ return document.cookie = cookieValue;
+ },
+ get: function(name) {
+ var cookie = document.cookie.match(new RegExp('(^|;)\\s*' + escape(name) + '=([^;\\s]+)'));
+ return (cookie ? unescape(cookie[2]) : null);
+ },
+ erase: function(name) {
+ var cookie = Cookie.get(name) || true;
+ Cookie.set(name, '', -1);
+ return cookie;
+ },
+ eraseAll: function() {
+ // Get cookie string and separate into individual cookie phrases:
+ var cookieString = "" + document.cookie;
+ var cookieArray = cookieString.split("; ");
+
+ // Try to delete each cookie:
+ for(var i = 0; i < cookieArray.length; ++ i)
+ {
+ var singleCookie = cookieArray[i].split("=");
+ if(singleCookie.length != 2)
+ continue;
+ var name = unescape(singleCookie[0]);
+ Cookie.erase(name);
+ }
+ },
+ accept: function() {
+ if (typeof navigator.cookieEnabled == 'boolean') {
+ return navigator.cookieEnabled;
+ }
+ Cookie.set('_test', '1');
+ return (Cookie.erase('_test') === '1');
+ },
+ exists: function(cookieName) {
+ var cookieValue = Cookie.get(cookieName);
+ if(!cookieValue) return false;
+ return cookieValue.toString() != "";
+ }
+};
View
@@ -1,46 +0,0 @@
-var Cookies = new Object();
-
-Cookies.read = function(name) {
- var nameEQ = name + "=";
- var cookies = document.cookie.split(';');
- var matches = [];
- for(var i=0;i < cookies.length;i++) {
- var cookie = cookies[i];
- while (cookie.charAt(0)==' ') {
- cookie = cookie.substring(1, cookie.length);
- }
- if (cookie.indexOf(nameEQ) == 0) {
- matches.push(cookie.substring(nameEQ.length, cookie.length));
- }
- }
- return matches.last() || null;
-}
-
-Cookies.write = function(name, value) {
- var expires = new Date();
- expires.setYear(expires.getFullYear() + 1);
- var expiresText = " expires=" + expires.toGMTString();
- document.cookie = escape(name) + "=" + escape(value) + "; path=/;" + expiresText;
-}
-
-Cookies.expire = function(name) {
- var expirationDate = new Date();
- expirationDate.setTime(expirationDate.getTime() - 1);
- document.cookie = escape(name) + "=null; path=/; expires=" + expirationDate.toGMTString() + ";";
-}
-
-Cookies.expireAll = function() {
- // Get cookie string and separate into individual cookie phrases:
- var cookieString = "" + document.cookie;
- var cookieArray = cookieString.split("; ");
-
- // Try to delete each cookie:
- for(var i = 0; i < cookieArray.length; ++ i)
- {
- var singleCookie = cookieArray[i].split("=");
- if(singleCookie.length != 2)
- continue;
- var name = unescape(singleCookie[0]);
- Cookies.expire(name);
- }
-}
View
@@ -3,10 +3,10 @@ var Flash = new Object();
Flash.data = {};
Flash.transferFromCookies = function() {
- var data = JSON.parse(unescape(Cookies.read("flash")));
+ var data = JSON.parse(unescape(Cookie.get("flash")));
if(!data) data = {};
Flash.data = data;
- Cookies.expire("flash");
+ Cookie.erase("flash");
};
Flash.writeDataTo = function(name, element) {
View
@@ -4,6 +4,17 @@ def self.included(base)
end
def write_flash_to_cookie
- cookies['flash'] = flash.to_json
+ cookie_flash = cookies['flash'] ? JSON.parse(cookies['flash']) : {}
+
+ flash.each do |key, value|
+ if cookie_flash[key.to_s].blank?
+ cookie_flash[key.to_s] = value
+ else
+ cookie_flash[key.to_s] << "<br/>#{value}"
+ end
+ end
+
+ cookies['flash'] = cookie_flash.to_json
+ flash.clear
end
end
@@ -4,27 +4,60 @@
class CacheableFlashTest < Test::Unit::TestCase
def setup
@controller_class = Struct.new(:cookies, :flash)
- @controller_class.stubs(:after_filter)
+ stub(@controller_class).after_filter
@controller_class.send(:include, CacheableFlash)
@controller = @controller_class.new({}, {})
+ @cookies = {}
+ stub(@controller).cookies {@cookies}
end
def test_write_flash_to_cookie
expected_flash = {
'errors' => "This is an Error",
'notice' => "This is a Notice"
}
- @controller.flash = expected_flash
+ @controller.flash = expected_flash.dup
@controller.write_flash_to_cookie
assert_equal expected_flash, JSON.parse(@controller.cookies['flash'])
end
+
+ def test_flash_is_appended_to_existing_flash_cookie
+ @cookies['flash'] = {
+ 'notice' => "Existing notice",
+ 'errors' => "Existing errors",
+ }.to_json
+
+ @controller.flash = {
+ 'notice' => 'New notice',
+ 'errors' => 'New errors',
+ }
+
+ @controller.write_flash_to_cookie
+
+ expected_flash = {
+ 'notice' => "Existing notice<br/>New notice",
+ 'errors' => "Existing errors<br/>New errors",
+ }
+ assert_equal expected_flash, JSON.parse(@controller.cookies['flash'])
+ end
+
+ def test_flash_is_cleared
+ flash = {
+ 'errors' => "This is an Error",
+ 'notice' => "This is a Notice"
+ }
+ @controller.flash = flash
+ @controller.write_flash_to_cookie
+
+ assert_equal({}, @controller.flash)
+ end
end
class CacheableFlashAfterFilterTest < Test::Unit::TestCase
def test_after_filter_is_set
@controller_class = Struct.new(:cookies, :flash)
- @controller_class.expects(:after_filter).with(:write_flash_to_cookie)
+ mock(@controller_class).after_filter(:write_flash_to_cookie)
@controller_class.send(:include, CacheableFlash)
end
end
@@ -24,11 +24,11 @@ def teardown
def test_install_without_json_js_file
assert !File.exists?("#{@js_dir}/flash.js")
assert !File.exists?("#{@js_dir}/json.js")
- assert !File.exists?("#{@js_dir}/cookies.js")
+ assert !File.exists?("#{@js_dir}/cookie.js")
load(@install_path)
assert File.exists?("#{@js_dir}/flash.js")
assert File.exists?("#{@js_dir}/json.js")
- assert File.exists?("#{@js_dir}/cookies.js")
+ assert File.exists?("#{@js_dir}/cookie.js")
end
def test_install_with_json_js_file
@@ -37,25 +37,25 @@ def test_install_with_json_js_file
end
assert File.exists?("#{@js_dir}/json.js")
assert !File.exists?("#{@js_dir}/flash.js")
- assert !File.exists?("#{@js_dir}/cookies.js")
+ assert !File.exists?("#{@js_dir}/cookie.js")
load(@install_path)
assert File.exists?("#{@js_dir}/flash.js")
assert File.exists?("#{@js_dir}/json.js")
- assert File.exists?("#{@js_dir}/cookies.js")
+ assert File.exists?("#{@js_dir}/cookie.js")
assert_equal "Original json.js", File.read("#{@js_dir}/json.js")
end
def test_install_with_cookies_js_file
- File.open("#{@js_dir}/cookies.js", "w") do |f|
- f.write "Original cookies.js"
+ File.open("#{@js_dir}/cookie.js", "w") do |f|
+ f.write "Original cookie.js"
end
assert !File.exists?("#{@js_dir}/json.js")
assert !File.exists?("#{@js_dir}/flash.js")
- assert File.exists?("#{@js_dir}/cookies.js")
+ assert File.exists?("#{@js_dir}/cookie.js")
load(@install_path)
assert File.exists?("#{@js_dir}/flash.js")
assert File.exists?("#{@js_dir}/json.js")
- assert File.exists?("#{@js_dir}/cookies.js")
- assert_equal "Original cookies.js", File.read("#{@js_dir}/cookies.js")
+ assert File.exists?("#{@js_dir}/cookie.js")
+ assert_equal "Original cookie.js", File.read("#{@js_dir}/cookie.js")
end
end
@@ -3,46 +3,46 @@
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<script src="../../javascripts/prototype.js" type="text/javascript"></script>
- <script src="../../javascripts/cookies.js" type="text/javascript"></script>
+ <script src="../../javascripts/cookie.js" type="text/javascript"></script>
<script language="javascript" type="text/javascript">
var content;
function setUp() {
content = $('content');
- Cookies.expireAll();
+ Cookie.expireAll();
}
function tearDown() {
- Cookies.expireAll();
+ Cookie.expireAll();
}
function test_read() {
var expires = new Date();
expires.setYear(expires.getFullYear() + 1);
document.cookie = "foobar=baz; path=/; expires=" + expires.toGMTString();
- assertEquals('baz', Cookies.read('foobar'));
+ assertEquals('baz', Cookie.read('foobar'));
}
function test_write() {
- Cookies.write('foobar', 'baz');
- assertEquals("baz", Cookies.read('foobar'));
+ Cookie.write('foobar', 'baz');
+ assertEquals("baz", Cookie.read('foobar'));
}
function test_expire() {
- Cookies.write('foobar', 'baz');
- Cookies.expire('foobar');
- assertNull(Cookies.read('foobar'));
+ Cookie.write('foobar', 'baz');
+ Cookie.expire('foobar');
+ assertNull(Cookie.read('foobar'));
}
function test_expireAll() {
- Cookies.write('foobar', 'baz');
- Cookies.write('another', 'cookie');
+ Cookie.write('foobar', 'baz');
+ Cookie.write('another', 'cookie');
- Cookies.expireAll();
+ Cookie.expireAll();
- assertNull(Cookies.read('foobar'));
- assertNull(Cookies.read('another'));
+ assertNull(Cookie.read('foobar'));
+ assertNull(Cookie.read('another'));
}
</script>
@@ -2,20 +2,27 @@
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
- <script src="../../javascripts/prototype.js" type="text/javascript"></script>
- <script src="../../javascripts/cookies.js" type="text/javascript"></script>
- <script src="../../javascripts/flash.js" type="text/javascript"></script>
-
+ <script src="../js-common/include.js" type="text/javascript"></script>
+ <script src="../cpr-include.js" type="text/javascript"></script>
+ <script type="text/javascript">
+ Include.jsUnitCore();
+ Include.allProduction();
+ Include.allCommonTest();
+ Include.testHelper();
+
+
+ </script>
+
<script language="javascript" type="text/javascript">
var content;
function setUp() {
content = $('content');
content.innerHTML = "";
- Cookies.expireAll();
+ Cookie.eraseAll();
}
function tearDown() {
- Cookies.expireAll();
+ Cookie.eraseAll();
}
function test_transferFromCookies_setsFlashData_whenCookieExists() {
@@ -45,7 +52,7 @@
'notice': "The Notice"
});
Flash.transferFromCookies();
- assertNull(Cookies.read("flash"));
+ assertNull(Cookie.get("flash"));
}
function test_writeDataTo_whenThereIsACookieValue() {
@@ -78,7 +85,7 @@
}
function setUpCookieWithData(values) {
- Cookies.write('flash', JSON.stringify(values));
+ Cookie.set('flash', JSON.stringify(values));
}
</script>
View
@@ -1,10 +1,15 @@
require "rubygems"
require "test/unit"
require "json"
-require "mocha"
+require "rr"
require "tmpdir"
require "fileutils"
+require "active_support"
dir = File.dirname(__FILE__)
$LOAD_PATH << "#{dir}/../lib"
require "cacheable_flash"
+
+class Test::Unit::TestCase
+ include RR::Adapters::TestUnit
+end

0 comments on commit d2cb0ae

Please sign in to comment.