No commit activity in last 3 years
No release in over 3 years
A simple and Rubyish view helper for Rails
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
 Dependencies
 Project Readme

LulalalaPresenter - forked from ActiveDecorator

...where you can put your view helpers if they are very closely related to ActiveModel.

This solves one very important computer science problem: naming your helper methods. All great programmers feels uneasy seeing:

  • auction_title(@auction)
  • board_title(@board)
  • category_title(@category)

and this gem will solve it.

Usage

  1. put gem 'lulalala_presenter' in Gemfile and then bundle install.
  2. create a presenter class for each AR model you wish to present. For example, a decorator for a model User should be named UserPresenter. In your helper methods:
  3. call any ActionView's helper methods using h, e.g. h.content_tag or h.link_to.
  4. call any model methods using model, e.g. model.title
  5. access this presenter from the model like this: record.presenter, and from there call the helper methods.

Examples

# app/models/user.rb
class User < ActiveRecord::Base
  # first_name:string last_name:string website:string
end

# app/presenters/user_presenter.rb
class UserPresenter < LulalalaPresenter::Base
  def full_name
    "#{model.first_name} #{model.last_name}"
  end

  def link
    h.link_to model.full_name, model.website
  end
end

# app/controllers/users_controller.rb
class UsersController < ApplicationController
  def index
    @users = User.all
  end
end
# app/views/users/index.html.erb
<% @users.each do |user| %>
  <%= user.presenter.link %><br>
<% end %>

Reason for fork

It's difficult to decorate models automatically. Leaky abstractions can happen easily. Having a presenter class that can be accessed from model would not have that issue at all. Even though more typing is needed, I think it is good for establishing the awareness that presenter helpers are different to model methods.

Contribute

  • Fork, fix, then send me a pull request.

Copyright

Copyright (c) 2011 Akira Matsuda. See MIT-LICENSE for further details.