Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

lokap-presenter

Package Overview
Dependencies
Maintainers
1
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

lokap-presenter

  • 0.0.2
  • Rubygems
  • Socket score

Version published
Maintainers
1
Created
Source

A simple Rails view presenter for those with discerning taste. From the Library of Knowledge and Power

Installation

Add this line to your application's Gemfile:

gem 'lokap-presenter'

Documentation

See https://rubydoc.info/github/adambair/lokap-presenter

Usage

Call the presenter within your view using the present helper:

present(object, :class_name).render('template')

Your Presenter will be searched for in:

app/presenters/<class_name>_presenter.rb

Templates will be searched for in:

app/views/presenters/<class_name>/_default.html.erb
app/views/presenters/<class_name>/_<template>.html.erb

And can be used like so:

present(@person, :emotion).render
# => app/presenters/emotion_presenter.rb
# => app/views/presenters/emotion/_default.html.erb

present(@person, :emotion).render(:happy)
# => app/presenters/emotion_presenter.rb
# => app/views/presenters/emotion/_happy.html.erb

present(@person, :emotion).render(:sad)
# => app/presenters/emotion_presenter.rb
# => app/views/presenters/emotion/_sad.html.erb

Access the presenter inside your presenter templates:

presenter.show_emotion
p.show_emotion

The Presenter Class

The class has full access to the view... and view helpers, etc. Think of this as a helper on steroids (but less angry)

class EmotionPresenter < Lokap::Presenter
  presents :person
  delegate :emotions, :moods, to: :human

  def who_is_this
    person
  end

  def show_emotion
    emotions.logic
  end

  def show_mood
    moods.logic
  end
end

Default Templates

If a specified template is not found, the presenter will attempt to render a default view.

present(@person, :share).render(:twitter)

# looks for /views/presenters/share/_twitter.html.erb (doesn't exist)
# => /views/presenters/share/_default.html.erb

This will also work for templates inside subfolders:

present(@person, :share).render('social/twitter')

# looks for /views/presenters/share/social/_twitter.html.erb (doesn't exist)
# => /views/presenters/share/social/_default.html.erb

Examples

Here are a few different ways to utilize presenters:

# Render (Default)
present(@person, :share).render
present(@person, :share).render(:twitter)
present(@person, :share).render('social/twitter')

# Block
present(@person, :share) do |p|
  link_to p.text, p.url
end

# Object
share = present(@person, :share)
share.render(:twitter)
share.render(:facebook)
share.render(:email)

FAQs

Package last updated on 16 Jul 2024

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc