Permalink
Browse files

Use custom presenters instead of Draper's decorators

  • Loading branch information...
1 parent 5b5068e commit f07209279b243a74f6b387836421fa24d6ea8504 @jhbabon committed Apr 20, 2012
View
@@ -56,5 +56,3 @@ gem 'mini_magick', '~> 3.4'
gem 'carrierwave', '~> 0.5.8'
# better forms
gem 'simple_form', '~> 2.0.1'
-
-gem 'draper'
@@ -3,6 +3,6 @@
class MgzineController < ApplicationController
def index
- @posts = PostDecorator.all
+ @posts = Post.all
end
end
@@ -2,7 +2,7 @@ class PostsController < ApplicationController
before_filter :find_post
def find_post
- @post = PostDecorator.find params[:id]
+ @post = Post.find params[:id]
end
protected :find_post
@@ -1,28 +0,0 @@
-class ApplicationDecorator < Draper::Base
- # Lazy Helpers
- # PRO: Call Rails helpers without the h. proxy
- # ex: number_to_currency(model.price)
- # CON: Add a bazillion methods into your decorator's namespace
- # and probably sacrifice performance/memory
- #
- # Enable them by uncommenting this line:
- # lazy_helpers
-
- # Shared Decorations
- # Consider defining shared methods common to all your models.
- #
- # Example: standardize the formatting of timestamps
- #
- # def formatted_timestamp(time)
- # h.content_tag :span, time.strftime("%a %m/%d/%y"),
- # :class => 'timestamp'
- # end
- #
- # def created_at
- # formatted_timestamp(model.created_at)
- # end
- #
- # def updated_at
- # formatted_timestamp(model.updated_at)
- # end
-end
@@ -1,48 +0,0 @@
-class PostDecorator < ApplicationDecorator
- decorates :post
-
- def linked_title
- h.link_to model.title, model;
- end
-
- def excerpt
- if post.excerpt.present?
- h.render partial: 'posts/excerpt', locals: { excerpt: post.excerpt }
- end
- end
-
- def category
- if post.category.present?
- h.render partial: 'posts/category', locals: { category: post.category }
- end
- end
-
- # Accessing Helpers
- # You can access any helper via a proxy
- #
- # Normal Usage: helpers.number_to_currency(2)
- # Abbreviated : h.number_to_currency(2)
- #
- # Or, optionally enable "lazy helpers" by including this module:
- # include Draper::LazyHelpers
- # Then use the helpers with no proxy:
- # number_to_currency(2)
-
- # Defining an Interface
- # Control access to the wrapped subject's methods using one of the following:
- #
- # To allow only the listed methods (whitelist):
- # allows :method1, :method2
- #
- # To allow everything except the listed methods (blacklist):
- # denies :method1, :method2
-
- # Presentation Methods
- # Define your own instance methods, even overriding accessors
- # generated by ActiveRecord:
- #
- # def created_at
- # h.content_tag :span, time.strftime("%a %m/%d/%y"),
- # :class => 'timestamp'
- # end
-end
@@ -52,11 +52,11 @@ def render_shared(partial, _scope = nil)
end
# TODO: add namespaces for presenters
- # def present(object, klass = nil)
- # presenter = BasePresenter.build(object, self, klass)
+ def present(object, klass = nil)
+ presenter = BasePresenter.build(object, self, klass)
- # yield presenter if block_given?
+ yield presenter if block_given?
- # presenter
- # end
+ presenter
+ end
end
@@ -1,7 +1,23 @@
# -*- encoding: utf-8 -*-
-# TODO: use it!
# TODO: tests!
class PostPresenter < BasePresenter
presents :post
+ delegate :title, :published_at, :body, :images, to: :post
+
+ def linked_title
+ h.link_to post.title, post;
+ end
+
+ def excerpt
+ if post.excerpt.present?
+ h.render partial: 'posts/excerpt', locals: { excerpt: post.excerpt }
+ end
+ end
+
+ def category
+ if post.category.present?
+ h.render partial: 'posts/category', locals: { category: post.category }
+ end
+ end
end
@@ -1,8 +1,8 @@
<%- title 'NTD Mgzine' -%>
-<% @posts.each do |post| %>
+<% @posts.map { |p| present p }.each do |post| %>
<section>
<h1><%= post.linked_title %></h1>
<%= post.category %>
-</section>
+</section>
<% end %>
@@ -1,29 +1,31 @@
-<%- title "#{@post.title}" -%>
-
-<article>
-
- <header><h1><%= @post.title %></h1></header>
-
- <time><%= l @post.published_at, format: :long %></time>
-
- <%= @post.excerpt %>
-
- <section class="body">
- <%= @post.body %>
- </section>
-
-
- <!-- TODO -->
-
- <ul class="thumbnails">
- <% @post.images.each do |image| %>
- <li class="span3">
- <div class="thumbnail">
- <%= image_tag image.resource_url(:thumb).to_s %>
- <h5><%= image.title %></h5>
- </div>
- </li>
- <% end -%>
- </ul>
-
-</article>
+<% present @post do |presenter| -%>
+ <%- title "#{presenter.title}" -%>
+
+ <article>
+
+ <header><h1><%= presenter.title %></h1></header>
+
+ <time><%= l presenter.published_at, format: :long %></time>
+
+ <%= presenter.excerpt %>
+
+ <section class="body">
+ <%= presenter.body %>
+ </section>
+
+
+ <!-- TODO -->
+
+ <ul class="thumbnails">
+ <% presenter.images.each do |image| %>
+ <li class="span3">
+ <div class="thumbnail">
+ <%= image_tag image.resource_url(:thumb).to_s %>
+ <h5><%= image.title %></h5>
+ </div>
+ </li>
+ <% end -%>
+ </ul>
+
+ </article>
+<% end -%>
@@ -1,4 +0,0 @@
-require 'spec_helper'
-
-describe ApplicationDecorator do
-end
@@ -1,5 +0,0 @@
-require 'spec_helper'
-
-describe PostDecorator do
- before { ApplicationController.new.set_current_view_context }
-end

0 comments on commit f072092

Please sign in to comment.