Memphis Ruby

Memphis Ruby

Fork me on GitHub

Memphis Ruby Posts Tagged: decorators

Josh W. Lewis — January 28, 2015

This month, we had a great meetup at Coroutine’s new office space on Broad Ave.

Jason Charnes educated us on how to decorate your models as an alternative to Rails' helper system. A tradidtional Rails view helper might look like this:

module ChurchesHelper
  def remove_church_from_name(name)
    name.split(" ").reject{ |e| e == "Church" }.join(" ")
  end
end

And you’d use it like this:

  <h5><%= remove_church_from_name(c.name) %></h5>

This works, and helps keep your code DRY, though you usually end up with long method names, and several concerns mixed into a module.

Draper is a RubyGem for adding decorating methods to models. With Draper, you can do something like this:

class ChurchDecorator < Draper::Decorator
  def name
    model.name.split(" ").reject{ |e| e == "Church" }.join(" ")
  end
end

Which you can use in your views like this:

  <h5><%= @church.name %></h5>

It certainly feels more Object Oriented, and in my book, it’s a little clearer. Jason showed us how to get setup with Draper, and explained some of the deeper semantics. Check out Jason’s slides here.

We concluded the event with an inpromptu editor showdown. Various developers showcased their editors, with varying degrees of success (I hope I didn’t turn anyone off on Vim). We saw Vim, Atom, and PHPStorm, and talked about others like Sublime and TextMate.

Thanks to Coroutine for the meeting space and the awesome Broadway Pizza.

Tagged: ruby, gem, decorators