SexyPresenter add presentation logic to your Rails application. To inject presentation logic in view, SexyPresenter use Refinements feature that added Ruby in version 2.1.
SexyPresenter provides only 2 features.
- You can assign presenters in view files.
- You can use
before_render
hook in presentation file.
Assign presenter in your view file's frontmatter. (SexyPresenter supports any template engines, e.g. .erb, .haml, .slim .)
---
presenter: MessagePresenter
---
<% @messages.each do |m| %>
<%= m.title %>,<%= m.body %>,<%= m.body_length_type %>
<% end %>
In this case, Message
class has title and body fields in models/message.rb
.
But it does not have body_length_type method because body_length_type related only this page.
So, you implement body_length_type method to Message
class in MessagePresenter
module using Refinements.
We suggest you to implement MessagePresenter
in app/presenters/message_presenter
,
but actually you can make the file in any autoload target directory, or in config/initializers
.
module MessagePresenter
refine Message
def body_length_type
if self.body.length > 100
'LONG'
elsif self.body.length > 50
'MIDDLE'
else
'SHORT'
end
end
end
end
This code works well.
body_length_type
method is activated only in sample.erb
.
This is nothing more than Refinements behavior.
If you want special initialization logic to a view file, you can use before_render
hook.
---
presenter: HeaderPresenter
---
Welcome to our web site.
Now we have <%= @customer_count %> customers. Join us!
module HeaderPresenter
before_render do
@customer_count = ::Customer.count
end
end
before_render
block runs in view context.
The frontmatter format is YAML. And you can assign more than one presenters in one template.
---
presenter:
- FirstPresenter
- SecondPresenter
---
...
SexyPresenter depends on Ruby's Refinements feature.
- Rails 4.0 or later.
- Ruby 2.1 or later. (depend on Refinements feature)
This project rocks and uses MIT-LICENSE.
- Fork it
- Create your feature branch (git checkout -b my-new-feature)
- Commit your changes (git commit -am 'Add some feature')
- Push to the branch (git push origin my-new-feature)
- Create new Pull Request