acts_as_cached 0.1.1 → 0.1.2

Sign up to get free protection for your applications and to get access to all the features.
@@ -31,14 +31,45 @@ ActionView::Base.class_eval do
31
31
 
32
32
  end
33
33
 
34
- def render_object(object, view, options = {})
34
+ # Will render and cache a partial
35
+ # If the object has been removed or doesn't exists nothing will be rendered
35
36
 
36
- logger.info "render_cached: object: #{object} view: #{view} options #{options.inspect}"
37
+ def render_object(object, view, options={})
38
+ options[:object] = object
39
+ do_render_object(view, options)
40
+ end
41
+
42
+
43
+ def render_object_klass_id(klass, id, view, options = {})
44
+ options[:klass] = klass
45
+ options[:id] = id
46
+ do_render_object(view, options)
47
+ end
48
+
49
+ private
50
+
51
+
52
+ def do_render_object(view, options={})
53
+
54
+ klass, id =
55
+ if options[:object]
56
+ [options[:object].class, options[:object].id]
57
+ else
58
+ [options[:klass], options[:id]]
59
+ end
60
+
61
+ unless klass && id
62
+ raise "options[:object] or options[:id] and options[:klass] must be specified"
63
+ end
64
+
65
+ id = id.to_i
66
+
67
+ logger.info "render_cached: class: #{klass.name} :id #{id} view: #{view} options #{options.inspect}"
37
68
 
38
69
 
39
70
  # XXX validate that object is an acts_as_cached FIXME
40
71
 
41
- namespace = "#{object.class.name}:#{object.id}"
72
+ namespace = "#{klass.name}:#{id}"
42
73
  if render_options = options[:options]
43
74
  key = "#{view}:#{render_options.to_key}"
44
75
  else
@@ -48,7 +79,7 @@ ActionView::Base.class_eval do
48
79
 
49
80
  fragment_proc = Proc.new do
50
81
  partial = options[:partial]
51
- class_lower = object.class.name.underscore
82
+ class_lower = klass.name.underscore
52
83
  unless partial
53
84
  if directory = options[:directory]
54
85
  partial = "#{directory}/#{class_lower}_#{view}"
@@ -56,7 +87,13 @@ ActionView::Base.class_eval do
56
87
  partial = "#{class_lower}_#{view}"
57
88
  end
58
89
  end
59
- render :partial => partial, :locals => { class_lower.to_sym => object, :options => render_options }
90
+ begin
91
+
92
+ object = view_extensions_fetch_object(options)
93
+ render :partial => partial, :locals => { class_lower.to_sym => object, :options => render_options }
94
+ rescue ActiveRecord::RecordNotFound => e
95
+ ''
96
+ end
60
97
  end
61
98
 
62
99
  unless options[:cache] == false
@@ -66,18 +103,34 @@ ActionView::Base.class_eval do
66
103
  end
67
104
 
68
105
  if transformer = options[:transform]
69
- case transformer
70
- when Symbol
71
- return send(transformer, object, fragment)
72
- when Proc
73
- return transformer.call(object, fragment)
74
- else
75
- raise TypeError, "Transformer must be either symbol or proc"
106
+
107
+ begin
108
+ object = view_extensions_fetch_object(options)
109
+
110
+ case transformer
111
+ when Symbol
112
+ return send(transformer, object, fragment)
113
+ when Proc
114
+ return transformer.call(object, fragment)
115
+ else
116
+ raise TypeError, "Transformer must be either symbol or proc"
117
+ end
118
+ rescue ActiveRecord::RecordNotFound => e
119
+ ''
76
120
  end
77
121
  else
78
122
  return fragment
79
123
  end
80
124
 
81
125
  end
126
+
127
+ def view_extensions_fetch_object(options)
128
+ if options[:object]
129
+ options[:object]
130
+ else
131
+ options[:klass].find(options[:id].to_i)
132
+ end
133
+ end
134
+
82
135
  end
83
136
 
@@ -1,8 +1,83 @@
1
1
  require 'test/unit'
2
-
2
+ require 'rubygems'
3
+ require 'action_pack'
4
+ require 'mocha'
5
+ require 'action_controller'
6
+ require 'action_view'
7
+ require 'view_extensions'
8
+ require 'active_record'
3
9
  class ActsAsCachedTest < Test::Unit::TestCase
4
- # Replace this with your real tests.
5
- def test_this_plugin
6
- flunk
10
+
11
+ class MockActiveRecord
12
+ attr_accessor :id
13
+ def initialize(id)
14
+ @id = id
15
+ end
16
+
17
+ end
18
+
19
+ def setup
20
+ @action_view = ActionView::Base.new
21
+ @action_view.stubs(:render).returns("yoyo")
22
+ @action_view.stubs(:logger).returns(stub_everything)
23
+ end
24
+
25
+ def test_render_object_cached
26
+ obj = MockActiveRecord.new(1)
27
+ MockActiveRecord.expects(:find).never
28
+ cached_value(@action_view, 'yoyo')
29
+
30
+ assert_equal "yoyo", @action_view.render_object(obj, 'view')
31
+ end
32
+
33
+ def test_render_object_uncached
34
+ obj = MockActiveRecord.new(1)
35
+ MockActiveRecord.expects(:find).never
36
+ uncached_value(@action_view)
37
+
38
+ assert_equal "yoyo", @action_view.render_object(obj, 'view')
39
+ end
40
+
41
+
42
+ def test_render_object_klass_id_cached
43
+ obj = MockActiveRecord.new(1)
44
+ MockActiveRecord.expects(:find).never
45
+ cached_value(@action_view, 'yoyo')
46
+ assert_equal "yoyo", @action_view.render_object_klass_id(MockActiveRecord, 1, 'view')
47
+ end
48
+
49
+ def test_render_object_klass_id_cached_uncached
50
+ obj = MockActiveRecord.new(1)
51
+ MockActiveRecord.expects(:find).with(1).once.returns(obj)
52
+ uncached_value(@action_view)
53
+
54
+ assert_equal "yoyo", @action_view.render_object_klass_id(MockActiveRecord, 1, 'view')
55
+
56
+ end
57
+
58
+ def test_render_object_klass_id_missing_object
59
+ obj = MockActiveRecord.new(1)
60
+ MockActiveRecord.expects(:find).with(1).once.raises(ActiveRecord::RecordNotFound)
61
+ uncached_value(@action_view)
62
+
63
+ assert_equal "", @action_view.render_object_klass_id(MockActiveRecord, 1, 'view')
64
+
65
+ end
66
+
67
+ def cached_value(obj, value)
68
+ obj.class.class_eval <<-EOS
69
+ def cache_value(*args)
70
+ '#{value}'
71
+ end
72
+ EOS
7
73
  end
74
+
75
+ def uncached_value(obj)
76
+ obj.class.class_eval do
77
+ def cache_value(namespace, expire, &proc)
78
+ proc.call
79
+ end
80
+ end
81
+ end
82
+
8
83
  end
metadata CHANGED
@@ -1,57 +1,65 @@
1
1
  --- !ruby/object:Gem::Specification
2
- rubygems_version: 0.8.11
3
- specification_version: 1
4
2
  name: acts_as_cached
5
3
  version: !ruby/object:Gem::Version
6
- version: 0.1.1
7
- date: 2006-12-22 00:00:00 +01:00
8
- summary: Caching helpers for Rails
9
- require_paths:
10
- - lib
11
- email:
12
- homepage:
13
- rubyforge_project:
14
- description:
15
- autorequire: acts_as_cached
16
- default_executable:
17
- bindir: bin
18
- has_rdoc: true
19
- required_ruby_version: !ruby/object:Gem::Version::Requirement
20
- requirements:
21
- - - ">"
22
- - !ruby/object:Gem::Version
23
- version: 0.0.0
24
- version:
4
+ version: 0.1.2
25
5
  platform: ruby
26
- signing_key:
27
- cert_chain:
28
6
  authors:
29
7
  - Adocca Entertainment AB
30
- files:
31
- - lib/acts_as_cached.rb
32
- - lib/view_extensions.rb
33
- - test/acts_as_cached_test.rb
34
- - README
35
- test_files: []
36
-
37
- rdoc_options:
38
- - --line-numbers
39
- - --inline-source
40
- extra_rdoc_files:
41
- - README
42
- executables: []
43
-
44
- extensions: []
45
-
46
- requirements: []
8
+ autorequire: acts_as_cached
9
+ bindir: bin
10
+ cert_chain: []
47
11
 
12
+ date: 2008-05-27 00:00:00 +02:00
13
+ default_executable:
48
14
  dependencies:
49
15
  - !ruby/object:Gem::Dependency
50
16
  name: AdoccaMemcache
51
17
  version_requirement:
52
- version_requirements: !ruby/object:Gem::Version::Requirement
18
+ version_requirements: !ruby/object:Gem::Requirement
53
19
  requirements:
54
20
  - - ">="
55
21
  - !ruby/object:Gem::Version
56
22
  version: 0.1.0
57
23
  version:
24
+ description:
25
+ email:
26
+ executables: []
27
+
28
+ extensions: []
29
+
30
+ extra_rdoc_files:
31
+ - README
32
+ files:
33
+ - lib/acts_as_cached.rb
34
+ - lib/view_extensions.rb
35
+ - test/acts_as_cached_test.rb
36
+ - README
37
+ has_rdoc: true
38
+ homepage:
39
+ post_install_message:
40
+ rdoc_options:
41
+ - --line-numbers
42
+ - --inline-source
43
+ require_paths:
44
+ - lib
45
+ required_ruby_version: !ruby/object:Gem::Requirement
46
+ requirements:
47
+ - - ">="
48
+ - !ruby/object:Gem::Version
49
+ version: "0"
50
+ version:
51
+ required_rubygems_version: !ruby/object:Gem::Requirement
52
+ requirements:
53
+ - - ">="
54
+ - !ruby/object:Gem::Version
55
+ version: "0"
56
+ version:
57
+ requirements: []
58
+
59
+ rubyforge_project:
60
+ rubygems_version: 1.1.1
61
+ signing_key:
62
+ specification_version: 2
63
+ summary: Caching helpers for Rails
64
+ test_files: []
65
+