Coder Social home page Coder Social logo

frosting's Introduction

Frosting

Let's simplify using presenters in Rails.

Installation

Add gem "frosting" to your Gemfile and run bundle install.

Introduction

You have a Post model. You're a good lil' rabbit, and it only contains methods concerned with its persistence.

So where do you put presentation-specific logic?

The view, right? Nah, man. You should probably use a presenter.

Usage

Let's say we're in PostsController#show and you want to implement some sort of presentation logic. How about we're color coding based on the age of the post.

If you present @post from your controller (and its class is Post), frosting will look for Presenters::Post (defined in /models/presenters/post.rb presumably). Here's what that could look like:

module Presenters
  class Post < Frosting::BasePresenter
    def color
      old? ? 'red' : 'green'
    end
  end
end

You defined #old? in your model because it's not a presentation concern. Good job.

Frosting::BasePresenter delegates to the resource you're presenting, and it also has access to the view context. It doesn't delegate anything by default, but you can delegate things like link_to and content_tag if it makes your life easier. You should probably make your own base presenter that inherits from frosting's base. It's your life, and you should do what you want to.

You can also call present_collection @posts should you be dealing with a collection of posts and want them all to be presented.

Presenting Associations

As an additional trick, if you find yourself writing code like:

def user
  present super(), context: @context
end

in order to have your presented Post return a presenter for the associated User, then you can save some typing with:

presents_super :user

The presents_super method accepts options, so you can specify a presenter:

presents_super :user, presenter: SomeCustomPresenter

You can also use presents_super for collections, by passing the collection option:

presents_super :users, collection: true

About Foraker Labs

Foraker Logo

Foraker Labs builds exciting web and mobile apps in Boulder, CO. Our work powers a wide variety of businesses with many different needs. We love open source software, and we're proud to contribute where we can. Interested to learn more? Contact us today.

This project is maintained by Foraker Labs. The names and logos of Foraker Labs are fully owned and copyright Foraker Design, LLC.

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.