All Projects → neighborland → Pres

neighborland / Pres

Licence: mit
A Simple Rails Presenter

Programming Languages

ruby
36898 projects - #4 most used programming language

Labels

Projects that are alternatives of or similar to Pres

Muscle man
💪🏻It is a chatbot which can receive your practice record!
Stars: ✭ 25 (+66.67%)
Mutual labels:  rails
Invisible captcha
🍯 Unobtrusive and flexible spam protection for Rails apps
Stars: ✭ 851 (+5573.33%)
Mutual labels:  rails
Vue Rails Form Builder Demo
An example of Rails app using vue-form-for gem
Stars: ✭ 12 (-20%)
Mutual labels:  rails
Rails Erd D3
Ruby gem for creating entity–relationship diagram with D3.js for your Rails application 📊
Stars: ✭ 25 (+66.67%)
Mutual labels:  rails
Ridgepole
Ridgepole is a tool to manage DB schema. It defines DB schema using Rails DSL, and updates DB schema according to DSL. (like Chef/Puppet)
Stars: ✭ 840 (+5500%)
Mutual labels:  rails
Heavens door
Capybara test scenario recorder for Rails
Stars: ✭ 857 (+5613.33%)
Mutual labels:  rails
Activejob Scheduler
A background job scheduler for any queue backend
Stars: ✭ 24 (+60%)
Mutual labels:  rails
Ahoy email
First-party email analytics for Rails
Stars: ✭ 876 (+5740%)
Mutual labels:  rails
Serve
Serve is a small Rack-based web server and rapid prototyping framework for Web applications (specifically Rails apps). Serve is meant to be a lightweight version of the Views part of the Rails MVC. This makes Serve an ideal framework for prototyping Rails applications or creating simple websites. Serve has full support for Rails-style partials and layouts.
Stars: ✭ 844 (+5526.67%)
Mutual labels:  rails
Simple Navigation
A ruby gem for creating navigations (with multiple levels) for your Rails, Sinatra or Padrino applications. Render your navigation as html list, link list or breadcrumbs.
Stars: ✭ 868 (+5686.67%)
Mutual labels:  rails
Auto Matching
Integrated Deai Engine(IDE: 統合出会い系エンジン)
Stars: ✭ 26 (+73.33%)
Mutual labels:  rails
Adminpanel
This gem has the goal to act as the administration panel for your resources, so you can focus only on the public part of your app
Stars: ✭ 7 (-53.33%)
Mutual labels:  rails
Docker Rails
Dockerize Rails 6 with ActionCable, Webpacker, Stimulus, Elasticsearch, Sidekiq
Stars: ✭ 856 (+5606.67%)
Mutual labels:  rails
Dotenv sekrets
Seamlessly encrypt/decrypt/edit your rails Dotenv files with the help of the Sekrets gem
Stars: ✭ 25 (+66.67%)
Mutual labels:  rails
Ruby Rails Mysql Template
This example shows how to use Anychart library with the Ruby on Rails and MySQL database.
Stars: ✭ 12 (-20%)
Mutual labels:  rails
Active decorator
ORM agnostic truly Object-Oriented view helper for Rails 4, 5, and 6
Stars: ✭ 928 (+6086.67%)
Mutual labels:  rails
Ui Bibz
Ui Frameworks based on Bootstrap and Ruby on Rails
Stars: ✭ 9 (-40%)
Mutual labels:  rails
Devise Jwt
JWT token authentication with devise and rails
Stars: ✭ 881 (+5773.33%)
Mutual labels:  rails
Livingstyleguide
Easily create front-end style guides with Markdown and Sass/SCSS.
Stars: ✭ 874 (+5726.67%)
Mutual labels:  rails
Active record doctor
Identify database issues before they hit production.
Stars: ✭ 865 (+5666.67%)
Mutual labels:  rails

pres

Gem Version Build Status

What?

A Presenter is a rendering class. The pres gem is a lightweight presenter solution with no runtime gem dependencies.

Pres provides the following:

  1. Pres::Presenter is a presenter base class.
  2. present is a convenience method to create presenters.
  3. Pres::ViewDelegation is a delegation module, included in the Presenter base class.

How and Why?

Presenters are an alternative to an unorganized pile of helper methods in your rails application.

Rails' ViewContext contains convenience methods for views, such as link_to, url_for, truncate, number_to_currency, etc. It's the thing that makes Rails views nice to work with.

Other presenter libraries mix in all the methods from the Rails ViewContext to make it easy to call those methods in the Presenter class. pres instead injects the ViewContext as a dependency into the Presenter class, and uses method_missing to delegate to ViewContext methods. pres produces small classes that contain and delegate to an existing object that handles server-side rendering.

Install

Add it to your Gemfile:

gem "pres"

Setup with Rails

Include the Pres::Presents module:

class ApplicationHelper
  include Pres::Presents
end

This will make the present method available in your views.

Use

There are two main approaches:

(1) Follow the traditional rails way with view templates, but move your helper methods into a presenter class. You'll probably want to start here if you have an existing rails app.

(2) Create self-contained rendering components (see "Components" below).

You can use both techniques.

(1) With View Templates

The quickest way to get started is to use the Pres::Presenter base class.

Create a presenter class in app/presenters:

class DogePresenter < Pres::Presenter
  # explicitly delegate methods to the model
  delegate :name, to: :object

  def know_your_meme_link
    # Rails helpers are available via the view context
    link_to "Know your meme", "http://knowyourmeme.com/memes/doge"
  end

  def name_header
    # object is the Doge used to initialize the presenter
    content_tag(:h1, object.name)  
  end

  def signed_in_status
    # controller methods are accessible via the view context
    if signed_in?
      "Signed in"
    else
      "Signed out"
    end
  end  
end

Standard rails controller method:

class DogesController
  def show
    @doge = Doge.find(params[:id])
  end
end

Wrap your model object in a presenter in your view with present:

doges/show.haml.html

- present(@doge) do |doge|
  = doge.name_header
  .status
    You are #{doge.signed_in_status}
  .links
    .meme-link= doge.know_your_meme_link

Collections

Standard rails controller method:

class DogesController
  def index
    @doges = present(Doge.all)
  end
end

Build an array of presenters in your view with present:

doges/index.haml.html

This renders "doges/_doge.html.haml" for each item, following rails' usual conventions:

= render present(@doges)

Or use each:

- present(@doges).each do |doge|
  = doge.name_header

(2) Components

You can use pres to build components that directly render HTML:

Note: #sanitize is a method on the view_context.

class NameHeader < Pres::Presenter
  def render
    return unless object&.name
    <<~HTML.html_safe
      <h1>#{sanitize(object.name.titleize)}</h1>
    HTML
  end
end

user = User.new(name: "joe cool <")

NameHeader.new(user, view_context).render 
=> "<h1>Joe Cool &lt;</h1>"

present(user, presenter: NameHeader).render
=> "<h1>Joe Cool &lt;</h1>"

You may notice that you could do without pres altogether when you don't need the view_context helper methods:

class PlusTwo
  def initialize(object)
    @object = object
  end

  def render
    return unless @object
    <<~HTML.html_safe
      <p>#{@object + 2}</p>
    HTML
  end
end

PlusTwo.new(2).render 
=> "<p>4</p>"

present(2, presenter: PlusTwo).render
=> "<p>4</p>"

If render is confusing, name that method #to_html or something else.

Options

Present with options

Pass additional options to a Presenter as a hash. The presenter class exposes the options hash as a method:

user = User.new

# These two lines are the same:
# 1. explicit
presenter = UserPresenter.new(user, view_context, something: 123)

# 2. using #present
presenter = present(user, something: 123)
=> #<UserPresenter object: #<User> ...>

presenter.options[:something]
=> 123

Use a custom presenter class

By default, a presenter class corresponding to the model class name is constructed in present. For example, if you present a User, a UserPresenter class is constructed. An error is raised if the presenter class does not exist. To specify a different class, use the presenter: key.

user = User.new
present(user, presenter: UserEditPresenter, cool: true)
=> #<UserEditPresenter object: #<User> ...>

You may also define a custom presenter class on any class you want to present:

class User
  def presenter_class
    MyPresenter
  end
end

present(User.new)
# => #<MyPresenter object: #<User> ...>

Presenters can create other presenters

Presenters can wrap child objects in presenters of their own.

class DogePresenter < Pres::Presenter
  def cats
    present(object.cats)
  end  
end
= render doge.cats

Using mixins instead of inheritance

If you don't want to inherit from Pres::Presenter, you can include Pres::ViewDelegation and implement your own initializer (so the present helper will work).

This technique is useful if you would like to delegate all methods in a model by default, instead of whitelisting methods on the wrapped model explicitly. Delegating everything to the model by default is how the draper gem works, for example.

class DogePresenter < DelegateClass(Doge)
  include Pres::ViewDelegation

  def initialize(object, view_context, options = {})
    super(object)
    @view_context = view_context
  end
= doge.name

see DelegateClass

Updating to version 1.0

Modules and classes have been moved into the Pres namespace with version 1.0. Change your code references to Pres::Presents and Pres::Presenter.

References

Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].