Skip to content

Helper Methods in Boxes

Brad Fults edited this page Oct 18, 2011 · 1 revision

Boxes are really just anonymous classes, so you can define any methods you want inside of a box without fear of them leaking out into the current namespace.

For instance:

Boxer.box(:place) do |box, place|
  box.view(:base) do
    {
      :name       => place.name,
      :image_urls => place_images(place),
    }
  end

  def place_images(p)
    {
      :square_100 => p.image(:small),
      :square_200 => p.image(:large),
    }
  end
end

Defining methods like this can be helpful for sharing common bits across different views, or just encapsulating some shared attributes. As a matter of code organization, it's not recommended that any business logic take place in these methods, but that they call out to model methods with business logic instead.

Clone this wiki locally