# \[ANN\] widgetz.rb

**URL:** <https://www.ruby-forum.com/t/ann-widgetz-rb/109717>\
**Category:** Rails\
**Created:** [July 18, 2007, 9:41am UTC](https://www.ruby-forum.com/t/ann-widgetz-rb/109717 "2007-07-18T09:41:55Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [July 18, 2007, 9:41am UTC](https://www.ruby-forum.com/t/ann-widgetz-rb/109717/1 "2007-07-18T09:41:55Z")

</div>

NAME

widgetz.rb

SYNOPSIS

simple widgets for rails

INSTALL

gem install widgetz

URIS  
[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
[http://codeforpeople.com/lib/ruby/widgetz/](http://codeforpeople.com/lib/ruby/widgetz/)  
[http://drawohara.tumblr.com/post/6060120](http://drawohara.tumblr.com/post/6060120)

DESCRIPTION

widgetz.rb aims to fix two problems with factoring and abstracting  
html  
generation in rails using the normal ‘helper’ methodolgy:

```
 1) helper methods are purely procedural and provide no state.

```

ruby is an  
object oriented language and we like to use objects to abstract  
things  
where possible. widgetz lets you do just that. some people  
call this  
encapsulation.

```
 2) by using the normal rendering chain in rails, widgetz makes

```

sure you  
get a nice stack stace from the location in the widgetz template  
where  
you used ruby’s power blow your leg off instead of some esoteric  
message  
from an anonymous module you barely knew.

widgetz are essentailly bags of data that use a controller to render  
themselves. all the state of the widget is made available in the  
view as  
local variables, while the normal @variables set in a controller  
remain  
visible as normal. one special local variable, ‘widget’, is set  
in the view’s  
local vars so you have a handle on the widget in order to use it  
for some evil  
purpose.

SAMPLE

# 

# app/controllers/sample\_controller.rb

# 

```
 def c
   @var = 42
   @c = widget 'c'

   render :layout => 'application', :inline => <<-rhtml
     var: <%= @var %>

     <hr>

     <%= @c %>
   rhtml
 end

```

# 

# lib/widgets/c.rb

# 

```
 Widget 'c' do
   attribute 'var' => 'forty-two'

   def answer
     42.0
   end
 end

```

# 

# app/views/widgets/c.rhtml

# 

```
 controller var : <%= @var %> <br>
 widget var : <%= var %> <br>
 widget method : <%= widget.answer %> <br>

```

# 

# output

# 

```
 var: 42
 ----------------------
 controller var : 42
 widget var : forty-two
 widget method : 42.0

```

DOCS

lib/\*  
sample/rails/\*

AUTHOR

a @ [http://drawohara.com/](http://drawohara.com/)

enjoy.

## -a

we can deny everything, except that we have the possibility of being  
better. simply reflect on that.  
h.h. the 14th dalai lama

---

<div class="post-metadata">

**Author:** ![ara.t.howard](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/a/a8b319/32.png) [@ara.t.howard](https://www.ruby-forum.com/u/ara.t.howard)\
**Post date:** [July 18, 2007, 9:52am UTC](https://www.ruby-forum.com/t/ann-widgetz-rb/109717/2 "2007-07-18T09:52:56Z")

</div>

(sorry for formatting - fixed here)

NAME

widgetz.rb

SYNOPSIS

simple widgets for rails

INSTALL

gem install widgetz

URIS  
[http://rubyforge.org/projects/codeforpeople/](http://rubyforge.org/projects/codeforpeople/)  
[http://codeforpeople.com/lib/ruby/widgetz/](http://codeforpeople.com/lib/ruby/widgetz/)  
[http://drawohara.tumblr.com/post/6060120](http://drawohara.tumblr.com/post/6060120)

DESCRIPTION

widgetz.rb aims to fix two problems with factoring and abstracting  
html generation in rails using the normal ‘helper’ methodolgy:

```
1) helper methods are purely procedural and provide no state.

```

ruby is an object oriented language and we like to use objects to  
abstract things where possible. widgetz lets you do just that. some  
people call this encapsulation.

```
2) by using the normal rendering chain in rails, widgetz makes

```

sure you get a nice stack stace from the location in the widgetz  
template where you used ruby’s power blow your leg off instead of some  
esoteric message from an anonymous module you barely knew.

widgetz are essentailly bags of data that use a controller to render  
themselves. all the state of the widget is made available in the view  
as local variables, while the normal @variables set in a controller  
remain visible as normal. one special local variable, ‘widget’, is  
set in the view’s local vars so you have a handle on the widget in  
order to use it for some evil purpose.

SAMPLE

# 

# app/controllers/sample\_controller.rb

# 

```
def c
  @var = 42
  @c = widget 'c'

  render :layout => 'application', :inline => <<-rhtml
    var: <%= @var %>

    <hr>

    <%= @c %>
  rhtml
end

```

# 

# lib/widgets/c.rb

# 

```
Widget 'c' do
  attribute 'var' => 'forty-two'

  def answer
    42.0
  end
end

```

# 

# app/views/widgets/c.rhtml

# 

```
controller var : <%= @var %> <br>
widget var : <%= var %> <br>
widget method : <%= widget.answer %> <br>

```

# 

# output

# 

```
var: 42
----------------------
controller var : 42
widget var : forty-two
widget method : 42.0

```

DOCS

lib/\*  
sample/rails/\*

AUTHOR

a @ [http://drawohara.com/](http://drawohara.com/)
