Permalink
Browse files

changing to new Widget.new API.

  • Loading branch information...
1 parent e730629 commit 4cb2d0214b46c117dedd43709a566e6cf964976b @apotonick committed Oct 10, 2011
@@ -87,6 +87,8 @@ def url_for_event(type, options)
# TODO: remove me! only needed in widget_shortcuts.
def parent_controller
+ puts "parent_controller"
+ #apotomo_root
self
end
@@ -75,6 +75,7 @@ def responds_to_event(*options)
# Same as #responds_to_event but executed on the widget instance, only.
def respond_to_event(type, options={})
+ # DISCUSS: do we need the :once option? how could we avoid re-adding?
options = options.reverse_merge(:once => true,
:with => type,
:on => widget_id)
@@ -1,22 +1,16 @@
-# stolen from ... ? couldn't find the original lib on the net.
-### TODO: insert copyright notice!
-
module Apotomo
module TreeNode
include Enumerable
attr_reader :name, :childrenHash
attr_accessor :parent
- def self.included(base)
- base.after_initialize :initialize_tree_node
- end
-
- def initialize_tree_node(*)
- root!
-
+ def setup_tree_node(parent)
+ @parent = nil
@childrenHash = {}
- @children = []
+ @children = []
+
+ parent.add_widget(self) if parent.kind_of? Widget # TODO: as long as cells needs parent_controller.
end
# Print the string representation of this node.
@@ -30,21 +24,16 @@ def to_s
# children hierarchies in the tree.
# E.g. root << child << grand_child
def <<(child)
- add(child)
+ constant_for(child[:class]).new(self, child[:id], child[:options]) if child.is_a?(Hash) # TODO: move to WidgetCreationProxy.
end
- # Adds the specified child node to the receiver node.
- # The child node's parent is set to be the receiver.
- # The child is added as the last child in the current
- # list of children for the receiver node.
- def add(child)
+ def add_widget(child) # TODO: rename #add, make private
raise "Child already added" if @childrenHash.has_key?(child.name)
-
- @childrenHash[child.name] = child
+
+ @childrenHash[child.widget_id] = child
@children << child
child.parent = self
- child.run_widget_hook(:after_add, child, self)
child
end
@@ -174,4 +163,4 @@ def path
path.reverse.join("/")
end
end
-end
+end
@@ -20,6 +20,7 @@ def widget(prefix, *args)
options = args.extract_options!
id = args.shift || prefix
+ return {:class => prefix, :id => id, :options => options}
constant_for(prefix).new(parent_controller, id, options).tap do |object|
yield object if block_given?
end
@@ -6,18 +6,18 @@ class EventHandlerTest < Test::Unit::TestCase
context "an abstract EventHandler" do
should "push nil to root's ordered page_updates when #call'ed" do
- @mum = mouse_mock('mum')
- @mum << @kid = mouse_mock('kid')
+ @mum = mouse
+ @mum << mouse_mock(:kid)
assert_equal 0, @mum.page_updates.size
- [@mum, @kid, @mum].each do |source|
+ [@mum, @mum[:kid], @mum].each do |source|
Apotomo::EventHandler.new.call(Apotomo::Event.new(:squeak, source))
end
# order matters:
assert_equal 3, @mum.page_updates.size
- assert_equal 0, @kid.page_updates.size
+ assert_equal 0, @mum[:kid].page_updates.size
assert_equal(nil, @mum.page_updates[0])
assert_equal(nil, @mum.page_updates[1])
assert_equal(nil, @mum.page_updates[2])
View
@@ -21,7 +21,7 @@ class EventTest < Test::Unit::TestCase
end
should "respond to #to_s" do
- @event = Apotomo::Event.new(:footsteps, mouse_mock('mum'))
+ @event = Apotomo::Event.new(:footsteps, mouse('mum'))
assert_equal "<Event :footsteps source=mum>", @event.to_s
end
end
@@ -5,8 +5,9 @@ class OnfireIntegrationTest < Test::Unit::TestCase
context "including Onfire into the StatefulWidget it" do
setup do
- @mum = mouse_mock('mum')
- @mum << @kid = mouse_mock('kid')
+ @mum = mouse('mum')
+ @mum << mouse_mock(:kid)
+ @kid = @mum[:kid]
end
should "respond to #root" do
@@ -3,63 +3,57 @@
class RailsIntegrationTest < ActionController::TestCase
include Apotomo::TestCaseMethods::TestController
- def simulate_request!
- @controller.instance_eval { @apotomo_request_processor = nil }
+ class MumWidget < MouseWidget
+ responds_to_event :squeak
+ responds_to_event :sniff
+
+ def eat
+ render
+ end
+
+ def make_me_squeak
+ render
+ end
+
+ def squeak(evt)
+ render :text => evt.data
+ end
+
+ def sniff(evt)
+ render :text => "<b>sniff sniff</b>"
+ end
end
+
context "ActionController" do
setup do
- @mum = mum = MouseWidget.new(parent_controller, 'mum', :eating)
- @mum.instance_eval do
- def eating; render; end
- end
-
- @mum.respond_to_event :squeak
-
@controller.class.has_widgets do |root|
- root << mum
+ MumWidget.new(root, 'mum')
end
@controller.instance_eval do
def mum
- render :text => render_widget('mum', :eat)
+ render :text => render_widget('mum', params[:state])
end
end
end
should "provide the rails view helpers in state views" do
- @mum.instance_eval do
- def eat; render :view => :make_me_squeak; end
- end
-
- get 'mum'
+ get 'mum', :state => :make_me_squeak
assert_select "a", "mum"
end
should "pass the event with all params data as state-args" do
- @mum.instance_eval do
- def squeak(evt); render :text => evt.data; end
- end
-
get 'render_event_response', :source => 'mum', :type => :squeak, :pitch => :high
assert_equal "{\"source\"=>\"mum\", \"type\"=>\"squeak\", \"pitch\"=>\"high\", \"controller\"=>\"barn\", \"action\"=>\"render_event_response\"}", @response.body
end
should "render updates to the parent window for an iframe request" do
- @mum.instance_eval do
- def squeak(evt); render :text => "<b>SQUEAK!</b>"; end
- end
-
- get 'mum'
- assert_response :success
-
- simulate_request!
-
- get 'render_event_response', :source => 'mum', :type => :squeak, :apotomo_iframe => true
+ get 'render_event_response', :source => 'mum', :type => :sniff, :apotomo_iframe => true
assert_response :success
assert_equal 'text/html', @response.content_type
- assert_equal "<html><body><script type='text/javascript' charset='utf-8'>\nvar loc = document.location;\nwith(window.parent) { setTimeout(function() { window.eval('<b>SQUEAK!<\\/b>'); window.loc && loc.replace('about:blank'); }, 1) }\n</script></body></html>", @response.body
+ assert_equal "<html><body><script type='text/javascript' charset='utf-8'>\nvar loc = document.location;\nwith(window.parent) { setTimeout(function() { window.eval('<b>sniff sniff<\\/b>'); window.loc && loc.replace('about:blank'); }, 1) }\n</script></body></html>", @response.body
end
@@ -66,14 +66,16 @@ def mouse_mock(id='mum', opts={}, &block)
end
should "respond to #render_widget" do
- mum = mouse_mock
- mum << mouse_mock('kid')
+ mum = mouse
+ MouseWidget.new(mum, :kid)
+
assert_equal("<div id=\"kid\">burp!</div>\n", in_view(mum){ render_widget 'kid', :eat })
end
should "respond to #children" do
- mum = mouse_mock
- mum << mouse_mock('kid')
+ mum = mouse
+ MouseWidget.new(mum, :kid)
+
assert_equal("<div id=\"kid\">burp!</div>\n", in_view(mum) do
children.inject("") { |html, child| html += render_widget(child, :eat) }.html_safe
end)
View
@@ -5,7 +5,7 @@ class RenderTest < ActionView::TestCase
context "#render" do
setup do
- @mum = mouse_mock('mum', :eating)
+ @mum = mouse('mum')
end
should "per default display the state content framed in a div" do
@@ -31,7 +31,7 @@ class RenderTest < ActionView::TestCase
end
should "expose its instance variables in the rendered view" do
- @mum = mouse_mock('mum', :educate) do
+ @mum = mouse('mum') do
def educate
@who = "the cat"
@what = "run away"
@@ -72,8 +72,9 @@ def flight; render :text => "away from here!"; end
def squeak; render :text => "squeak!"; end
end
- procs = [Proc.new{ |root,controller|
- root << widget(:mouse, 'mum') << KidWidget.new(parent_controller, 'kid', :squeak)
+ procs = [Proc.new{ |root|
+ root << widget(:mouse, 'mum')
+ KidWidget.new(root['mum'], 'kid')
}]
@processor = Apotomo::RequestProcessor.new(parent_controller, {:js_framework => :prototype}, procs)
@@ -156,7 +157,7 @@ def kid; @kid end
@r = @class.new(parent_controller, {},
[Proc.new { |root| root << widget(:mouse, :mum) }])
- assert_equal @kid, @r.root[:mum][:kid]
+ assert @r.root[:mum][:kid]
end
end
@@ -171,7 +172,7 @@ def kid; @kid end
[Proc.new { |root| root << widget(:mouse, :mum) }])
@r.process_for(:source => "root", :type => :noop) # calls ~after_fire.
- assert_equal @r.root[:mum][:kid], @kid
+ assert @r.root[:mum][:kid]
end
end
end
View
@@ -1,10 +1,22 @@
module Apotomo
module TestCaseMethods
+ def root_mock
+ MouseWidget.new(parent_controller, :root)
+ end
+
+ def mouse(id=nil, &block)
+ MouseWidget.new(parent_controller, id || :mouse).tap do |widget|
+ widget.instance_eval &block if block_given?
+ end
+ end
+
+
# Provides a ready-to-use mouse widget instance.
def mouse_mock(id='mouse', opts={}, &block)
- mouse = MouseWidget.new(parent_controller, id, opts)
- mouse.instance_eval &block if block_given?
- mouse
+ #mouse = MouseWidget.new(parent_controller, id, opts)
+ #mouse.instance_eval &block if block_given?
+ widget(:mouse, id, opts)
+ #mouse
end
def mouse_class_mock(&block)
@@ -14,8 +26,9 @@ def mouse_class_mock(&block)
end
def mum_and_kid!
- @mum = mouse_mock('mum')
- @mum << @kid = mouse_mock('kid')
+ @mum = mouse('mum')
+ @kid = MouseWidget.new(@mum, 'kid')
+
@mum.respond_to_event :squeak, :with => :answer_squeak
@mum.respond_to_event :squeak, :from => 'kid', :with => :alert

0 comments on commit 4cb2d02

Please sign in to comment.