cacheable_flash 0.2.2 → 0.2.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- data/.document +5 -5
- data/CHANGES +42 -36
- data/Gemfile +20 -17
- data/Gemfile.lock +121 -82
- data/LICENSE.txt +21 -21
- data/README.rdoc +158 -159
- data/Rakefile +54 -54
- data/VERSION +1 -1
- data/cacheable_flash.gemspec +15 -18
- data/init.rb +6 -6
- data/install.rb +18 -18
- data/lib/cacheable_flash.rb +39 -38
- data/lib/cacheable_flash/rails/engine.rb +8 -13
- data/lib/cacheable_flash/rails/railtie.rb +14 -14
- data/lib/generators/cacheable_flash/install/install_generator.rb +23 -23
- data/spec/cacheable_flash/cacheable_flash_spec.rb +105 -101
- data/spec/cacheable_flash/install_spec.rb +68 -68
- data/spec/cacheable_flash/test_helpers_spec.rb +37 -36
- data/spec/js_unit/cookie_test.html +60 -60
- data/spec/js_unit/flash_test.html +112 -112
- data/spec/spec_helper.rb +20 -20
- data/spec/support/test_helpers.rb +10 -10
- data/tasks/cacheable_flash_tasks.rake +3 -3
- data/vendor/assets/javascripts/flash.js +21 -21
- data/vendor/assets/javascripts/jquery.cookie.js +91 -91
- metadata +26 -37
data/Rakefile
CHANGED
@@ -1,54 +1,54 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
require 'rubygems'
|
4
|
-
require 'bundler'
|
5
|
-
begin
|
6
|
-
Bundler.setup(:default, :development)
|
7
|
-
rescue Bundler::BundlerError => e
|
8
|
-
$stderr.puts e.message
|
9
|
-
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
-
exit e.status_code
|
11
|
-
end
|
12
|
-
require 'rake'
|
13
|
-
|
14
|
-
require 'jeweler'
|
15
|
-
Jeweler::Tasks.new do |gem|
|
16
|
-
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
-
gem.name = "cacheable_flash"
|
18
|
-
gem.homepage = "http://github.com/
|
19
|
-
gem.license = "MIT"
|
20
|
-
gem.summary = %Q{Render flash messages from a cookie using JavaScript, instead of in your Rails
|
21
|
-
view template}
|
22
|
-
gem.description = %Q{This plugin enables greater levels of page caching by rendering flash
|
23
|
-
messages from a cookie using JavaScript, instead of in your Rails
|
24
|
-
view template. Flash contents are converted to JSON and placed in
|
25
|
-
a cookie by an after_filter in a controller.}
|
26
|
-
gem.email = "peter.boling@gmail.com"
|
27
|
-
gem.authors = ["Peter H. Boling","Brian Takita"]
|
28
|
-
# dependencies defined in Gemfile
|
29
|
-
end
|
30
|
-
Jeweler::RubygemsDotOrgTasks.new
|
31
|
-
|
32
|
-
require 'rspec/core'
|
33
|
-
require 'rspec/core/rake_task'
|
34
|
-
RSpec::Core::RakeTask.new(:spec) do |spec|
|
35
|
-
spec.pattern = FileList['spec/**/*_spec.rb']
|
36
|
-
end
|
37
|
-
|
38
|
-
require 'reek/rake/task'
|
39
|
-
Reek::Rake::Task.new do |t|
|
40
|
-
t.fail_on_error = true
|
41
|
-
t.verbose = false
|
42
|
-
t.source_files = 'lib/**/*.rb'
|
43
|
-
end
|
44
|
-
|
45
|
-
require 'roodi'
|
46
|
-
require 'roodi_task'
|
47
|
-
RoodiTask.new do |t|
|
48
|
-
t.verbose = false
|
49
|
-
end
|
50
|
-
|
51
|
-
task :default => :spec
|
52
|
-
|
53
|
-
require 'yard'
|
54
|
-
YARD::Rake::YardocTask.new
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
require 'rubygems'
|
4
|
+
require 'bundler'
|
5
|
+
begin
|
6
|
+
Bundler.setup(:default, :development)
|
7
|
+
rescue Bundler::BundlerError => e
|
8
|
+
$stderr.puts e.message
|
9
|
+
$stderr.puts "Run `bundle install` to install missing gems"
|
10
|
+
exit e.status_code
|
11
|
+
end
|
12
|
+
require 'rake'
|
13
|
+
|
14
|
+
require 'jeweler'
|
15
|
+
Jeweler::Tasks.new do |gem|
|
16
|
+
# gem is a Gem::Specification... see http://docs.rubygems.org/read/chapter/20 for more options
|
17
|
+
gem.name = "cacheable_flash"
|
18
|
+
gem.homepage = "http://github.com/pivotal/cacheable-flash"
|
19
|
+
gem.license = "MIT"
|
20
|
+
gem.summary = %Q{Render flash messages from a cookie using JavaScript, instead of in your Rails
|
21
|
+
view template}
|
22
|
+
gem.description = %Q{This plugin enables greater levels of page caching by rendering flash
|
23
|
+
messages from a cookie using JavaScript, instead of in your Rails
|
24
|
+
view template. Flash contents are converted to JSON and placed in
|
25
|
+
a cookie by an after_filter in a controller.}
|
26
|
+
gem.email = "peter.boling@gmail.com"
|
27
|
+
gem.authors = ["Peter H. Boling","Brian Takita"]
|
28
|
+
# dependencies defined in Gemfile
|
29
|
+
end
|
30
|
+
Jeweler::RubygemsDotOrgTasks.new
|
31
|
+
|
32
|
+
require 'rspec/core'
|
33
|
+
require 'rspec/core/rake_task'
|
34
|
+
RSpec::Core::RakeTask.new(:spec) do |spec|
|
35
|
+
spec.pattern = FileList['spec/**/*_spec.rb']
|
36
|
+
end
|
37
|
+
|
38
|
+
require 'reek/rake/task'
|
39
|
+
Reek::Rake::Task.new do |t|
|
40
|
+
t.fail_on_error = true
|
41
|
+
t.verbose = false
|
42
|
+
t.source_files = 'lib/**/*.rb'
|
43
|
+
end
|
44
|
+
|
45
|
+
require 'roodi'
|
46
|
+
require 'roodi_task'
|
47
|
+
RoodiTask.new do |t|
|
48
|
+
t.verbose = false
|
49
|
+
end
|
50
|
+
|
51
|
+
task :default => :spec
|
52
|
+
|
53
|
+
require 'yard'
|
54
|
+
YARD::Rake::YardocTask.new
|
data/VERSION
CHANGED
@@ -1 +1 @@
|
|
1
|
-
0.2.
|
1
|
+
0.2.3
|
data/cacheable_flash.gemspec
CHANGED
@@ -5,11 +5,11 @@
|
|
5
5
|
|
6
6
|
Gem::Specification.new do |s|
|
7
7
|
s.name = "cacheable_flash"
|
8
|
-
s.version = "0.2.
|
8
|
+
s.version = "0.2.3"
|
9
9
|
|
10
10
|
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
11
11
|
s.authors = ["Peter H. Boling", "Brian Takita"]
|
12
|
-
s.date = "
|
12
|
+
s.date = "2012-01-14"
|
13
13
|
s.description = "This plugin enables greater levels of page caching by rendering flash\nmessages from a cookie using JavaScript, instead of in your Rails\nview template. Flash contents are converted to JSON and placed in\na cookie by an after_filter in a controller."
|
14
14
|
s.email = "peter.boling@gmail.com"
|
15
15
|
s.extra_rdoc_files = [
|
@@ -43,7 +43,7 @@ Gem::Specification.new do |s|
|
|
43
43
|
"vendor/assets/javascripts/flash.js",
|
44
44
|
"vendor/assets/javascripts/jquery.cookie.js"
|
45
45
|
]
|
46
|
-
s.homepage = "http://github.com/
|
46
|
+
s.homepage = "http://github.com/pivotal/cacheable-flash"
|
47
47
|
s.licenses = ["MIT"]
|
48
48
|
s.require_paths = ["lib"]
|
49
49
|
s.rubygems_version = "1.8.10"
|
@@ -53,33 +53,30 @@ Gem::Specification.new do |s|
|
|
53
53
|
s.specification_version = 3
|
54
54
|
|
55
55
|
if Gem::Version.new(Gem::VERSION) >= Gem::Version.new('1.2.0') then
|
56
|
-
s.add_runtime_dependency(%q<
|
57
|
-
s.add_runtime_dependency(%q<actionpack>, [">= 0"])
|
56
|
+
s.add_runtime_dependency(%q<rails>, ["> 3.0"])
|
58
57
|
s.add_runtime_dependency(%q<json>, [">= 0"])
|
59
|
-
s.add_development_dependency(%q<rspec>, ["~> 2.
|
60
|
-
s.add_development_dependency(%q<yard>, ["~> 0.
|
61
|
-
s.add_development_dependency(%q<bundler>, ["~> 1.0.
|
58
|
+
s.add_development_dependency(%q<rspec>, ["~> 2.8.0"])
|
59
|
+
s.add_development_dependency(%q<yard>, ["~> 0.7.4"])
|
60
|
+
s.add_development_dependency(%q<bundler>, ["~> 1.0.21"])
|
62
61
|
s.add_development_dependency(%q<jeweler>, ["~> 1.6.4"])
|
63
62
|
s.add_development_dependency(%q<reek>, ["~> 1.2.8"])
|
64
63
|
s.add_development_dependency(%q<roodi>, ["~> 2.1.0"])
|
65
64
|
else
|
66
|
-
s.add_dependency(%q<
|
67
|
-
s.add_dependency(%q<actionpack>, [">= 0"])
|
65
|
+
s.add_dependency(%q<rails>, ["> 3.0"])
|
68
66
|
s.add_dependency(%q<json>, [">= 0"])
|
69
|
-
s.add_dependency(%q<rspec>, ["~> 2.
|
70
|
-
s.add_dependency(%q<yard>, ["~> 0.
|
71
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.
|
67
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
68
|
+
s.add_dependency(%q<yard>, ["~> 0.7.4"])
|
69
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.21"])
|
72
70
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
73
71
|
s.add_dependency(%q<reek>, ["~> 1.2.8"])
|
74
72
|
s.add_dependency(%q<roodi>, ["~> 2.1.0"])
|
75
73
|
end
|
76
74
|
else
|
77
|
-
s.add_dependency(%q<
|
78
|
-
s.add_dependency(%q<actionpack>, [">= 0"])
|
75
|
+
s.add_dependency(%q<rails>, ["> 3.0"])
|
79
76
|
s.add_dependency(%q<json>, [">= 0"])
|
80
|
-
s.add_dependency(%q<rspec>, ["~> 2.
|
81
|
-
s.add_dependency(%q<yard>, ["~> 0.
|
82
|
-
s.add_dependency(%q<bundler>, ["~> 1.0.
|
77
|
+
s.add_dependency(%q<rspec>, ["~> 2.8.0"])
|
78
|
+
s.add_dependency(%q<yard>, ["~> 0.7.4"])
|
79
|
+
s.add_dependency(%q<bundler>, ["~> 1.0.21"])
|
83
80
|
s.add_dependency(%q<jeweler>, ["~> 1.6.4"])
|
84
81
|
s.add_dependency(%q<reek>, ["~> 1.2.8"])
|
85
82
|
s.add_dependency(%q<roodi>, ["~> 2.1.0"])
|
data/init.rb
CHANGED
@@ -1,6 +1,6 @@
|
|
1
|
-
require "json"
|
2
|
-
require "cacheable_flash"
|
3
|
-
#use Rails::VERSION::STRING for rails < 1 somehow -- or perhaps, who cares??
|
4
|
-
if ::Rails::VERSION::MAJOR == 2
|
5
|
-
ActionView::Helpers::AssetTagHelper.register_javascript_include_default('jquery.cookie','flash')
|
6
|
-
end
|
1
|
+
require "json"
|
2
|
+
require "cacheable_flash"
|
3
|
+
#use Rails::VERSION::STRING for rails < 1 somehow -- or perhaps, who cares??
|
4
|
+
if ::Rails::VERSION::MAJOR == 2
|
5
|
+
ActionView::Helpers::AssetTagHelper.register_javascript_include_default('jquery.cookie','flash')
|
6
|
+
end
|
data/install.rb
CHANGED
@@ -1,18 +1,18 @@
|
|
1
|
-
# Only executed when installed as plugin.
|
2
|
-
# When installed as a gem must use the generator, unless using Rails >= 3.1,
|
3
|
-
# in which case the asset pipeline takes care of everything
|
4
|
-
require 'fileutils'
|
5
|
-
include FileUtils
|
6
|
-
|
7
|
-
dir = "#{File.dirname(__FILE__)}/vendor/assets/javascripts"
|
8
|
-
rails_javascripts_dir = "#{Rails.root}/public/javascripts"
|
9
|
-
|
10
|
-
puts "copying flash.js to #{rails_javascripts_dir}"
|
11
|
-
cp "#{dir}/flash.js", rails_javascripts_dir
|
12
|
-
|
13
|
-
if File.exists?("#{rails_javascripts_dir}/jquery.cookie.js")
|
14
|
-
puts "#{rails_javascripts_dir}/jquery.cookie.js already exists"
|
15
|
-
else
|
16
|
-
puts "copying jquery.cookie.js to #{rails_javascripts_dir}"
|
17
|
-
cp "#{dir}/jquery.cookie.js", rails_javascripts_dir
|
18
|
-
end
|
1
|
+
# Only executed when installed as plugin.
|
2
|
+
# When installed as a gem must use the generator, unless using Rails >= 3.1,
|
3
|
+
# in which case the asset pipeline takes care of everything
|
4
|
+
require 'fileutils'
|
5
|
+
include FileUtils
|
6
|
+
|
7
|
+
dir = "#{File.dirname(__FILE__)}/vendor/assets/javascripts"
|
8
|
+
rails_javascripts_dir = "#{Rails.root}/public/javascripts"
|
9
|
+
|
10
|
+
puts "copying flash.js to #{rails_javascripts_dir}"
|
11
|
+
cp "#{dir}/flash.js", rails_javascripts_dir
|
12
|
+
|
13
|
+
if File.exists?("#{rails_javascripts_dir}/jquery.cookie.js")
|
14
|
+
puts "#{rails_javascripts_dir}/jquery.cookie.js already exists"
|
15
|
+
else
|
16
|
+
puts "copying jquery.cookie.js to #{rails_javascripts_dir}"
|
17
|
+
cp "#{dir}/jquery.cookie.js", rails_javascripts_dir
|
18
|
+
end
|
data/lib/cacheable_flash.rb
CHANGED
@@ -1,38 +1,39 @@
|
|
1
|
-
module CacheableFlash
|
2
|
-
|
3
|
-
|
4
|
-
|
5
|
-
|
6
|
-
require 'cacheable_flash/rails/
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
flash.
|
37
|
-
|
38
|
-
end
|
1
|
+
module CacheableFlash
|
2
|
+
require 'rails'
|
3
|
+
if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR == 0
|
4
|
+
require 'cacheable_flash/rails/railtie'
|
5
|
+
elsif ::Rails.version >= "3.1"
|
6
|
+
require 'cacheable_flash/rails/engine'
|
7
|
+
require 'cacheable_flash/rails/railtie'
|
8
|
+
else
|
9
|
+
# For older rails use generator
|
10
|
+
end
|
11
|
+
|
12
|
+
def self.included(base)
|
13
|
+
base.around_filter :write_flash_to_cookie
|
14
|
+
end
|
15
|
+
|
16
|
+
def write_flash_to_cookie
|
17
|
+
yield if block_given?
|
18
|
+
cookie_flash = if cookies['flash']
|
19
|
+
begin
|
20
|
+
ActiveSupport::JSON.decode(cookies['flash'])
|
21
|
+
rescue
|
22
|
+
{}
|
23
|
+
end
|
24
|
+
else
|
25
|
+
{}
|
26
|
+
end
|
27
|
+
|
28
|
+
flash.each do |key, value|
|
29
|
+
if cookie_flash[key.to_s].blank?
|
30
|
+
cookie_flash[key.to_s] = value.kind_of?(Numeric) ? value.to_s : value
|
31
|
+
else
|
32
|
+
cookie_flash[key.to_s] << "<br/>#{value}"
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
cookies['flash'] = cookie_flash.to_json.gsub("+", "%2B")
|
37
|
+
flash.clear
|
38
|
+
end
|
39
|
+
end
|
@@ -1,13 +1,8 @@
|
|
1
|
-
# Configure Rails 3.1:
|
2
|
-
# This gem contains this empty engine class which inherits from Rails::Engine.
|
3
|
-
# By doing this, Rails is informed that the directory for this gem may contain assets and the
|
4
|
-
# app/assets, lib/assets and vendor/assets directories of this engine are added to the search path of Sprockets.
|
5
|
-
module
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
end
|
11
|
-
|
12
|
-
end
|
13
|
-
end
|
1
|
+
# Configure Rails 3.1:
|
2
|
+
# This gem contains this empty engine class which inherits from Rails::Engine.
|
3
|
+
# By doing this, Rails is informed that the directory for this gem may contain assets and the
|
4
|
+
# app/assets, lib/assets and vendor/assets directories of this engine are added to the search path of Sprockets.
|
5
|
+
module CacheableFlash
|
6
|
+
class Engine < ::Rails::Engine
|
7
|
+
end
|
8
|
+
end
|
@@ -1,14 +1,14 @@
|
|
1
|
-
# Add cacheable flash JS to defaults for Rails < 3.1 (not needed with asset pipeline)
|
2
|
-
module Jquery
|
3
|
-
module Rails
|
4
|
-
|
5
|
-
class Railtie < ::Rails::Railtie
|
6
|
-
if ::Rails::VERSION::MAJOR == 3
|
7
|
-
config.before_configuration do
|
8
|
-
config.action_view.javascript_expansions[:cacheable_flash] = %w(flash jquery.cookie)
|
9
|
-
end
|
10
|
-
end
|
11
|
-
end
|
12
|
-
|
13
|
-
end
|
14
|
-
end
|
1
|
+
# Add cacheable flash JS to defaults for Rails < 3.1 (not needed with asset pipeline)
|
2
|
+
module Jquery
|
3
|
+
module Rails
|
4
|
+
|
5
|
+
class Railtie < ::Rails::Railtie
|
6
|
+
if ::Rails::VERSION::MAJOR == 3
|
7
|
+
config.before_configuration do
|
8
|
+
config.action_view.javascript_expansions[:cacheable_flash] = %w(flash jquery.cookie)
|
9
|
+
end
|
10
|
+
end
|
11
|
+
end
|
12
|
+
|
13
|
+
end
|
14
|
+
end
|
@@ -1,23 +1,23 @@
|
|
1
|
-
# encoding: utf-8
|
2
|
-
|
3
|
-
module CacheableFlash
|
4
|
-
# Copies javascript libraries flash.js and jquery.cookie.js to public/javascripts/ (Rails 3.0.X only, Rails 3.1 has asset pipeline)
|
5
|
-
#
|
6
|
-
# @example
|
7
|
-
# $ rails generate cacheable_flash:install
|
8
|
-
#
|
9
|
-
class InstallGenerator < Rails::Generators::Base
|
10
|
-
source_root File.expand_path('../../../../../vendor/assets/javascripts/', __FILE__)
|
11
|
-
|
12
|
-
# Rails 3.1 has the asset pipeline, no need to copy javascript files anymore
|
13
|
-
# Rails 3.0 doesn't have an asset pipeline, so we copy in javascript files
|
14
|
-
desc "Copies some JS files to public/javascripts/"
|
15
|
-
def copy_files
|
16
|
-
if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR == 1
|
17
|
-
ActiveSupport::Deprecation.warn("Rails 3.1 has the asset pipeline, so you only need to copy javascript files if you aren't using it.")
|
18
|
-
end
|
19
|
-
template 'flash.js', 'public/javascripts/flash.js'
|
20
|
-
template 'jquery.cookie.js', 'public/javascripts/jquery.cookie.js'
|
21
|
-
end
|
22
|
-
end
|
23
|
-
end
|
1
|
+
# encoding: utf-8
|
2
|
+
|
3
|
+
module CacheableFlash
|
4
|
+
# Copies javascript libraries flash.js and jquery.cookie.js to public/javascripts/ (Rails 3.0.X only, Rails 3.1 has asset pipeline)
|
5
|
+
#
|
6
|
+
# @example
|
7
|
+
# $ rails generate cacheable_flash:install
|
8
|
+
#
|
9
|
+
class InstallGenerator < Rails::Generators::Base
|
10
|
+
source_root File.expand_path('../../../../../vendor/assets/javascripts/', __FILE__)
|
11
|
+
|
12
|
+
# Rails 3.1 has the asset pipeline, no need to copy javascript files anymore
|
13
|
+
# Rails 3.0 doesn't have an asset pipeline, so we copy in javascript files
|
14
|
+
desc "Copies some JS files to public/javascripts/"
|
15
|
+
def copy_files
|
16
|
+
if ::Rails::VERSION::MAJOR == 3 && ::Rails::VERSION::MINOR == 1
|
17
|
+
ActiveSupport::Deprecation.warn("Rails 3.1 has the asset pipeline, so you only need to copy javascript files if you aren't using it.")
|
18
|
+
end
|
19
|
+
template 'flash.js', 'public/javascripts/flash.js'
|
20
|
+
template 'jquery.cookie.js', 'public/javascripts/jquery.cookie.js'
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|
@@ -1,101 +1,105 @@
|
|
1
|
-
require 'spec_helper'
|
2
|
-
|
3
|
-
describe 'CacheableFlash' do
|
4
|
-
attr_reader :controller_class, :controller, :cookies
|
5
|
-
before do
|
6
|
-
@controller_class = Struct.new(:cookies, :flash)
|
7
|
-
|
8
|
-
@controller_class.send(:include, CacheableFlash)
|
9
|
-
@controller = @controller_class.new({}, {})
|
10
|
-
@cookies = {}
|
11
|
-
stub(
|
12
|
-
end
|
13
|
-
|
14
|
-
describe "#write_flash_to_cookie" do
|
15
|
-
context "when there is not an existing flash cookie" do
|
16
|
-
it "sets the flash cookie with a JSON representation of the Hash" do
|
17
|
-
expected_flash = {
|
18
|
-
'errors' => "This is an Error",
|
19
|
-
'notice' => "This is a Notice"
|
20
|
-
}
|
21
|
-
controller.flash = expected_flash.dup
|
22
|
-
controller.write_flash_to_cookie
|
23
|
-
|
24
|
-
JSON.parse(@controller.cookies['flash']).should == expected_flash
|
25
|
-
end
|
26
|
-
end
|
27
|
-
|
28
|
-
context "when there is an existing flash cookie" do
|
29
|
-
context "when the flash cookie is valid json" do
|
30
|
-
it "appends new data to existing flash cookie" do
|
31
|
-
@cookies['flash'] = {
|
32
|
-
'notice' => "Existing notice",
|
33
|
-
'errors' => "Existing errors",
|
34
|
-
}.to_json
|
35
|
-
|
36
|
-
@controller.flash = {
|
37
|
-
'notice' => 'New notice',
|
38
|
-
'errors' => 'New errors',
|
39
|
-
}
|
40
|
-
|
41
|
-
@controller.write_flash_to_cookie
|
42
|
-
|
43
|
-
expected_flash = {
|
44
|
-
'notice' => "Existing notice<br/>New notice",
|
45
|
-
'errors' => "Existing errors<br/>New errors",
|
46
|
-
}
|
47
|
-
JSON.parse(@controller.cookies['flash']).should == expected_flash
|
48
|
-
end
|
49
|
-
end
|
50
|
-
|
51
|
-
context "when the flash cookie is 'invalid' json" do
|
52
|
-
it "does not have an error and starts with an empty Hash" do
|
53
|
-
@cookies['flash'] = ""
|
54
|
-
lambda do
|
55
|
-
JSON.parse(@cookies['flash'])
|
56
|
-
end.should raise_error(JSON::ParserError)
|
57
|
-
|
58
|
-
@controller.write_flash_to_cookie
|
59
|
-
|
60
|
-
JSON.parse(@cookies['flash']).should == {}
|
61
|
-
end
|
62
|
-
end
|
63
|
-
end
|
64
|
-
|
65
|
-
it "converts flash value to string before storing in cookie if value is a number" do
|
66
|
-
@controller.flash = { 'quantity' => 5 }
|
67
|
-
@controller.write_flash_to_cookie
|
68
|
-
JSON.parse(@controller.cookies['flash']).should == { 'quantity' => "5" }
|
69
|
-
end
|
70
|
-
|
71
|
-
it "does not convert flash value to string before storing in cookie if value is anything other than a number" do
|
72
|
-
@controller.flash = { 'foo' => { 'bar' => 'baz' } }
|
73
|
-
@controller.write_flash_to_cookie
|
74
|
-
JSON.parse(@controller.cookies['flash']).should == { 'foo' => { 'bar' => 'baz' } }
|
75
|
-
end
|
76
|
-
|
77
|
-
it "encodes plus signs in generated JSON before storing in cookie" do
|
78
|
-
@controller.flash = { 'notice' => 'Life, Love + Liberty' }
|
79
|
-
@controller.write_flash_to_cookie
|
80
|
-
|
81
|
-
|
82
|
-
|
83
|
-
|
84
|
-
|
85
|
-
|
86
|
-
|
87
|
-
|
88
|
-
|
89
|
-
|
90
|
-
|
91
|
-
|
92
|
-
|
93
|
-
|
94
|
-
|
95
|
-
|
96
|
-
|
97
|
-
|
98
|
-
|
99
|
-
|
100
|
-
|
101
|
-
|
1
|
+
require 'spec_helper'
|
2
|
+
|
3
|
+
describe 'CacheableFlash' do
|
4
|
+
attr_reader :controller_class, :controller, :cookies
|
5
|
+
before do
|
6
|
+
@controller_class = Struct.new(:cookies, :flash)
|
7
|
+
@controller_class.stub(:around_filter)
|
8
|
+
@controller_class.send(:include, CacheableFlash)
|
9
|
+
@controller = @controller_class.new({}, {})
|
10
|
+
@cookies = {}
|
11
|
+
@controller.stub(:cookies).and_return {@cookies}
|
12
|
+
end
|
13
|
+
|
14
|
+
describe "#write_flash_to_cookie" do
|
15
|
+
context "when there is not an existing flash cookie" do
|
16
|
+
it "sets the flash cookie with a JSON representation of the Hash" do
|
17
|
+
expected_flash = {
|
18
|
+
'errors' => "This is an Error",
|
19
|
+
'notice' => "This is a Notice"
|
20
|
+
}
|
21
|
+
controller.flash = expected_flash.dup
|
22
|
+
controller.write_flash_to_cookie
|
23
|
+
|
24
|
+
JSON.parse(@controller.cookies['flash']).should == expected_flash
|
25
|
+
end
|
26
|
+
end
|
27
|
+
|
28
|
+
context "when there is an existing flash cookie" do
|
29
|
+
context "when the flash cookie is valid json" do
|
30
|
+
it "appends new data to existing flash cookie" do
|
31
|
+
@cookies['flash'] = {
|
32
|
+
'notice' => "Existing notice",
|
33
|
+
'errors' => "Existing errors",
|
34
|
+
}.to_json
|
35
|
+
|
36
|
+
@controller.flash = {
|
37
|
+
'notice' => 'New notice',
|
38
|
+
'errors' => 'New errors',
|
39
|
+
}
|
40
|
+
|
41
|
+
@controller.write_flash_to_cookie
|
42
|
+
|
43
|
+
expected_flash = {
|
44
|
+
'notice' => "Existing notice<br/>New notice",
|
45
|
+
'errors' => "Existing errors<br/>New errors",
|
46
|
+
}
|
47
|
+
JSON.parse(@controller.cookies['flash']).should == expected_flash
|
48
|
+
end
|
49
|
+
end
|
50
|
+
|
51
|
+
context "when the flash cookie is 'invalid' json" do
|
52
|
+
it "does not have an error and starts with an empty Hash" do
|
53
|
+
@cookies['flash'] = ""
|
54
|
+
lambda do
|
55
|
+
JSON.parse(@cookies['flash'])
|
56
|
+
end.should raise_error(JSON::ParserError)
|
57
|
+
|
58
|
+
@controller.write_flash_to_cookie
|
59
|
+
|
60
|
+
JSON.parse(@cookies['flash']).should == {}
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
64
|
+
|
65
|
+
it "converts flash value to string before storing in cookie if value is a number" do
|
66
|
+
@controller.flash = { 'quantity' => 5 }
|
67
|
+
@controller.write_flash_to_cookie
|
68
|
+
JSON.parse(@controller.cookies['flash']).should == { 'quantity' => "5" }
|
69
|
+
end
|
70
|
+
|
71
|
+
it "does not convert flash value to string before storing in cookie if value is anything other than a number" do
|
72
|
+
@controller.flash = { 'foo' => { 'bar' => 'baz' } }
|
73
|
+
@controller.write_flash_to_cookie
|
74
|
+
JSON.parse(@controller.cookies['flash']).should == { 'foo' => { 'bar' => 'baz' } }
|
75
|
+
end
|
76
|
+
|
77
|
+
it "encodes plus signs in generated JSON before storing in cookie" do
|
78
|
+
@controller.flash = { 'notice' => 'Life, Love + Liberty' }
|
79
|
+
@controller.write_flash_to_cookie
|
80
|
+
if JSON::VERSION >= "1.6"
|
81
|
+
@controller.cookies['flash'].should == "{\"notice\":\"Life, Love %2B Liberty\"}"
|
82
|
+
else
|
83
|
+
@controller.cookies['flash'].should == "{\"notice\": \"Life, Love %2B Liberty\"}"
|
84
|
+
end
|
85
|
+
end
|
86
|
+
|
87
|
+
it "clears the controller.flash hash provided by Rails" do
|
88
|
+
flash = {
|
89
|
+
'errors' => "This is an Error",
|
90
|
+
'notice' => "This is a Notice"
|
91
|
+
}
|
92
|
+
@controller.flash = flash
|
93
|
+
@controller.write_flash_to_cookie
|
94
|
+
|
95
|
+
@controller.flash.should == {}
|
96
|
+
end
|
97
|
+
end
|
98
|
+
|
99
|
+
describe ".included" do
|
100
|
+
it "sets the around_filter on the controller to call #write_flash_to_cookie" do
|
101
|
+
@controller_class.should_receive(:around_filter).with(:write_flash_to_cookie)
|
102
|
+
@controller_class.send(:include, CacheableFlash)
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|