class Apotomo::Widget

Accessing Parameters

Apotomo tries to prevent you from having to access the global params hash. We have the following concepts to retrieve input data.

  1. Configuration values are available both in render and triggered states. Pass those in widget

when creating the widget tree. Use options for reading.

has_widgets do |root|
  root << widget(:mouse_widget, 'mum', :favorites => ["Gouda", "Chedar"])

and read in your widget state

def display
  @cheese = options[:favorites].first
  1. Request data from forms etc. is available through event.data in the triggered states.

Use the #[] shortcut to access values directly.

def update(evt)
  @cheese = Cheese.find evt[:cheese_id]

Attributes

name[R]
options[R]
visible[W]
widget_id[R]

Public Class Methods

controller_path() click to toggle source
# File lib/apotomo/widget.rb, line 144
def self.controller_path
  @controller_path ||= name.sub(/Widget$/, '').underscore unless anonymous?
end
new(parent, id, options={}) click to toggle source
Calls superclass method
# File lib/apotomo/widget.rb, line 81
def initialize(parent, id, options={})
  super(parent)  # TODO: do that as long as cells do need a parent_controller.
  @options      = options
  @name         = id
  @visible      = true

  setup_tree_node(parent)

  run_hook :after_initialize, self
end

Public Instance Methods

address_for_event(type, options={}) click to toggle source
# File lib/apotomo/widget.rb, line 133
def address_for_event(type, options={})
  options.reverse_merge!  :source     => name,
                          :type       => type,
                          :controller => parent_controller.controller_path  # DISCUSS: dependency to parent_controller.
end
find_widget(widget_id) click to toggle source

Returns the widget named widget_id if it’s a descendent or self.

# File lib/apotomo/widget.rb, line 129
def find_widget(widget_id)
  find {|node| node.name.to_s == widget_id.to_s}
end
invoke(state, *args) click to toggle source

Invokes state and hopefully returns the rendered content.

# File lib/apotomo/widget.rb, line 102
def invoke(state, *args)
  return render_state(state, *args) if method(state).arity != 0 # TODO: remove check and make trigger states receive the evt default.
  render_state(state)
end
parent_controller() click to toggle source
# File lib/apotomo/widget.rb, line 92
def parent_controller
  # i hope we'll get rid of any parent_controller dependency, soon.
  root? ? @parent_controller : root.parent_controller
end
render(*args, &block) click to toggle source

Renders and returns a view for the current state. That’s why it is usually called at the end of a state method.

Options

Example:

class MouseWidget < Apotomo::Widget
  def eat
    render
  end

render the view eat.haml.

render :text => "alert('SQUEAK!');"

issues a squeaking alert dialog on the page.

Calls superclass method
# File lib/apotomo/widget.rb, line 124
def render(*args, &block)
  super
end
render_widget(widget_id, state=:display, *args) click to toggle source

Renders the widget (instance or id).

# File lib/apotomo/widget.rb, line 149
def render_widget(widget_id, state=:display, *args)
  if widget_id.kind_of?(Widget)
    widget = widget_id
  else
    widget = find_widget(widget_id) or raise "Couldn't render non-existent widget `#{widget_id}`"
  end

  widget.invoke(state, *args)
end
url_for_event(type, options={}) click to toggle source
# File lib/apotomo/widget.rb, line 139
def url_for_event(type, options={})
  apotomo_event_path address_for_event(type, options)
end
visible?() click to toggle source
# File lib/apotomo/widget.rb, line 97
def visible?
  @visible
end