-
Notifications
You must be signed in to change notification settings - Fork 4
Preconditions
Brad Fults edited this page Oct 18, 2011
·
1 revision
Boxer gives you a way to short-circuit box rendering via the precondition
block within a box. This block is always executed before any views of the box
and is meant to provide an alternate rendering of the box, e.g. in case a
primary object is nil.
Boxer.box(:user) do |box, user|
box.precondition {|resp| resp.emit({}) if user.nil? }
box.view(:base) { {:name => user.name} }
end
In this example, rendering the :base
view would fail when user
is nil
,
so the precondition preemptively returns {}
via the emit
method. As soon
as the emit
method is called, rendering of the current box is abandoned and
the passed value is returned from ship
.
>> Boxer.ship(:user, nil)
=> {}
>> Boxer.ship(:user, User.first)
=> {:name => "Bob Jones"}