Permalink
Browse files

Moved backend/posts/{index, show} views to mustache.

  • Loading branch information...
1 parent 605a321 commit d7947d36abcaeddd6b99d6ad9c1b13ff3f5a6e6f @jhbabon committed May 9, 2012
@@ -16,6 +16,5 @@
//= require_self
$(document).ready(function() {
- alert('loaded');
$('.datepicker').datepicker();
});
@@ -0,0 +1,11 @@
+# -*- encoding: utf-8 -*-
+
+# TODO: tests!
+class ImagePresenter < BasePresenter
+ presents :image
+ delegate :title, to: :image
+
+ def thumbnail
+ h.image_tag image.resource_url(:thumb).to_s
+ end
+end
@@ -3,10 +3,14 @@
# TODO: tests!
class PostPresenter < BasePresenter
presents :post
- delegate :title, :published_at, :body, :images, :event, to: :post
+ delegate :title, :published, :body, :images, :event, to: :post
def linked_title
- h.link_to post.title, post;
+ h.link_to post.title, post
+ end
+
+ def clean_excerpt
+ post.excerpt
end
def excerpt
@@ -20,4 +24,32 @@ def category
h.render partial: 'posts/category', locals: { category: post.category }
end
end
+
+ def published_at
+ h.l(post.published_at, format: :long) if post.published_at.present?
+ end
+
+ def images
+ post.images.map { |image| self.class.build(image, h) }
+ end
+
+ # TODO: chose a better name
+ def link_to_display
+ h.link_to 'Display', post
+ end
+
+ def link_to_view
+ h.link_to 'View', h.backend_post_path(post)
+ end
+
+ def link_to_edit
+ h.link_to 'Edit', h.edit_backend_post_path(post)
+ end
+
+ def link_to_destroy
+ h.link_to 'Destroy',
+ h.backend_post_path(post),
+ confirm: 'Are you sure?',
+ method: :delete
+ end
end
@@ -0,0 +1,35 @@
+<div class="row">
+ <div class="span12">
+ <table class="table">
+ <tr>
+ <th>Title</th>
+ <th>Published at</th>
+ <th>Published</th>
+ <th></th>
+ <th></th>
+ <th></th>
+ <th></th>
+ </tr>
+
+ {{# each_post }}
+ <tr>
+ <td>{{& linked_title }}</td>
+ <td>{{ published_at }}</td>
+ <td>{{ published }}</td>
+ <td>{{& link_to_display }}</td>
+ <td>{{& link_to_view }}</td>
+ <td>{{& link_to_edit }}</td>
+ <td>{{& link_to_destroy }}</td>
+ </tr>
+ {{/ each_post }}
+ </table>
+ <!-- TODO: add pagination with kaminari -->
+
+ <br />
+
+ <ul>
+ <li>{{& link_to_new_post }}</li>
+ <li>{{& link_to_new_evented_post }}</li>
+ </ul>
+ </div>
+</div>
@@ -0,0 +1,52 @@
+<div class="row">
+ <div class="span12">
+
+ {{# notice? }}
+ <p id="notice">{{ notice }}</p>
+ {{/ notice? }}
+
+ {{# show_post }}
+ <p>
+ <b>Title:</b>
+ {{ title }}
+ </p>
+
+ <p>
+ <b>Body:</b>
+ {{ body }}
+ </p>
+
+ <p>
+ <b>Excerpt:</b>
+ {{ clean_excerpt }}
+ </p>
+
+ <p>
+ <b>Published at:</b>
+ {{ published_at }}
+ </p>
+
+ <p>
+ <b>Published:</b>
+ {{ published }}
+ </p>
+
+ <p>
+ <strong>Images</strong>
+ </p>
+ <ul class="thumbnails">
+ {{# images }}
+ <li class="span3">
+ <div class="thumbnail">
+ {{& thumbnail }}
+ <h5>{{ title }}</h5>
+ </div>
+ </li>
+ {{/ images }}
+ </ul>
+
+ {{& link_to_edit }} |
+ {{/ show_post}}
+ {{& link_to_back }}
+ </div>
+</div>
@@ -1,40 +0,0 @@
-<%- title 'All posts' -%>
-
-<div class="row">
- <div class="span12">
- <table class="table">
- <tr>
- <th>Title</th>
- <th>Body</th>
- <th>Excerpt</th>
- <th>Published at</th>
- <th>Published</th>
- <th></th>
- <th></th>
- <th></th>
- <th></th>
- </tr>
-
- <% @posts.each do |post| %>
- <tr>
- <td><%= post.title %></td>
- <td><%= post.body %></td>
- <td><%= post.excerpt %></td>
- <td><%= l post.published_at, format: :short if post.published_at? %></td>
- <td><%= post.published %></td>
- <td><%= link_to 'Display', post %></td>
- <td><%= link_to 'View', backend_post_path(post) %></td>
- <td><%= link_to 'Edit', edit_backend_post_path(post) %></td>
- <td><%= link_to 'Destroy', backend_post_path(post), confirm: 'Are you sure?', method: :delete %></td>
- </tr>
- <% end %>
- </table>
-
- <%# TODO: add pagination with kaminari %>
-
- <br />
-
- <%= link_to 'New Post', new_backend_post_path %>
- <%= link_to 'New event post', new_backend_post_path(with_events: 1) %>
- </div>
-</div>
@@ -0,0 +1,22 @@
+# -*- encoding: utf-8 -*-
+
+module Backend
+ module Posts
+ class Index < ::Canvas::View
+ title 'All posts'
+
+ def each_post
+ @posts.map { |post| h.present post }
+ end
+
+ def link_to_new_post
+ h.link_to 'New Post', h.new_backend_post_path
+ end
+
+ def link_to_new_evented_post
+ h.link_to 'New event post', h.new_backend_post_path(with_events: 1)
+ end
+ end
+ end
+end
+
@@ -1,49 +0,0 @@
-<%- title "Post: #{@post.title}" -%>
-<div class="row">
- <div class="span12">
-
- <p id="notice"><%= notice %></p>
-
- <p>
- <b>Title:</b>
- <%= @post.title %>
- </p>
-
- <p>
- <b>Body:</b>
- <%= @post.body %>
- </p>
-
- <p>
- <b>Excerpt:</b>
- <%= @post.excerpt %>
- </p>
-
- <p>
- <b>Published at:</b>
- <%= l @post.published_at, format: :long %>
- </p>
-
- <p>
- <b>Published:</b>
- <%= @post.published %>
- </p>
-
- <p>
- <strong>Images</strong>
- </p>
- <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>
-
- <%= link_to 'Edit', edit_backend_post_path(@post) %> |
- <%= link_to 'Back', backend_posts_path %>
- </div>
-</div>
@@ -0,0 +1,18 @@
+# -*- encoding: utf-8 -*-
+
+module Backend
+ module Posts
+ class Show < ::Canvas::View
+ title { "Post: #{@post.title}" }
+
+ def show_post
+ h.present @post
+ end
+
+ def link_to_back
+ h.link_to 'Back', h.backend_posts_path
+ end
+ end
+ end
+end
+
@@ -5,7 +5,7 @@
<header><h1><%= presenter.title %></h1></header>
- <time><%= l presenter.published_at, format: :long %></time>
+ <time><%= presenter.published_at %></time>
<%= presenter.excerpt %>
@@ -26,7 +26,7 @@
<% presenter.images.each do |image| %>
<li class="span3">
<div class="thumbnail">
- <%= image_tag image.resource_url(:thumb).to_s %>
+ <%= image.thumbnail %>
<h5><%= image.title %></h5>
</div>
</li>
View
@@ -4,7 +4,11 @@ module Canvas
class Layout < ::Canvas::View
# TODO: add i18n
def page_title
- view.content_for?(:title) ? view.content_for(:title) : 'Untitled'
+ if view.content_for?(:title)
+ view.content_for(:title)
+ else
+ I18n.t('application.defaults.title')
+ end
end
end
end
View
@@ -25,14 +25,17 @@ def title(txt = '', &block)
end
end
- def render(*)
+ alias_method :h, :view
+
+ def render(*arguments)
_setup # get ready for the render party
- super
+ super(*arguments)
end
def _setup
_execute_callback(:title)
end
+ protected :_setup
def title
_execute_callback(:title) unless view.content_for?(:title)
@@ -57,6 +60,14 @@ def _setup_assets
_assets
end
protected :_setup_assets
+
+ def notice?
+ notice.present?
+ end
+
+ def alert?
+ alert.present?
+ end
end
end

0 comments on commit d7947d3

Please sign in to comment.