Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

partials receive the variables available to the parent

  • Loading branch information...
commit dbb18d55856144128f059f7b44adec1e3141badc 1 parent 3e89cc4
@baccigalupi authored
View
1  TODO
@@ -8,7 +8,6 @@ view
caching of full views
partial
- pass the block content to partial instances too
partial passes down params from parent view
renderers
View
12 lib/view.rb
@@ -333,7 +333,17 @@ def self.render(opts={}, &block)
def partial(*args, &block)
view = if (klass = args.first).is_a?(Class)
args.shift
- klass.new(*args)
+ partial_opts = args.shift || {}
+ available_opts = self.class.default_variables.merge(options)
+ opts = if klass.selective_require
+ klass.required.each do |key|
+ partial_opts[key] ||= available_opts[key]
+ end
+ partial_opts
+ else
+ available_opts.merge(partial_opts)
+ end
+ klass.new(opts)
else
args.first
end
View
2  spec/view/view_basics_spec.rb
@@ -60,7 +60,7 @@ def alt_content
view.block.is_a?(Proc).should be_true
end
- it 'saves the options' do
+ it 'saves the initialization options' do
view = BasicView.new(:foo => 'foo', :bar => 'bar')
view.options.should == {:foo => 'foo', :bar => 'bar'}
end
View
44 spec/view/view_partial_spec.rb
@@ -72,6 +72,50 @@ def content
partial = @view.buffer.last
partial.block.is_a?(Proc).should be_true
end
+
+ describe 'passing down arguments' do
+ class Containment < Garterbelt::View
+ needs :x => 'find me'
+ def content
+ partial PartedOut
+ end
+ end
+
+ it 'works with defaults' do
+ view = Containment.new
+ view.content
+
+ parted = view.buffer.last
+ parted.is_a?(PartedOut).should be_true
+ parted.x.should == 'find me'
+ end
+
+ it 'should recieved initailialized options' do
+ view = Containment.new(:x => 'newer x')
+ view.content
+
+ parted = view.buffer.last
+ parted.x.should == 'newer x'
+ end
+
+ it 'should pass all the initialization options along' do
+ view = Containment.new(:y => 'two dimensions, yo!')
+ view.content
+
+ parted = view.buffer.last
+ parted.y.should == 'two dimensions, yo!'
+ end
+
+ it 'only passes required arguments if partial is selective' do
+ PartedOut.selective_require = true
+ view = Containment.new(:z => 'two dimensions, again')
+ view.content
+
+ parted = view.buffer.last
+ parted.x.should == 'find me'
+ parted.should_not respond_to :z
+ end
+ end
end
end
end
Please sign in to comment.
Something went wrong with that request. Please try again.