Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

copied from svn://rubyforge.org/var/svn/pivotalrb/erector/trunk. See …

…doc/svn-log-from-pivotalrb.txt for version history

git-svn-id: svn+ssh://rubyforge.org/var/svn/erector/trunk@2 4b8798b9-51f2-4387-91b1-b04f834b421f
  • Loading branch information...
commit 8e0bbea35b25f4e16b723aa088e3a94764345e84 1 parent 4c1d62a
alexch authored
34 README
View
@@ -0,0 +1,34 @@
+Erector
+=======
+
+With Erector, you define views in an object oriented fashion.
+
+Layout Inheritance
+------------------
+
+Erector replaces the typical Rails layout mechanism with a more natural construct, the use of inheritance. Want a common layout? Just implement a layout superclass and inherit from it. Implement render in the superclass and implement template methods in its subclasses. There's one trick you'll need to use this layout for non-erector templates. Here's an example.
+
+`application.rb` - The Erector layout superclass
+
+ class Views::Layouts::Application < Erector::Widget
+ attr_accessor :content
+
+ def render
+ html do
+ head { } # head content here
+ # body content here
+ body do
+ text content
+ end
+ end
+ end
+ end
+
+`application.mab` - The markaby template (adjust for other appropriately templating technologies)
+
+ widget = Views::Layouts::Application.new(self)
+ widget.content = content_for_layout
+ self << widget.to_s
+
+
+Here the abstract layout widget is used in a concrete fashion by the template-based layout. Normally, the `content` method would be implemented by subclassing widgets, but the layout template sets it directly and then calls to_s on the layout widget. This allows the same layout to be shared in a backward compatible way.
39 Rakefile
View
@@ -0,0 +1,39 @@
+require 'rake'
+require 'rake/testtask'
+require 'rake/rdoctask'
+require 'rubygems'
+require 'spec/rake/spectask'
+require 'rake/gempackagetask'
+
+desc 'Default: run unit tests.'
+task :default => :spec
+
+desc "Run the specs for the erector plugin"
+Spec::Rake::SpecTask.new do |t|
+ t.spec_files = FileList['spec/**/*_spec.rb']
+end
+
+desc 'Generate documentation for the widgets plugin.'
+Rake::RDocTask.new(:rdoc) do |rdoc|
+ rdoc.rdoc_dir = 'rdoc'
+ rdoc.title = 'Widgets'
+ rdoc.options << '--line-numbers' << '--inline-source'
+ rdoc.rdoc_files.include('README')
+ rdoc.rdoc_files.include('lib/**/*.rb')
+end
+
+gemspec = Gem::Specification.new do |s|
+ s.name = "erector"
+ s.version = "0.0.1"
+ s.author = "Pivotal Labs"
+ s.summary = "An object oriented HTML generation framework"
+ s.platform = Gem::Platform::RUBY
+ s.files = FileList["Rakefile", "{spec,lib}/**/*"].to_a
+ s.require_path = "lib"
+ s.autorequire = "treetop"
+ s.has_rdoc = true
+end
+
+Rake::GemPackageTask.new(gemspec) do |pkg|
+ pkg.need_tar = true
+end
76 doc/svn-log-from-pivotalrb.txt
View
@@ -0,0 +1,76 @@
+------------------------------------------------------------------------
+r1245 | btakita | 2008-01-31 09:41:00 -0800 (Thu, 31 Jan 2008) | 1 line
+Changed paths:
+ M /erector/trunk/lib/erector/widget.rb
+ M /erector/trunk/spec/erector/widget_spec.rb
+
+Renamed Widget#tag to #element and #standalone_tag to #standalone_element.
+------------------------------------------------------------------------
+r1244 | btakita | 2008-01-31 09:39:15 -0800 (Thu, 31 Jan 2008) | 1 line
+Changed paths:
+ A /erector/trunk/lib/erector/html_parts.rb
+ M /erector/trunk/lib/erector/widget.rb
+ M /erector/trunk/lib/erector.rb
+
+Renamed Widget#tag to #__tag__ and #standalone_tag to #__standalone_tag__. Moved HtmlParts into its own file.
+------------------------------------------------------------------------
+r1243 | btakita | 2008-01-30 17:28:39 -0800 (Wed, 30 Jan 2008) | 1 line
+Changed paths:
+ M /erector/trunk/lib/erector/helpers.rb
+
+CTI/BT - Adding a couple of rails helpers.
+------------------------------------------------------------------------
+r1242 | btakita | 2008-01-30 17:22:26 -0800 (Wed, 30 Jan 2008) | 1 line
+Changed paths:
+ M /erector/trunk/lib/erector/widget.rb
+ M /erector/trunk/spec/erector/widget_spec.rb
+
+CTI/BT - Added standalone tag support.
+------------------------------------------------------------------------
+r1179 | btakita | 2008-01-09 22:38:47 -0800 (Wed, 09 Jan 2008) | 1 line
+Changed paths:
+ D /erector/trunk/spec/erector/widgets/standard_table_spec.rb
+
+Removing failing spec.
+------------------------------------------------------------------------
+r1178 | btakita | 2008-01-09 22:37:14 -0800 (Wed, 09 Jan 2008) | 1 line
+Changed paths:
+ A /erector/trunk/README
+ A /erector/trunk/Rakefile
+ A /erector/trunk/erector.iml
+ A /erector/trunk/init.rb
+ A /erector/trunk/install.rb
+ A /erector/trunk/lib
+ A /erector/trunk/lib/erector
+ A /erector/trunk/lib/erector/extensions
+ A /erector/trunk/lib/erector/extensions/action_controller.rb
+ A /erector/trunk/lib/erector/extensions/object.rb
+ A /erector/trunk/lib/erector/helpers.rb
+ A /erector/trunk/lib/erector/widget.rb
+ A /erector/trunk/lib/erector/widgets
+ A /erector/trunk/lib/erector/widgets/table.rb
+ A /erector/trunk/lib/erector/widgets.rb
+ A /erector/trunk/lib/erector.rb
+ A /erector/trunk/spec
+ A /erector/trunk/spec/erector
+ A /erector/trunk/spec/erector/extensions
+ A /erector/trunk/spec/erector/extensions/render_widget_spec.rb
+ A /erector/trunk/spec/erector/widget_spec.rb
+ A /erector/trunk/spec/erector/widgets
+ A /erector/trunk/spec/erector/widgets/standard_table_spec.rb
+ A /erector/trunk/spec/erector/widgets/table_spec.rb
+ A /erector/trunk/spec/spec_helper.rb
+ A /erector/trunk/spec/spec_suite.rb
+ A /erector/trunk/spec/view_caching.rb
+ A /erector/trunk/tasks
+ A /erector/trunk/tasks/widgets_tasks.rake
+ A /erector/trunk/uninstall.rb
+
+BT - Adding erector,.
+------------------------------------------------------------------------
+r1172 | btakita | 2008-01-07 23:04:02 -0800 (Mon, 07 Jan 2008) | 1 line
+Changed paths:
+ A /erector/trunk
+
+trunk
+------------------------------------------------------------------------
16 erector.iml
View
@@ -0,0 +1,16 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<module relativePaths="true" type="RUBY_MODULE" version="4">
+ <component name="NewModuleRootManager" inherit-compiler-output="false">
+ <content url="file://$MODULE_DIR$" />
+ <orderEntry type="jdk" jdkName="Ruby SDK" jdkType="RUBY_SDK" />
+ <orderEntry type="sourceFolder" forTests="false" />
+ <orderEntryProperties />
+ </component>
+ <component name="RModuleContentRootManager">
+ <TEST_URLS url="file://$MODULE_DIR$/spec" />
+ </component>
+ <component name="RModuleSettingsStorage">
+ <option name="shouldUseRSpecTestFramework" value="true" />
+ </component>
+</module>
+
2  init.rb
View
@@ -0,0 +1,2 @@
+# Include hook code here
+require "erector"
1  install.rb
View
@@ -0,0 +1 @@
+# Install hook code here
7 lib/erector.rb
View
@@ -0,0 +1,7 @@
+require "action_controller"
+require "erector/extensions/action_controller"
+require "erector/extensions/object"
+require "erector/helpers"
+require "erector/html_parts"
+require "erector/widget"
+require "erector/widgets"
13 lib/erector/extensions/action_controller.rb
View
@@ -0,0 +1,13 @@
+class ActionController::Base
+ def render_widget(widget_class, assigns=@assigns)
+ render :text => render_widget_to_string(widget_class, assigns)
+ end
+
+ def render_widget_to_string(widget_class, assigns = @assigns)
+ add_variables_to_assigns
+ @rendered_widget = widget_class.new(@template, assigns.merge(:params => params))
+ @rendered_widget.to_s
+ end
+
+ attr_reader :rendered_widget
+end
5 lib/erector/extensions/object.rb
View
@@ -0,0 +1,5 @@
+class Object
+ def metaclass
+ class << self; self; end
+ end
+end
51 lib/erector/helpers.rb
View
@@ -0,0 +1,51 @@
+module Erector
+ module Helpers
+ [
+ :link_to,
+ :image_tag,
+ :javascript_include_tag,
+ :stylesheet_link_tag,
+ :link_to_function,
+ :link_to_remote,
+ :sortable_element,
+ :sortable_element_js,
+ :mail_to
+ ].each do |helper_name|
+ define_method helper_name do |*args|
+ text helpers.send(helper_name, *args)
+ end
+ end
+
+ def javascript_include_merged(key)
+ helpers.javascript_include_merged(key)
+ end
+
+ def stylesheet_link_merged(key)
+ helpers.stylesheet_link_merged(key)
+ end
+
+ def flash
+ helpers.controller.send(:flash)
+ end
+
+ def session
+ helpers.controller.session
+ end
+
+ def cycle(*args)
+ helpers.cycle(*args)
+ end
+
+ def simple_format(*args)
+ helpers.simple_format(*args)
+ end
+
+ def time_ago_in_words(*args)
+ helpers.time_ago_in_words(*args)
+ end
+
+ def pluralize(*args)
+ helpers.pluralize(*args)
+ end
+ end
+end
34 lib/erector/html_parts.rb
View
@@ -0,0 +1,34 @@
+module Erector
+ class HtmlParts < Array
+ def to_s
+ map do |part|
+ case part['type']
+ when 'open'
+ part['attributes'] ?
+ "<#{part['tagName']}#{format_attributes(part['attributes'])}>" :
+ "<#{part['tagName']}>"
+ when 'close'
+ "</#{part['tagName']}>"
+ when 'standalone'
+ part['attributes'] ?
+ "<#{part['tagName']}#{format_attributes(part['attributes'])} />" :
+ "<#{part['tagName']} />"
+ when 'text'
+ part['value'].to_s
+ when 'instruct'
+ "<?xml#{format_attributes(part['attributes'])}?>"
+ end
+ end.join
+ end
+
+ protected
+ def format_attributes(attributes)
+ return "" if !attributes || attributes.empty?
+ results = ['']
+ attributes.each do |key, value|
+ results << "#{key}=#{value.to_s.inspect}" if value
+ end
+ results.join ' '
+ end
+ end
+end
180 lib/erector/widget.rb
View
@@ -0,0 +1,180 @@
+module Erector
+ class Widget
+ class << self
+ def all_tags
+ Erector::Widget.full_tags + Erector::Widget.standalone_tags
+ end
+
+ def standalone_tags
+ ['area', 'base', 'br', 'hr', 'img', 'input', 'link', 'meta']
+ end
+
+ def full_tags
+ [
+ 'a', 'acronym', 'address', 'b', 'bdo', 'big', 'blockquote', 'body',
+ 'button', 'caption', 'cite', 'code', 'dd', 'del', 'div', 'dl', 'dt', 'em',
+ 'fieldset', 'form', 'h1', 'h2', 'h3', 'h4', 'h5', 'h6', 'head', 'html', 'i',
+ 'iframe', 'ins', 'kbd', 'label', 'legend', 'li', 'map',
+ 'noframes', 'noscript', 'ol', 'optgroup', 'option', 'p', 'param', 'pre',
+ 'samp', 'script', 'select', 'small', 'span', 'strong', 'style', 'sub', 'sup',
+ 'table', 'tbody', 'td', 'textarea', 'th', 'thead', 'title', 'tr', 'tt', 'u', 'ul', 'var'
+ ]
+ end
+ end
+
+ include ActionController::UrlWriter, Helpers
+ attr_reader :helpers
+ attr_reader :assigns
+ attr_reader :doc
+ attr_reader :block
+ attr_reader :parent
+
+ # Each item in @doc is an array containing three values: type, value, attributes
+ def initialize(helpers=nil, assigns={}, doc = HtmlParts.new, &block)
+ @assigns = assigns
+ assigns.each do |name, value|
+ instance_variable_set("@#{name}", value)
+ metaclass.module_eval do
+ attr_reader name
+ end
+ end
+ @helpers = helpers
+ fake_erbout
+ @parent = block ? eval("self", block.binding) : nil
+ @doc = doc
+ @block = block
+ end
+
+ def render
+ if @block
+ instance_eval(&@block)
+ end
+ end
+
+ def widget(widget_class, assigns={}, &block)
+ child = widget_class.new(helpers, assigns, doc, &block)
+ child.render
+ end
+
+ def h(content)
+ text CGI.escapeHTML(content)
+ end
+
+ def open_tag(tag_name, attributes={})
+ @doc << {'type' => 'open', 'tagName' => tag_name, 'attributes' => attributes}
+ end
+
+ def text(value)
+ @doc << {'type' => 'text', 'value' => value}
+ nil
+ end
+
+ def close_tag(tag_name)
+ @doc << {'type' => 'close', 'tagName' => tag_name}
+ end
+
+ def instruct!(attributes={:version => "1.0", :encoding => "UTF-8"})
+ @doc << {'type' => 'instruct', 'attributes' => attributes}
+ end
+
+ def javascript(*args, &blk)
+ params = args[0] if args[0].is_a?(Hash)
+ params ||= args[1] if args[1].is_a?(Hash)
+ unless params
+ params = {}
+ args << params
+ end
+ params[:type] = "text/javascript"
+ script(*args, &blk)
+ end
+
+ def __element__(tag_name, *args, &block)
+ if args.length > 2
+ raise ArgumentError, "Cannot accept more than three arguments"
+ end
+ attributes, value = nil, nil
+ arg0 = args[0]
+ if arg0.is_a?(Hash)
+ attributes = arg0
+ else
+ value = arg0.to_s
+ arg1 = args[1]
+ if arg1.is_a?(Hash)
+ attributes = arg1
+ end
+ end
+ attributes ||= {}
+ open_tag tag_name, attributes
+ if block
+ instance_eval(&block)
+ else
+ text value
+ end
+ close_tag tag_name
+ end
+ alias_method :element, :__element__
+
+ def __standalone_element__(tag_name, attributes={})
+ @doc << {'type' => 'standalone', 'tagName' => tag_name, 'attributes' => attributes}
+ end
+ alias_method :standalone_element, :__standalone_element__
+
+ def capture(&block)
+ begin
+ original_doc = @doc
+ @doc = HtmlParts.new
+ yield
+ @doc.to_s
+ ensure
+ @doc = original_doc
+ end
+ end
+
+ def to_s(&blk)
+ return @__to_s if @__to_s
+ render(&blk)
+ @__to_s = @doc.to_s
+ end
+
+ alias_method :inspect, :to_s
+
+ full_tags.each do |tag_name|
+ self.class_eval(
+ "def #{tag_name}(*args, &block)\n" <<
+ " __element__('#{tag_name}', *args, &block)\n" <<
+ "end",
+ __FILE__,
+ __LINE__ - 4
+ )
+ end
+
+ standalone_tags.each do |tag_name|
+ self.class_eval(
+ "def #{tag_name}(*args, &block)\n" <<
+ " __standalone_element__('#{tag_name}', *args, &block)\n" <<
+ "end",
+ __FILE__,
+ __LINE__ - 4
+ )
+ end
+
+ protected
+ def method_missing(name, *args, &block)
+ block ||= lambda {} # captures self HERE
+ if @parent
+ @parent.send(name, *args, &block)
+ else
+ super
+ end
+ end
+
+ def fake_erbout
+ widget = self
+ @helpers.metaclass.class_eval do
+ define_method :concat do |some_text, binding|
+ widget.text some_text
+ end
+ end
+ end
+ end
+end
1  lib/erector/widgets.rb
View
@@ -0,0 +1 @@
+require "erector/widgets/table"
45 lib/erector/widgets/table.rb
View
@@ -0,0 +1,45 @@
+module Erector
+ module Widgets
+ class Table < Erector::Widget
+ ColumnDefinition = Struct.new(:id, :name, :cell_proc)
+ class << self
+ def column(id, name=id.to_s.humanize.titleize, &cell_proc)
+ cell_proc ||= proc {|object| text object.__send__(id)}
+ column_definitions << ColumnDefinition.new(id, name, cell_proc)
+ end
+
+ def column_definitions
+ @column_definitions ||= []
+ end
+ end
+
+ def render
+ table do
+ tr do
+ column_definitions.each do |column_def|
+ th do
+ h column_def.name
+ end
+ end
+ end
+ tbody do
+ @row_objects.each do |object|
+ tr do
+ column_definitions.each do |column_def|
+ td do
+ self.instance_exec(object, &column_def.cell_proc)
+ end
+ end
+ end
+ end
+ end
+ end
+ end
+
+ protected
+ def column_definitions
+ self.class.column_definitions
+ end
+ end
+ end
+end
49 spec/erector/extensions/render_widget_spec.rb
View
@@ -0,0 +1,49 @@
+require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
+
+module BaseSpec
+ class TestWidgetController < ActionController::Base
+ def index_with_implicit_assigns
+ @foobar = "foobar"
+ render_widget Erector::TestWidget
+ end
+
+ def index_with_explicit_assigns
+ render_widget Erector::TestWidget, :foobar => "foobar"
+ end
+ end
+
+ class Erector::TestWidget < Erector::Widget
+ def render
+ text @foobar
+ end
+ end
+
+ describe TestWidgetController, "#render_widget" do
+ before do
+ @controller = BaseSpec::TestWidgetController.new
+ @request = ActionController::TestRequest.new
+ @response = ActionController::TestResponse.new
+ @controller.send(:initialize_template_class, @response)
+ @controller.send(:assign_shortcuts, @request, @response)
+ class << @controller
+ public :rendered_widget
+ end
+ end
+
+ it "assigns to @rendered_widget" do
+ @controller.rendered_widget.should be_nil
+ @controller.render_widget Erector::TestWidget
+ @controller.rendered_widget.should be_instance_of(Erector::TestWidget)
+ end
+
+ it "instantiates a widget with implicit assigns" do
+ @controller.index_with_implicit_assigns
+ @response.body.should == "foobar"
+ end
+
+ it "instantiates a widget with explicit assigns" do
+ @controller.index_with_explicit_assigns
+ @response.body.should == "foobar"
+ end
+ end
+end
226 spec/erector/widget_spec.rb
View
@@ -0,0 +1,226 @@
+require File.expand_path("#{File.dirname(__FILE__)}/../spec_helper")
+
+module WidgetSpec
+ describe Erector::Widget do
+ describe ".all_tags" do
+ it "returns set of full and standalone tags" do
+ Erector::Widget.all_tags.class.should == Array
+ Erector::Widget.all_tags.should == Erector::Widget.full_tags + Erector::Widget.standalone_tags
+ end
+ end
+
+ describe "#instruct!" do
+ it "when passed no arguments; returns an instruct element with version 1 and utf-8" do
+ html = Erector::Widget.new do
+ instruct!
+ end.to_s
+ html.should include("<?xml")
+ html.should include('encoding="UTF-8"')
+ html.should include('version="1.0')
+ html.should include("?>")
+ end
+ end
+
+ describe "#element" do
+ it "when receiving one argument; returns an empty element" do
+ Erector::Widget.new do
+ element('div')
+ end.to_s.should == "<div></div>"
+ end
+
+ it "with a attribute hash; returns an empty element with the attributes" do
+ html = Erector::Widget.new do
+ element(
+ 'div',
+ :class => "foo bar",
+ :style => "display: none; color: white; float: left;",
+ :nil_attribute => nil
+ )
+ end.to_s
+ doc = Hpricot(html)
+ div = doc.at('div')
+ div[:class].should == "foo bar"
+ div[:style].should == "display: none; color: white; float: left;"
+ div[:nil_attribute].should be_nil
+ end
+
+ it "with inner tags; returns nested tags" do
+ widget = Erector::Widget.new do
+ element 'div' do
+ element 'div'
+ end
+ end
+ widget.to_s.should == '<div><div></div></div>'
+ end
+
+ it "with text; returns element with inner text" do
+ Erector::Widget.new do
+ element 'div', 'test text'
+ end.to_s.should == "<div>test text</div>"
+ end
+
+ it "with object other than hash; returns element with inner text == object.to_s" do
+ object = ['a', 'b']
+ Erector::Widget.new do
+ element 'div', object
+ end.to_s.should == "<div>#{object.to_s}</div>"
+ end
+
+ it "with parameters and block; returns element with inner html and attributes" do
+ Erector::Widget.new do
+ element 'div', 'class' => "foobar" do
+ element 'span', 'style' => 'display: none;'
+ end
+ end.to_s.should == '<div class="foobar"><span style="display: none;"></span></div>'
+ end
+
+ it "with content and parameters; returns element with content as inner html and attributes" do
+ Erector::Widget.new do
+ element 'div', 'test text', :style => "display: none;"
+ end.to_s.should == '<div style="display: none;">test text</div>'
+ end
+
+ it "with more than three arguments; raises ArgumentError" do
+ proc do
+ Erector::Widget.new do
+ element 'div', 'foobar', {}, 'fourth'
+ end.to_s
+ end.should raise_error(ArgumentError)
+ end
+
+ it "renders the proper full tags" do
+ Erector::Widget.full_tags.each do |tag_name|
+ expected = "<#{tag_name}></#{tag_name}>"
+ actual = Erector::Widget.new do
+ send(tag_name)
+ end.to_s
+ begin
+ actual.should == expected
+ rescue Spec::Expectations::ExpectationNotMetError => e
+ puts "Expected #{tag_name} to be a full element. Expected #{expected}, got #{actual}"
+ raise e
+ end
+ end
+ end
+ end
+
+ describe "#standalone_element" do
+ it "when receiving attributes, renders a standalone_element with the attributes" do
+ Erector::Widget.new do
+ standalone_element 'input', :name => 'foo[bar]'
+ end.to_s.should == '<input name="foo[bar]" />'
+ end
+
+ it "when not receiving attributes, renders a standalone_element without attributes" do
+ Erector::Widget.new do
+ standalone_element 'br'
+ end.to_s.should == '<br />'
+ end
+
+ it "renders the proper standalone tags" do
+ ['area', 'base', 'br', 'hr', 'img', 'input', 'link', 'meta'].each do |tag_name|
+ expected = "<#{tag_name} />"
+ actual = Erector::Widget.new do
+ send(tag_name)
+ end.to_s
+ begin
+ actual.should == expected
+ rescue Spec::Expectations::ExpectationNotMetError => e
+ puts "Expected #{tag_name} to be a standalone element. Expected #{expected}, got #{actual}"
+ raise e
+ end
+ end
+ end
+ end
+
+ describe "#javascript" do
+ it "when receiving a block; renders the content inside of a script text/javascript element" do
+ body = Erector::Widget.new do
+ javascript do
+ text 'alert("hello");'
+ end
+ end.to_s
+ doc = Hpricot(body)
+ script_tag = doc.at("script")
+ script_tag[:type].should == "text/javascript"
+ script_tag.inner_html.should include('alert("hello");')
+ end
+
+ it "when receiving a params hash; renders a source file" do
+ html = Erector::Widget.new do
+ javascript(:src => "/my/js/file.js")
+ end.to_s
+ doc = Hpricot(html)
+ doc.at('/')[:src].should == "/my/js/file.js"
+ end
+
+ it "when receiving text and a params hash; renders a source file" do
+ html = Erector::Widget.new do
+ javascript('alert("hello");', :src => "/my/js/file.js")
+ end.to_s
+ doc = Hpricot(html)
+ script_tag = doc.at('script')
+ script_tag[:src].should == "/my/js/file.js"
+ script_tag.inner_html.should include('alert("hello");')
+ end
+ end
+
+ describe '#capture' do
+ it "should return content rather than write it to the buffer" do
+ widget = Erector::Widget.new do
+ captured = capture do
+ p 'Captured Content'
+ end
+ div do
+ text captured
+ end
+ end
+ widget.to_s.should == '<div><p>Captured Content</p></div>'
+ end
+
+ it "works with nested captures" do
+ widget = Erector::Widget.new do
+ captured = capture do
+ captured = capture do
+ p 'Nested Capture'
+ end
+ p 'Captured Content'
+ text captured
+ end
+ div do
+ text captured
+ end
+ end
+ widget.to_s.should == '<div><p>Captured Content</p><p>Nested Capture</p></div>'
+ end
+ end
+
+ describe '#widget' do
+ before do
+ class Parent < Erector::Widget
+ def render
+ text 1
+ widget Child do
+ text 2
+ third
+ end
+ end
+
+ def third
+ text 3
+ end
+ end
+
+ class Child < Erector::Widget
+ def render
+ super
+ end
+ end
+ end
+
+ it "renders nested widgets in the correct order" do
+ Parent.new.to_s.should == '123'
+ end
+ end
+ end
+end
104 spec/erector/widgets/table_spec.rb
View
@@ -0,0 +1,104 @@
+require File.expand_path("#{File.dirname(__FILE__)}/../../spec_helper")
+
+module TableSpec
+ class DefaultsTestTable < Erector::Widgets::Table
+ column :column_a
+ column :column_b
+ column :column_c
+ end
+
+ class CustomHeadingTable < Erector::Widgets::Table
+ column :a, "Column - A"
+ end
+
+ class CustomCellTable < Erector::Widgets::Table
+ column :a do |obj|
+ span obj.a
+ end
+ end
+
+ describe Erector::Widgets::Table do
+ before do
+ view_cache do
+ widget = CustomHeadingTable.new(
+ nil,
+ :row_objects => []
+ )
+ widget.to_s
+ end
+ end
+
+ it "renders a tbody to be compatible with IE6" do
+ doc.at("tbody").should_not be_nil
+ end
+ end
+
+ describe Erector::Widgets::Table, "with custom heading" do
+ before do
+ view_cache do
+ widget = CustomHeadingTable.new(
+ nil,
+ :row_objects => []
+ )
+ widget.to_s
+ end
+ end
+
+ it "renders a custom heading" do
+ table = doc.at("table")
+ table.at("th").inner_html.should == "Column - A"
+ end
+ end
+
+ describe Erector::Widgets::Table, "with custom cell content" do
+ before do
+ @object1 = Struct.new(:a).new("Hello")
+ view_cache do
+ widget = CustomCellTable.new(
+ nil,
+ :row_objects => [@object1]
+ )
+ widget.to_s
+ end
+ end
+
+ it "renders custom cell html" do
+ table = doc.at("table")
+ row = table.search("tr")[1]
+ row.at("td").inner_html.should == "<span>Hello</span>"
+ end
+ end
+
+ describe Erector::Widgets::Table, "with default heading and cell definitions" do
+ before do
+ @object1 = Struct.new(:column_a, :column_b, :column_c).new(1, 2, 3)
+ @object2 = Struct.new(:column_a, :column_b, :column_c).new(4, 5, 6)
+ view_cache do
+ widget = DefaultsTestTable.new(
+ nil,
+ :row_objects => [@object1, @object2]
+ )
+ widget.to_s
+ end
+ @table = doc.at("table")
+ end
+
+ it "renders column titles" do
+ title_row = @table.at("tr")
+ titles = title_row.search("th").collect {|heading| heading.inner_html}
+ titles.should == [ "Column A", "Column B", "Column C" ]
+ end
+
+ it "renders data" do
+ data_rows = @table.search("tr")[1..-1]
+ cell_values = data_rows.collect do |row|
+ row.search("td").collect {|col| col.inner_html}
+ end
+
+ cell_values.should == [
+ ['1', '2', '3'],
+ ['4', '5', '6']
+ ]
+ end
+ end
+end
13 spec/spec_helper.rb
View
@@ -0,0 +1,13 @@
+dir = File.dirname(__FILE__)
+require "rubygems"
+require "active_record"
+require "spec"
+require "#{dir}/view_caching"
+$LOAD_PATH.unshift("#{dir}/../lib")
+require "erector"
+require "hpricot"
+require "action_controller/test_process"
+
+Spec::Runner.configure do |config|
+ config.include ViewCaching
+end
4 spec/spec_suite.rb
View
@@ -0,0 +1,4 @@
+dir = File.dirname(__FILE__)
+Dir["#{dir}/**/*_spec.rb"].each do |file|
+ require file
+end
30 spec/view_caching.rb
View
@@ -0,0 +1,30 @@
+module ViewCaching
+ def self.included(mod)
+ mod.extend ClassMethods
+ end
+ module ClassMethods
+ def view_cache
+ @view_cache ||= {}
+ end
+ end
+
+ def view_cache(&blk)
+ cache = self.class.view_cache
+ if cache.empty?
+ cache[:body] = @body = yield
+ cache[:doc] = @doc = Hpricot(@body)
+ else
+ @body = cache[:body]
+ @doc = cache[:doc]
+ end
+ end
+
+ def doc
+ @doc
+ end
+
+ def body
+ @body
+ end
+ alias_method :html, :body
+end
4 tasks/widgets_tasks.rake
View
@@ -0,0 +1,4 @@
+# desc "Explaining what the task does"
+# task :widgets do
+# # Task goes here
+# end
1  uninstall.rb
View
@@ -0,0 +1 @@
+# Uninstall hook code here
Please sign in to comment.
Something went wrong with that request. Please try again.