gem install simple_presenter
class User < ActiveRecord::Base # implements the following attributes: name, email, password_hash, password_salt end class UserPresenter < Presenter expose :name, :email end user = UserPresenter.new(User.first) users = UserPresenter.map(User.all)
If you're using Simple Presenter within Rails, presenters also have access to:
route helpers: just use the routes or r methods
view helpers: just use the helpers or h methods
I18n methods: just use the translate, t, localize or l methods
For additional usage, check the specs.
Recognize ActiveRecord objects and automatically expose attributes used by url and form helpers (like Model.model_name, Model#to_key, and Model#to_param).
Override respond_to? to reflect exposed attributes.
If you're having problems because already have a class/module called Presenter that is conflicting with this gem, you can require the namespace and inherit from SimplePresenter::Base.
require "simple_presenter/namespace" class UserPresenter < SimplePresenter::Base end
If you're using Rails/Bundler or something like that, remember to override the :require option.
# Gemfile source :rubygems gem "simple_presenter", :require => "simple_presenter/namespace"
Nando Vieira (nandovieira.com.br)
(The MIT License)
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the 'Software'), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.