Code Monkey home page Code Monkey logo

presenter_rails's Introduction

Presenter Gem Version Build Status Test Coverage License

Presenter helps you expose view models to your views in a convenient way, while still allowing you to define methods with the same name inside your controllers.

 # app/controllers/people_controller.rb
 class PeopleController < ApplicationController

    present(:person) {
      PersonDecorator.decorate(person)
    }

    ...

    def person
      People.find(params[:id])
    end
 end
 / app/views/people/show.html.haml
 .person
   .person-name= person.name
   .person-info= person.biography

The method is also available in the controller, with a _presenter suffix:

 # app/controllers/people_controller.rb
 class PeopleController < ApplicationController

    present(:person) {
      PersonDecorator.decorate(person)
    }

    def update
      person.update(attrs)
      redirect_to person_presenter.path, notice: "Successfully updated."
    end

    ...

 end

Background

Presenter attempts to simplify the exposure of variables to the views. It doesn't really care about what you are exposing, although it's specially useful to implement two-step views while using view models in combination with resourcerer.

How it works

When you provide a block, it defines a "#{name}_presenter" private method in your controller.

After that, it creates a helper method for your views, which calls the "#{name}_presenter" counterpart in the controller.

Memoization

Each presenter method is memoized, so the method is called only once and your views get the same instance every time. The block is evaluated only if the method is called.

Corolary

Since the helper methods defined are only available for the view, you can define methods with the same name in your controller ๐Ÿ˜ƒ

Credits

Presenter was crafted to use in combination with resourcerer.

presenter_rails's People

Contributors

elmassimo avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

presenter_rails's Issues

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.