Note: while we're actively using boring
in production, it is still actively under development, and you should expect breaking changes.
Below is an example of usage for a classic Rails controller/view pattern.
# presenters/user_presenter.rb
class UserPresenter < Boring::Presenter
# Declare the arguments needed to bind to presenter and their type
arguments user: User
# Declare pass-through methods
delegate :birth_date, to: :user
# Methods to be handled by the presenter
def name
"#{user.first_name} #{user.last_name}".strip
end
end
# controllers/users_controller.rb
class UsersController < ApplicationController
def index
@users = User.all
@user_presenter = UsersPresenter.new
end
end
# views/users/index.html.erb
<ul>
<% @users.each do |user| %>
<% @user_presenter.bind(user: user) %>
<li>
<p>Full Name: <%= @user_presenter.name %></p>
<p>Birthday: <%= @user_presenter.birth_date %></p>
</li>
<% end %>
</ul>
Some things worth noting that set boring
apart from other presentation layers:
- Explicit Delegation: only methods intended for presentation layer should be allowed in the presenter.
boring
will never passsuper_dangerous_method!
through to your bound object unless you want it to. - Type-Safe Bindings: the
arguments
method in theBoring::Presenter
class lets you set up type checking for the arguments passed to thebind
method. If you try to bind aFoo
to yourBarPresenter
, we'll raise an exception. - Separate Objects: The presenter doesn't take over for your bound object; whether that bound object is available to your view is up to you, but you should never be unsure if you're dealing with a
Foo
or aFooPresenter
.
After checking out the repo, run bin/setup
to install dependencies. Then, run rake spec
to run the tests. You can also run bin/console
for an interactive prompt that will allow you to experiment.
To install this gem onto your local machine, run bundle exec rake install
. To release a new version, update the version number in version.rb
, and then run bundle exec rake release
, which will create a git tag for the version, push git commits and tags, and push the .gem
file to rubygems.org.
Bug reports and pull requests are welcome on GitHub at https://github.com/wkirby/boring.
The gem is available as open source under the terms of the MIT License.
boring
was built by Apsis Labs. We love sharing what we build! Check out our other libraries on Github, and if you like our work you can hire us to build your vision.