Permalink
Browse files

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

  • Loading branch information...
1 parent b7b9a50 commit 90777694f4080e50511bb89ca782fb73b247c1bd @rf- committed Dec 14, 2012
Showing with 25 additions and 7 deletions.
  1. +4 −1 lib/keynote.rb
  2. +2 −2 lib/keynote/controller.rb
  3. +2 −2 lib/keynote/helper.rb
  4. +2 −2 lib/keynote/presenter.rb
  5. +15 −0 spec/keynote_spec.rb
View
@@ -15,6 +15,7 @@
module Keynote
class << self
# Create or retrieve a presenter wrapping zero or more objects.
+ # If a block is given, yield the presenter into it.
#
# The first parameter is a Rails view context, but you'll usually access
# this method through `Keynote::Helper#present`,
@@ -52,7 +53,9 @@ def present(view, *objects)
end
Cache.fetch(name, view, *objects) do
- presenter_from_name(name).new(view, *objects)
+ presenter_from_name(name).new(view, *objects).tap do |presenter|
+ yield presenter if block_given?
+ end
end
end
@@ -7,8 +7,8 @@ module Keynote
module Controller
# Instantiate a presenter.
# @see Keynote.present
- def present(*objects)
- Keynote.present(view_context, *objects)
+ def present(*objects, &blk)
+ Keynote.present(view_context, *objects, &blk)
end
alias k present
end
@@ -6,8 +6,8 @@ module Keynote
module Helper
# Instantiate a presenter.
# @see Keynote.present
- def present(*objects)
- Keynote.present(self, *objects)
+ def present(*objects, &blk)
+ Keynote.present(self, *objects, &blk)
end
alias k present
end
@@ -56,8 +56,8 @@ def initialize(view_context)
# Instantiate another presenter.
# @see Keynote.present
- def present(*objects)
- Keynote.present(@view, *objects)
+ def present(*objects, &blk)
+ Keynote.present(@view, *objects, &blk)
end
alias k present
View
@@ -26,6 +26,21 @@
p.model.must_equal 'hello'
end
+ it "should take a block and pass the presenter into it" do
+ m = mock()
+ m.expects(:block_yielded)
+
+ Keynote.present(:view, :normal, 'hello') do |p|
+ m.block_yielded
+
+ p.wont_be_nil
+ p.must_be_instance_of NormalPresenter
+
+ p.view.must_equal :view
+ p.model.must_equal 'hello'
+ end
+ end
+
it "should integrate with Rumble" do
p = Keynote.present(:view, model)
rx = /<div>&lt;script&gt;alert\(/

0 comments on commit 9077769

Please sign in to comment.