Created
October 1, 2015 10:40
-
-
Save johnlane/603aa9df77834d804e5c to your computer and use it in GitHub Desktop.
Widget wrapper around cell
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# app/concepts/widget/cell.rb | |
# A very basic "Widget" that is a container of Cell objects | |
# When rendered, it recursively renders all cells that it | |
# contains. | |
class Widget < Cell::Concept | |
attr_reader :parent | |
attr_reader :children | |
# Create a new widget | |
# All widgets are created as orphans | |
def initialize(*args) | |
super | |
@parent = nil | |
@children = [] | |
end | |
# Accept an orphan widget as a child | |
def <<(w) | |
if w.parent.nil? | |
@children << w | |
w.parent = self | |
else | |
raise Widget::HasParent | |
end | |
end | |
def show(options={}) | |
out = '' | |
@children.each { |c| out << c.show(options) } | |
out.html_safe | |
end | |
protected | |
attr_writer :parent | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I use this to build dynamic messaging object (flash on steroids) that is accessible from anywhere. Any method can write a message and it will be rendered in the layout.
They can do this:
which makes use of the below, where
@messages
is a widget cell.