Into The Wind

If you work with web applications written using the Ruby on Rails framework, you might find this useful. Everyone else can treat this post like the waste of time it is. (maybe not work-safe)

I decided that subclassing FormBuilder was my best solution to a problem for one of my work projects. Being the behavior-driven development masochist that I am, I wanted to develop my custom FormBuilder example-first. There is surprisingly little information about this floating around the interwebs, so I decided to throw out a first cut via code example. Simplifications welcome.


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
# build a ViewExampleGroup
describe CustomFormBuilder, :type => :view do
before do
# model name and model object
object_name = model
object = OpenStruct.new
builder = CustomFormBuilder.new(object_name, object, template, {}, nil)

# make the builder available to the view
assigns[:builder] = builder
end

it generates text fields do
# run the builder in an ERb template…
str = <%= @builder.text_field(:foo) %>
render :inline => str

# …and make sure you’re satisfied with the result
template.should have_tag(input[name=?], model[foo])
end

# …more examples…
end