Project

bourgeois

0.01
No commit activity in last 3 years
No release in over 3 years
Bourgeois is a Ruby library that makes using presenters a very simple thing.
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
 Dependencies

Development

~> 1.3
>= 0
~> 2.13

Runtime

>= 3.0.0
>= 3.0.0
 Project Readme

Bourgeois
Bourgeois is a Ruby library that makes using presenters a very simple thing.


Installation

Add this line to your application’s Gemfile:

gem 'bourgeois'

And then execute:

$ bundle

Usage

Create an app/presenters directory and put some presenters in it:

# app/presenters/user_presenter.rb

class UserPresenter < Bourgeois::Presenter
  def formatted_name
    "#{first_name} #{last_name}".strip
  end
end

Then, you can use the present helper in your views to wrap ActiveModel (and ActiveRecord) objects around a presenter:

<% present User.first do |user| %>
  <p>This is <%= user.formatted_name %></p>
<% end %>

Methods that aren’t in the presenter (first_name and last_name) are delegated to the presented object. You can also use the view method in the presenter to get the original view it was called in:

# app/presenters/user_presenter.rb

class UserPresenter < Bourgeois::Presenter
  def birthdate
    # To get the original `birthdate` value, you can either use `super` or `object.birthdate`
    super.presence || view.content_tag(:em, 'Unknown')
  end
end

Custom block helpers

You can use the simple helper DSL to define block helpers that will be executed if certain conditions are matched.

class UserPresenter < Bourgeois::Presenter
  helper :with_profile, if: -> { profile.present? && profile.public? }
end

User.first.profile = Profile.create(public: true, title: 'Foo', description: 'Bar')
<% present User.first do |user| %>
  <h1><%= user.full_name %></h1>
  <% user.with_profile do %>
    <div class="profile">
      <h2><%= user.profile.title %></h2>
      <%= simple_format(user.profile.description) %>
    </div>
  <% end %>
<% end %>

Inspiration

Bourgeois was inspired by some code @rafBM wrote for his OpenCode talk on May 28th, 2013.

License

Bourgeois is © 2013-2015 Mirego and may be freely distributed under the New BSD license. See the LICENSE.md file.

About Mirego

Mirego is a team of passionate people who believe that work is a place where you can innovate and have fun. We're a team of talented people who imagine and build beautiful Web and mobile applications. We come together to share ideas and change the world.

We also love open-source software and we try to give back to the community as much as we can.