Changeset 1291

Show
Ignore:
Timestamp:
01/12/08 20:11:36 (11 months ago)
Author:
sethrasmuss..@gmail.com
Message:

snag tag() from merb_helpers and give it some steroids.. maybe all the tag helpers belong in merb_helpers? all of tag()'s relatives are in merb so this move makes sense for now

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/lib/merb/mixins/view_context.rb

    r1228 r1291  
    508508    end 
    509509     
     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     
    510535    # Creates the opening tag with attributes for the provided +name+ 
    511536    # attrs is a hash where all members will be mapped to key="value" 
     
    513538    # Note: This tag will need to be closed 
    514539    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?}>" 
    516541    end 
    517542     
     
    526551    # +attrs+ : a hash where all members will be mapped to key="value" 
    527552    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 
    531556end 
  • trunk/spec/merb/view_context_spec.rb

    r1228 r1291  
    11require File.dirname(__FILE__) + '/../spec_helper' 
     2require FIXTURES + '/controllers/render_spec_controllers' 
    23 
    34describe "View Context", "image tag" do 
     
    310311 
    311312describe 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   
    313324  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   
    317328  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   
    322333  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   
    326337  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   
    330342  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 
     366end