https://github.com/elmassimo/presenter_rails
🔠Expose your view models in a convenient way
https://github.com/elmassimo/presenter_rails
presenters rails views
Last synced: 6 months ago
JSON representation
🔠Expose your view models in a convenient way
- Host: GitHub
- URL: https://github.com/elmassimo/presenter_rails
- Owner: ElMassimo
- License: mit
- Created: 2014-02-25T20:17:45.000Z (over 11 years ago)
- Default Branch: main
- Last Pushed: 2022-04-11T23:00:22.000Z (over 3 years ago)
- Last Synced: 2025-04-14T08:51:36.778Z (6 months ago)
- Topics: presenters, rails, views
- Language: Ruby
- Homepage:
- Size: 23.4 KB
- Stars: 6
- Watchers: 3
- Forks: 2
- Open Issues: 1
-
Metadata Files:
- Readme: README.md
- Changelog: CHANGELOG.md
- License: LICENSE.txt
Awesome Lists containing this project
README
Presenter [](https://rubygems.org/gems/presenter_rails) [](https://github.com/ElMassimo/presenter_rails/actions) [](https://codeclimate.com/github/ElMassimo/presenter_rails) [](https://github.com/ElMassimo/presenter_rails/blob/master/LICENSE.txt)
=====================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.```ruby
# app/controllers/people_controller.rb
class PeopleController < ApplicationControllerpresent(:person) {
PersonDecorator.decorate(person)
}...
def person
People.find(params[:id])
end
end
``````haml
/ 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:
```ruby
# app/controllers/people_controller.rb
class PeopleController < ApplicationControllerpresent(: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](http://martinfowler.com/eaaCatalog/twoStepView.html) while using
[view models](https://github.com/drapergem/draper) in combination with [resourcerer](https://github.com/ElMassimo/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 :smiley:Credits
--------
Presenter was crafted to use in combination with [resourcerer](https://github.com/ElMassimo/resourcerer).