Code Monkey home page Code Monkey logo

actionpresenter's Introduction

ActionPresenter

Gem Version Code Climate

A lightweight presenter implementation for Ruby on Rails.

Requirements

  • Ruby on Rails >= 4

Installation

  1. Add this to your Gemfile:
gem 'actionpresenter', '0.2.0'
  1. Create the app/presenters folder in your project which will be holding your presenter files.

Usage

Lets say we have a model named User.

class User < ActiveRecord::Base
  # Model with columns first_name and last_name
  # ...
  def name
    "#{first_name} #{last_name}".strip
  end
end
Creating a Presenter

In our views we always want to display the name of a specific user with a link to his profile. The simplest way of providing similar behavior to all our views is to create a UserPresenter in app/presenters/user_presenter.rb.

class UserPresenter < ActionPresenter::Base
  presents :user

  def name(linked = true)
    h.link_to_if linked, super(), h.user_profile_path(user)
  end
end

We can use super as alternative to user.name or object.name in this case to get the value from the original User instance.

All helpers from the view context are available through the template accessor (or its short variant h).

You can also make all view helpers available to the presenter as if they were mixed into the presenter by including ActionPresenter::TemplateDelegation:

class UserPresenter < ActionPresenter::Base
  include ActionPresenter::TemplateDelegation

  presents :user

  def name(linked = true)
    link_to_if linked, super(), user_profile_path(user)
  end
end
Using Presenters in a View

A presenter can be embedded into a view by using the present helper.

<% present @user do |user| %>
  <strong>Name:</strong>
  <br>
  <%= user.name %>
<% end %>

You can also reference to any custom presenter:

<% present @user, with: Admin::UserPresenter do |user| %>
  ...
<% end %>

Alternatively, you can use the following syntax:

<% present [:admin, @user] do |user| %>
  ...
<% end %>

If you have a collection of objects you can rely on present_collection which will wrap each element of the collection with a presenter of its own:

<% present_collection(@users).each do |user| %>
  ...
<% end %>

The following snippet will search for matching presenter classes within the Admin module:

<% present_collection(@users, scope: :admin).each do |user| %>
  ...
<% end %>
Keeping it DRY

To share common functionality between different presenters we encourage you to use presenter concerns. Latter should be put into app/presenters/concerns. Note: As this folder is not recognized by the default Rails setup, it has to be added manually to config/application.rb:

config.autoload_paths << Rails.root.join('app', 'presenters', 'concerns').to_s

Contributing to ActionPresenter

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet.
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it.
  • Fork the project.
  • Start a feature/bugfix branch.
  • Commit and push until you are happy with your contribution.
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Credits

Many thanks to Ryan Bates whose Railscasts episode Presenters from Scratch helped building the API for this Gem.

Copyright

Copyright (c) 2015 Tobias Casper. See LICENSE.txt for further details.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.