Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Allow block form of present() (fix #2)

  • Loading branch information...
commit 90777694f4080e50511bb89ca782fb73b247c1bd 1 parent b7b9a50
Ryan Fitzgerald authored December 14, 2012
5  lib/keynote.rb
@@ -15,6 +15,7 @@
15 15
 module Keynote
16 16
   class << self
17 17
     # Create or retrieve a presenter wrapping zero or more objects.
  18
+    # If a block is given, yield the presenter into it.
18 19
     #
19 20
     # The first parameter is a Rails view context, but you'll usually access
20 21
     # this method through `Keynote::Helper#present`,
@@ -52,7 +53,9 @@ def present(view, *objects)
52 53
       end
53 54
 
54 55
       Cache.fetch(name, view, *objects) do
55  
-        presenter_from_name(name).new(view, *objects)
  56
+        presenter_from_name(name).new(view, *objects).tap do |presenter|
  57
+          yield presenter if block_given?
  58
+        end
56 59
       end
57 60
     end
58 61
 
4  lib/keynote/controller.rb
@@ -7,8 +7,8 @@ module Keynote
7 7
   module Controller
8 8
     # Instantiate a presenter.
9 9
     # @see Keynote.present
10  
-    def present(*objects)
11  
-      Keynote.present(view_context, *objects)
  10
+    def present(*objects, &blk)
  11
+      Keynote.present(view_context, *objects, &blk)
12 12
     end
13 13
     alias k present
14 14
   end
4  lib/keynote/helper.rb
@@ -6,8 +6,8 @@ module Keynote
6 6
   module Helper
7 7
     # Instantiate a presenter.
8 8
     # @see Keynote.present
9  
-    def present(*objects)
10  
-      Keynote.present(self, *objects)
  9
+    def present(*objects, &blk)
  10
+      Keynote.present(self, *objects, &blk)
11 11
     end
12 12
     alias k present
13 13
   end
4  lib/keynote/presenter.rb
@@ -56,8 +56,8 @@ def initialize(view_context)
56 56
 
57 57
     # Instantiate another presenter.
58 58
     # @see Keynote.present
59  
-    def present(*objects)
60  
-      Keynote.present(@view, *objects)
  59
+    def present(*objects, &blk)
  60
+      Keynote.present(@view, *objects, &blk)
61 61
     end
62 62
     alias k present
63 63
 
15  spec/keynote_spec.rb
@@ -26,6 +26,21 @@
26 26
       p.model.must_equal 'hello'
27 27
     end
28 28
 
  29
+    it "should take a block and pass the presenter into it" do
  30
+      m = mock()
  31
+      m.expects(:block_yielded)
  32
+
  33
+      Keynote.present(:view, :normal, 'hello') do |p|
  34
+        m.block_yielded
  35
+
  36
+        p.wont_be_nil
  37
+        p.must_be_instance_of NormalPresenter
  38
+
  39
+        p.view.must_equal  :view
  40
+        p.model.must_equal 'hello'
  41
+      end
  42
+    end
  43
+
29 44
     it "should integrate with Rumble" do
30 45
       p  = Keynote.present(:view, model)
31 46
       rx = /<div>&lt;script&gt;alert\(/

0 notes on commit 9077769

Please sign in to comment.
Something went wrong with that request. Please try again.