ryanb-render-caching 0.1.1
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/CHANGELOG +0 -0
- data/LICENSE +20 -0
- data/Manifest +12 -0
- data/README +61 -0
- data/TODO +0 -0
- data/lib/render_caching.rb +2 -0
- data/lib/render_caching/controller_additions.rb +21 -0
- data/render-caching.gemspec +50 -0
- data/spec/render_caching/controller_additions_spec.rb +82 -0
- data/spec/spec_helper.rb +11 -0
- data/tasks/deployment.rake +2 -0
- data/tasks/spec.rake +9 -0
- metadata +76 -0
data/CHANGELOG
ADDED
File without changes
|
data/LICENSE
ADDED
@@ -0,0 +1,20 @@
|
|
1
|
+
Copyright (c) 2008 Ryan Bates
|
2
|
+
|
3
|
+
Permission is hereby granted, free of charge, to any person obtaining
|
4
|
+
a copy of this software and associated documentation files (the
|
5
|
+
"Software"), to deal in the Software without restriction, including
|
6
|
+
without limitation the rights to use, copy, modify, merge, publish,
|
7
|
+
distribute, sublicense, and/or sell copies of the Software, and to
|
8
|
+
permit persons to whom the Software is furnished to do so, subject to
|
9
|
+
the following conditions:
|
10
|
+
|
11
|
+
The above copyright notice and this permission notice shall be
|
12
|
+
included in all copies or substantial portions of the Software.
|
13
|
+
|
14
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
15
|
+
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
16
|
+
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
17
|
+
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
18
|
+
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
19
|
+
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
20
|
+
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
data/Manifest
ADDED
@@ -0,0 +1,12 @@
|
|
1
|
+
CHANGELOG
|
2
|
+
lib/render_caching/controller_additions.rb
|
3
|
+
lib/render_caching.rb
|
4
|
+
LICENSE
|
5
|
+
Manifest
|
6
|
+
README
|
7
|
+
render-caching.gemspec
|
8
|
+
spec/render_caching/controller_additions_spec.rb
|
9
|
+
spec/spec_helper.rb
|
10
|
+
tasks/deployment.rake
|
11
|
+
tasks/spec.rake
|
12
|
+
TODO
|
data/README
ADDED
@@ -0,0 +1,61 @@
|
|
1
|
+
= Render Caching
|
2
|
+
|
3
|
+
Cache render calls in Rails controllers.
|
4
|
+
|
5
|
+
|
6
|
+
== Install
|
7
|
+
|
8
|
+
First install the gem.
|
9
|
+
|
10
|
+
gem install ryanb-render-caching --source=http://gems.github.com
|
11
|
+
|
12
|
+
Then specify it in your Rails config.
|
13
|
+
|
14
|
+
gem.config 'ryanb-render-caching', :lib => 'render_caching', :source => 'http://gems.github.com'
|
15
|
+
|
16
|
+
Rails 2.1 or later required.
|
17
|
+
|
18
|
+
|
19
|
+
== Usage
|
20
|
+
|
21
|
+
This gem adds the render_with_cache method to all controllers. Call
|
22
|
+
this inside of an action to cache the view.
|
23
|
+
|
24
|
+
def show
|
25
|
+
@user = User.find(params[:id])
|
26
|
+
render_with_cache
|
27
|
+
end
|
28
|
+
|
29
|
+
This will cache the full rendered contents into a key matching the URL
|
30
|
+
path (similar to action caching). You can change this key by simply
|
31
|
+
passing any parameter.
|
32
|
+
|
33
|
+
def show
|
34
|
+
@user = User.find(params[:id])
|
35
|
+
render_with_cache @user.cache_key
|
36
|
+
end
|
37
|
+
|
38
|
+
Cache key is a method supplied by Rails. This includes the updated_at
|
39
|
+
time which will give you an auto-expiring cache when the user record is
|
40
|
+
updated.
|
41
|
+
|
42
|
+
You can also supply a block to the render call which will only get
|
43
|
+
executed if there is no cache. Here is a good place to do any custom
|
44
|
+
render calls.
|
45
|
+
|
46
|
+
def show
|
47
|
+
@user = User.find(params[:id])
|
48
|
+
render_with_cache @user.cache_key do
|
49
|
+
render :layout => false
|
50
|
+
end
|
51
|
+
end
|
52
|
+
|
53
|
+
|
54
|
+
== Development
|
55
|
+
|
56
|
+
This project can be found on github at the following URL.
|
57
|
+
|
58
|
+
http://github.com/ryanb/render-caching/
|
59
|
+
|
60
|
+
If you would like to contribute to this project, please fork the
|
61
|
+
repository and send me a pull request.
|
data/TODO
ADDED
File without changes
|
@@ -0,0 +1,21 @@
|
|
1
|
+
module RenderCaching
|
2
|
+
module ControllerAdditions
|
3
|
+
private
|
4
|
+
|
5
|
+
def render_with_cache(key = nil, options = nil)
|
6
|
+
key ||= request.request_uri
|
7
|
+
body = Rails.cache.read(key)
|
8
|
+
if body
|
9
|
+
render :text => body
|
10
|
+
else
|
11
|
+
yield if block_given?
|
12
|
+
render unless performed?
|
13
|
+
Rails.cache.write(key, response.body, options)
|
14
|
+
end
|
15
|
+
end
|
16
|
+
end
|
17
|
+
end
|
18
|
+
|
19
|
+
class ActionController::Base
|
20
|
+
include RenderCaching::ControllerAdditions
|
21
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
|
2
|
+
# Gem::Specification for Render-caching-0.1.1
|
3
|
+
# Originally generated by Echoe
|
4
|
+
|
5
|
+
Gem::Specification.new do |s|
|
6
|
+
s.name = %q{render-caching}
|
7
|
+
s.version = "0.1.1"
|
8
|
+
|
9
|
+
s.specification_version = 2 if s.respond_to? :specification_version=
|
10
|
+
|
11
|
+
s.required_rubygems_version = Gem::Requirement.new(">= 0") if s.respond_to? :required_rubygems_version=
|
12
|
+
s.authors = ["Ryan Bates"]
|
13
|
+
s.date = %q{2008-06-19}
|
14
|
+
s.description = %q{Cache render calls in Rails controllers.}
|
15
|
+
s.email = %q{ryan (at) railscasts (dot) com}
|
16
|
+
s.extra_rdoc_files = ["CHANGELOG", "lib/render_caching/controller_additions.rb", "lib/render_caching.rb", "LICENSE", "README", "tasks/deployment.rake", "tasks/spec.rake", "TODO"]
|
17
|
+
s.files = ["CHANGELOG", "lib/render_caching/controller_additions.rb", "lib/render_caching.rb", "LICENSE", "Manifest", "README", "render-caching.gemspec", "spec/render_caching/controller_additions_spec.rb", "spec/spec_helper.rb", "tasks/deployment.rake", "tasks/spec.rake", "TODO"]
|
18
|
+
s.has_rdoc = true
|
19
|
+
s.homepage = %q{http://github.com/ryanb/render-caching}
|
20
|
+
s.rdoc_options = ["--line-numbers", "--inline-source", "--title", "Render-caching", "--main", "README"]
|
21
|
+
s.require_paths = ["lib"]
|
22
|
+
s.rubyforge_project = %q{render-caching}
|
23
|
+
s.rubygems_version = %q{1.0.0}
|
24
|
+
s.summary = %q{Cache render calls in Rails controllers.}
|
25
|
+
end
|
26
|
+
|
27
|
+
|
28
|
+
# # Original Rakefile source (requires the Echoe gem):
|
29
|
+
#
|
30
|
+
# require 'rubygems'
|
31
|
+
# require 'rake'
|
32
|
+
#
|
33
|
+
# begin
|
34
|
+
# require 'echoe'
|
35
|
+
#
|
36
|
+
# Echoe.new('render-caching', '0.1.1') do |p|
|
37
|
+
# p.summary = "Cache render calls in Rails controllers."
|
38
|
+
# p.description = "Cache render calls in Rails controllers."
|
39
|
+
# p.url = "http://github.com/ryanb/render-caching"
|
40
|
+
# p.author = 'Ryan Bates'
|
41
|
+
# p.email = "ryan (at) railscasts (dot) com"
|
42
|
+
# p.ignore_pattern = ["script/*"]
|
43
|
+
# end
|
44
|
+
#
|
45
|
+
# rescue LoadError => boom
|
46
|
+
# puts "You are missing a dependency required for meta-operations on this gem."
|
47
|
+
# puts "#{boom.to_s.capitalize}."
|
48
|
+
# end
|
49
|
+
#
|
50
|
+
# Dir["#{File.dirname(__FILE__)}/tasks/*.rake"].sort.each { |ext| load ext }
|
@@ -0,0 +1,82 @@
|
|
1
|
+
require File.dirname(__FILE__) + '/../spec_helper'
|
2
|
+
|
3
|
+
# stub the Rails module functionality
|
4
|
+
RAILS_CACHE = ActiveSupport::Cache.lookup_store(:memory_store)
|
5
|
+
module Rails
|
6
|
+
def self.cache
|
7
|
+
RAILS_CACHE
|
8
|
+
end
|
9
|
+
end
|
10
|
+
|
11
|
+
describe ActionController::Base do
|
12
|
+
it "should have render_with_cache private method" do
|
13
|
+
ActionController::Base.new.private_methods.should include('render_with_cache')
|
14
|
+
end
|
15
|
+
end
|
16
|
+
|
17
|
+
|
18
|
+
describe RenderCaching::ControllerAdditions do
|
19
|
+
include RenderCaching::ControllerAdditions
|
20
|
+
|
21
|
+
before(:each) do
|
22
|
+
Rails.cache.clear
|
23
|
+
@request = stub
|
24
|
+
@response = stub(:body => '')
|
25
|
+
stubs(:request).returns(@request)
|
26
|
+
stubs(:response).returns(@response)
|
27
|
+
stubs(:performed?)
|
28
|
+
stubs(:render)
|
29
|
+
end
|
30
|
+
|
31
|
+
it "should read from the cache with request uri as key and render that text" do
|
32
|
+
@request.stubs(:request_uri).returns('/foo/bar')
|
33
|
+
Rails.cache.write('/foo/bar', 'page content')
|
34
|
+
expects(:render).with(:text => 'page content')
|
35
|
+
render_with_cache
|
36
|
+
end
|
37
|
+
|
38
|
+
it "should read from the cache with custom passed key and render that text" do
|
39
|
+
Rails.cache.write('my_key', 'page content')
|
40
|
+
expects(:render).with(:text => 'page content')
|
41
|
+
render_with_cache 'my_key'
|
42
|
+
end
|
43
|
+
|
44
|
+
it "should save response.body to cache as key when not specified" do
|
45
|
+
@response.stubs(:body).returns('content')
|
46
|
+
render_with_cache 'some_key'
|
47
|
+
Rails.cache.read('some_key').should == 'content'
|
48
|
+
end
|
49
|
+
|
50
|
+
it "should call render when not cached or rendered yet" do
|
51
|
+
stubs(:performed?).returns(false)
|
52
|
+
expects(:render).with()
|
53
|
+
render_with_cache 'some_key'
|
54
|
+
end
|
55
|
+
|
56
|
+
it "should not call render if already rendered" do
|
57
|
+
stubs(:performed?).returns(true)
|
58
|
+
stubs(:render).raises('should not be called')
|
59
|
+
lambda { render_with_cache 'some_key' }.should_not raise_error
|
60
|
+
end
|
61
|
+
|
62
|
+
it "should not call render :text if cache doesn't exist" do
|
63
|
+
stubs(:render).with(:text => @response.body).raises('should not be called')
|
64
|
+
lambda { render_with_cache 'some_key' }.should_not raise_error
|
65
|
+
end
|
66
|
+
|
67
|
+
it "should yield to block when not cached" do
|
68
|
+
pass = false
|
69
|
+
render_with_cache('some_key') { pass = true }
|
70
|
+
pass.should be_true
|
71
|
+
end
|
72
|
+
|
73
|
+
it "should not yield to block when cached" do
|
74
|
+
Rails.cache.write('some_key', 'page content')
|
75
|
+
render_with_cache('some_key') { violated('block was executed') }
|
76
|
+
end
|
77
|
+
|
78
|
+
it "should pass options to cache write call" do
|
79
|
+
Rails.cache.expects(:write).with('some_key', @response.body, :expires_in => 5)
|
80
|
+
render_with_cache('some_key', :expires_in => 5)
|
81
|
+
end
|
82
|
+
end
|
data/spec/spec_helper.rb
ADDED
@@ -0,0 +1,11 @@
|
|
1
|
+
require 'rubygems'
|
2
|
+
require 'spec'
|
3
|
+
require 'active_support'
|
4
|
+
require 'active_record'
|
5
|
+
require 'action_controller'
|
6
|
+
require 'action_view'
|
7
|
+
require File.dirname(__FILE__) + '/../lib/render_caching.rb'
|
8
|
+
|
9
|
+
Spec::Runner.configure do |config|
|
10
|
+
config.mock_with :mocha
|
11
|
+
end
|
data/tasks/spec.rake
ADDED
metadata
ADDED
@@ -0,0 +1,76 @@
|
|
1
|
+
--- !ruby/object:Gem::Specification
|
2
|
+
name: ryanb-render-caching
|
3
|
+
version: !ruby/object:Gem::Version
|
4
|
+
version: 0.1.1
|
5
|
+
platform: ruby
|
6
|
+
authors:
|
7
|
+
- Ryan Bates
|
8
|
+
autorequire:
|
9
|
+
bindir: bin
|
10
|
+
cert_chain: []
|
11
|
+
|
12
|
+
date: 2008-06-19 00:00:00 -07:00
|
13
|
+
default_executable:
|
14
|
+
dependencies: []
|
15
|
+
|
16
|
+
description: Cache render calls in Rails controllers.
|
17
|
+
email: ryan (at) railscasts (dot) com
|
18
|
+
executables: []
|
19
|
+
|
20
|
+
extensions: []
|
21
|
+
|
22
|
+
extra_rdoc_files:
|
23
|
+
- CHANGELOG
|
24
|
+
- lib/render_caching/controller_additions.rb
|
25
|
+
- lib/render_caching.rb
|
26
|
+
- LICENSE
|
27
|
+
- README
|
28
|
+
- tasks/deployment.rake
|
29
|
+
- tasks/spec.rake
|
30
|
+
- TODO
|
31
|
+
files:
|
32
|
+
- CHANGELOG
|
33
|
+
- lib/render_caching/controller_additions.rb
|
34
|
+
- lib/render_caching.rb
|
35
|
+
- LICENSE
|
36
|
+
- Manifest
|
37
|
+
- README
|
38
|
+
- render-caching.gemspec
|
39
|
+
- spec/render_caching/controller_additions_spec.rb
|
40
|
+
- spec/spec_helper.rb
|
41
|
+
- tasks/deployment.rake
|
42
|
+
- tasks/spec.rake
|
43
|
+
- TODO
|
44
|
+
has_rdoc: true
|
45
|
+
homepage: http://github.com/ryanb/render-caching
|
46
|
+
post_install_message:
|
47
|
+
rdoc_options:
|
48
|
+
- --line-numbers
|
49
|
+
- --inline-source
|
50
|
+
- --title
|
51
|
+
- Render-caching
|
52
|
+
- --main
|
53
|
+
- README
|
54
|
+
require_paths:
|
55
|
+
- lib
|
56
|
+
required_ruby_version: !ruby/object:Gem::Requirement
|
57
|
+
requirements:
|
58
|
+
- - ">="
|
59
|
+
- !ruby/object:Gem::Version
|
60
|
+
version: "0"
|
61
|
+
version:
|
62
|
+
required_rubygems_version: !ruby/object:Gem::Requirement
|
63
|
+
requirements:
|
64
|
+
- - ">="
|
65
|
+
- !ruby/object:Gem::Version
|
66
|
+
version: "0"
|
67
|
+
version:
|
68
|
+
requirements: []
|
69
|
+
|
70
|
+
rubyforge_project: render-caching
|
71
|
+
rubygems_version: 1.0.1
|
72
|
+
signing_key:
|
73
|
+
specification_version: 2
|
74
|
+
summary: Cache render calls in Rails controllers.
|
75
|
+
test_files: []
|
76
|
+
|