0.0
No commit activity in last 3 years
No release in over 3 years
Simple Presenter for Rails App
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.9
~> 0.10.0
~> 10.0
~> 3.4.0
 Project Readme

Build Status Code Climate

UPresenter

Simple Presenter for your Rails App.

Why not use other gems?

All gems is good, but we just need to have a simple presenter layer without a lot of magic. UPresenter - solved our problem.

Installation

Add this line to your application's Gemfile:

gem 'u_presenter'

And then execute:

$ bundle

Or install it yourself as:

$ gem install u_presenter

Usage

Let's create your first presenter for Article model.

$ mkdir app/presenters/article_presenter.rb

And put this in your class:

class ArticlePresenter < UPresenter::Base
  def id_humanize
    "Article id: #{object.id}"
  end
end

Now let's use this presenter:

@article = ArticlePresenter.present(Article.last)

@article.id_humanize # your presenter method works fine
@article.created_at # and method from Model also works fine

What about collection?

To present collection you can do this:

@articles = ArticlePresenter.present_collection(current_user.articles)
@articles.presented.first.id_humanize # just use presented collection to show record

Why we use different method in this case? Just for simplicity. Because now, if you use Kaminari pagination you can easy get access to all methods, without extra delegation setup:

@articles.presented.each do |article|
  article.id_humanize
end

@articles.current_page

Helpers in Presenter

To use helper methods inside Presenter you just need to add view_context:

  @article = ArticlePresenter.present_collection(article, view_context)

Contributing

  1. Fork it ( https://github.com/[my-github-username]/u_presenter/fork )
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create a new Pull Request