Changeset 1291
- Timestamp:
- 01/12/08 20:11:36 (11 months ago)
- Files:
-
- trunk/lib/merb/mixins/view_context.rb (modified) (3 diffs)
- trunk/spec/merb/view_context_spec.rb (modified) (2 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/lib/merb/mixins/view_context.rb
r1228 r1291 508 508 end 509 509 510 # Creates a generic HTML tag. You can invoke it a variety of ways. 511 # 512 # tag :div 513 # # <div></div> 514 # 515 # tag :div, :class => 'class' 516 # # <div class="class"></div> 517 # 518 # tag :div do 519 # 'content' 520 # end 521 # # <div>content</div> 522 # 523 # tag :div, :class => 'class' do 524 # 'content' 525 # end 526 # # <div class="class">content</div> 527 # 528 # 529 def tag(name, contents = nil, attrs = {}, &block) 530 attrs = contents if contents.is_a?(Hash) 531 contents = capture(&block) if block_given? 532 open_tag(name, attrs) + contents.to_s + close_tag(name) 533 end 534 510 535 # Creates the opening tag with attributes for the provided +name+ 511 536 # attrs is a hash where all members will be mapped to key="value" … … 513 538 # Note: This tag will need to be closed 514 539 def open_tag(name, attrs = nil) 515 "<#{name}#{ (' ' + attrs.to_html_attributes)if attrs && !attrs.empty?}>"540 "<#{name}#{' ' + attrs.to_html_attributes if attrs && !attrs.empty?}>" 516 541 end 517 542 … … 526 551 # +attrs+ : a hash where all members will be mapped to key="value" 527 552 def self_closing_tag(name, attrs = nil) 528 "<#{name}#{' ' + attrs.to_html_attributes if attrs }/>"529 end 530 end 553 "<#{name}#{' ' + attrs.to_html_attributes if attrs && !attrs.empty?}/>" 554 end 555 end 531 556 end trunk/spec/merb/view_context_spec.rb
r1228 r1291 1 1 require File.dirname(__FILE__) + '/../spec_helper' 2 require FIXTURES + '/controllers/render_spec_controllers' 2 3 3 4 describe "View Context", "image tag" do … … 310 311 311 312 describe Merb::ViewContextMixin do 312 313 # The use of this here is to avoid conflict with the tag() 314 # Hpricot test helper invoked via match_tag() when testing the 315 # tag() view context helper. 316 class FakeViewContext 317 include Merb::ErubisCaptureMixin, Merb::ViewContextMixin 318 end 319 320 before :each do 321 @view = FakeViewContext.new 322 end 323 313 324 it "should render the start of a tag" do 314 open_tag(:div).should == "<div>"315 end 316 325 @view.open_tag(:div).should match_tag(:div) 326 end 327 317 328 it "should render the start of a tag with attributes" do 318 tag = open_tag(:div, :id => 1, :class => "CLASS")319 tag.should match_tag(:div, :id => "1", :class => "CLASS")320 end 321 329 @view.open_tag(:div, :id => 1, :class => "CLASS"). 330 should match_tag(:div, :id => "1", :class => "CLASS") 331 end 332 322 333 it "should render a self closing tag" do 323 self_closing_tag(:br).should == "<br/>"324 end 325 334 @view.self_closing_tag(:br).should match_tag(:br) 335 end 336 326 337 it "should render a self closing tag with attributes" do 327 self_closing_tag(:img, :src => "SOURCE").should match_tag(:img, :src => "SOURCE") 328 end 329 338 @view.self_closing_tag(:img, :src => "SOURCE"). 339 should match_tag(:img, :src => "SOURCE") 340 end 341 330 342 it "should render a closing tag" do 331 close_tag(:div).should == "</div>" 332 end 333 334 end 343 @view.close_tag(:div).should == "</div>" 344 end 345 346 it 'should render a tag with content' do 347 @view.tag(:div, 'divs rool').should match_tag(:div, :content => 'divs rool') 348 end 349 350 it 'should render a tag with content and attributes' do 351 @view.tag(:div, 'divs rool', :class => 'classy'). 352 should match_tag(:div, :content => 'divs rool', :class => 'classy') 353 end 354 355 it 'should render a tag using a block for its contents' do 356 @view.tag(:div) do 357 'divs rool' 358 end.should match_tag(:div, :content => 'divs rool') 359 end 360 361 it 'should render a tag with attributes using a block for its contents' do 362 @view.tag(:div, :class => 'classy') do 363 'divs rool' 364 end.should match_tag(:div, :content => 'divs rool', :class => 'classy') 365 end 366 end
