Permalink
Browse files

Table render style, step 1

  • Loading branch information...
1 parent 769a9e8 commit 5961e617c8ef60ae9961944f7bb286905f973978 @kaineer committed Feb 9, 2011
View
@@ -5,10 +5,10 @@ class Tag
def initialize( name = nil, &block )
@name = name
@attributes = {}
- @block = block
@close_tag = false
@items = []
+ block.call( self ) if block_given?
end
# Dsl part
@@ -50,7 +50,7 @@ def render_tag
end
def has_content?
- !@items.empty? || @block
+ !@items.empty?
end
def render_tag_with_attributes
@@ -64,7 +64,6 @@ def render_open_tag
end
def render_content
- @block.call( self ) if @block
@items.map do |item|
item.is_a?( Tag ) ? item.render : item.to_s
end.join
@@ -1,2 +1,3 @@
require 'ream/form/render_style/null'
require 'ream/form/render_style/transparent'
+require 'ream/form/render_style/table'
@@ -0,0 +1,33 @@
+require 'ream/form/render_style/base'
+
+module Ream::Form::RenderStyle
+ class Table
+
+ include Ream::Form::Html
+
+ def render_form_content( raw_html, opts = {} )
+ t = tag( "table" )
+ t.render_open_tag +
+ raw_html +
+ t.render_close_tag
+ end
+
+ def render_input( raw_html, opts = {} )
+ tag( "tr" ) do |tr|
+ tr.tag( "td" ) do |td|
+ td.tag( "label" ) do |l|
+ l.text( opts[ :label ] )
+ l.attr( "for", opts[ :id ] ) if opts[ :id ]
+ end
+ end
+ tr.tag( "td" ) do |td|
+ td.text( raw_html )
+ end
+ end.render
+ end
+
+ def id_attribute( opts = {} )
+ opts[ :id ] ? " id='#{opts[:id]}'" : ""
+ end
+ end
+end
View
@@ -1,5 +1,6 @@
require 'test/spec'
require 'mocha'
+require 'nokogiri'
require 'redgreen'
# require 'leftright'
@@ -14,7 +15,7 @@
class ::TestForm < Form
def initialize( render_style = nil )
super( "/someurl" ) do |f|
- f.submit( "Big submit button", :label => "button-label" )
+ f.submit( "Big submit button", :label => "button-label", :id => "teh_button" )
end
@render_style = render_style
@@ -0,0 +1,48 @@
+require 'test/test_helper'
+
+context "Form with table render style" do
+ setup do
+ @myform = ::TestForm.new( RenderStyle::Table.new )
+ @doc = Nokogiri::XML( @myform.render )
+ end
+
+ it "should contain table" do
+ @doc.xpath( "//table" ).size.should.be == 1
+ end
+
+ it "should contain table with one row" do
+ @doc.xpath( "//table/tr" ).size.should.be == 1
+ end
+
+ it "should contain table with one row and two td inside" do
+ @doc.xpath( "//table/tr/td" ).size.should.be == 2
+ end
+
+ context ".. label inside" do
+ setup do
+ @label = @doc.xpath( "//td/label" ).first
+ end
+
+ it "should exist" do
+ @label.should.not.be.nil?
+ end
+
+ it "should have text 'button-label'" do
+ @label.text().should.be == "button-label"
+ end
+
+ it "should have for='teh_button'" do
+ @label['for'].should.be == "teh_button"
+ end
+ end
+
+ context ".. second td" do
+ setup do
+ @td = @doc.xpath( "//table/tr/td" )[ 1 ]
+ end
+
+ it "should contain input" do
+ @td.xpath( "input" ).size.should == 1
+ end
+ end
+end

0 comments on commit 5961e61

Please sign in to comment.