Class: Apotomo::Widget

Inherits:
Cell::Base
  • Object
show all
Includes:
EventMethods, JavascriptMethods, WidgetShortcuts, Hooks, Onfire, TreeNode
Defined in:
lib/apotomo/widget.rb

Overview

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]

Constant Summary

DEFAULT_VIEW_PATHS =
[
  File.join('app', 'widgets'),
  File.join('app', 'widgets', 'layouts')
]

Instance Attribute Summary (collapse)

Attributes included from EventMethods

#page_updates

Attributes included from TreeNode

#childrenHash, #name, #parent

Class Method Summary (collapse)

Instance Method Summary (collapse)

Methods included from JavascriptMethods

#escape_js, #replace, #update

Methods included from WidgetShortcuts

#widget

Methods included from EventMethods

#add_class_event_handlers, #respond_to_event, #trigger

Methods included from TreeNode

#<<, #<=>, #[], #add, #children, #each, #find_by_path, included, #initialize_tree_node, #path, #printTree, #remove!, #remove_all!, #remove_from_parent!, #root, #root?, #size, #to_s

Constructor Details

- (Widget) initialize(parent_controller, id, options = {})

Returns a new instance of Widget



92
93
94
95
96
97
98
99
# File 'lib/apotomo/widget.rb', line 92

def initialize(parent_controller, id, options={})
  super(parent_controller, options)  # do that as long as cells do need a parent_controller.
  
  @name         = id
  @visible      = true
  
  run_hook :after_initialize, self
end

Instance Attribute Details

- (Object) visible=(value) (writeonly)

Sets the attribute visible

Parameters:

  • value

    the value to set the attribute visible to.



61
62
63
# File 'lib/apotomo/widget.rb', line 61

def visible=(value)
  @visible = value
end

Class Method Details

+ (Object) controller_path



160
161
162
# File 'lib/apotomo/widget.rb', line 160

def self.controller_path
  @controller_path ||= name.sub(/Widget$/, '').underscore unless anonymous?
end

Instance Method Details

- (Object) add_has_widgets_blocks



86
87
88
# File 'lib/apotomo/widget.rb', line 86

def add_has_widgets_blocks(*)
  run_widget_hook(:has_widgets, self)
end

- (Object) address_for_event(type, options = {})



149
150
151
152
153
# File 'lib/apotomo/widget.rb', line 149

def address_for_event(type, options={})
  options.reverse_merge!  :source     => name,
                          :type       => type,
                          :controller => parent_controller.controller_path  # DISCUSS: dependency to parent_controller.  
end

- (Object) find_widget(widget_id)

Returns the widget named widget_id as long as it is below self or self itself.



145
146
147
# File 'lib/apotomo/widget.rb', line 145

def find_widget(widget_id)
  find {|node| node.name.to_s == widget_id.to_s}
end

- (Object) invoke(state, *args)

Invokes state and hopefully returns the rendered content.



106
107
108
109
# File 'lib/apotomo/widget.rb', line 106

def invoke(state, *args)
  return render_state(state, *args) if state_accepts_args?(state)
  render_state(state)
end

- (Object) param(name)



137
138
139
140
141
# File 'lib/apotomo/widget.rb', line 137

def param(name)
  msg = "Deprecated. Use #options for widget constructor options or #params for request data."
  ActiveSupport::Deprecation.warn(msg)
  raise msg
end

- (Object) render(*args, &block) Also known as: emit

Render the view for the current state. Usually called at the end of a state method.

Options

  • :view - Specifies the name of the view file to render. Defaults to the current state name.

  • :template_format - Allows using a format different to :html.

  • :layout - If set to a valid filename inside your cell's view_paths, the current state view will be rendered inside the layout (as known from controller actions). Layouts should reside in app/cells/layouts.

  • see Cell::Base#render for additional options

Example:

class MouseWidget < Apotomo::StatefulWidget
  def eat
    # ... do something
    render 
  end

will just render the view eat.haml.

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

issues a squeaking alert dialog on the page.



131
132
133
# File 'lib/apotomo/widget.rb', line 131

def render(*args, &block)
  super
end

- (Object) render_widget(widget_id, state = :display, *args)

Renders the widget (instance or id).



165
166
167
168
169
170
171
172
173
# File 'lib/apotomo/widget.rb', line 165

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

- (Object) run_widget_hook(name, *args)

Runs callbacks for name hook in instance context.



82
83
84
# File 'lib/apotomo/widget.rb', line 82

def run_widget_hook(name, *args)
  self.class.callbacks_for_hook(name).each { |blk| instance_exec(*args, &blk) }
end

- (Object) url_for_event(type, options = {})



155
156
157
# File 'lib/apotomo/widget.rb', line 155

def url_for_event(type, options={})
  apotomo_event_path address_for_event(type, options) 
end

- (Boolean) visible?

Returns:

  • (Boolean)


101
102
103
# File 'lib/apotomo/widget.rb', line 101

def visible?
  @visible
end