Browse files

Added forms to mustache templates

  • Loading branch information...
1 parent d7947d3 commit a78a716dd34bc302b6c753749ab763f866b6578a @jhbabon committed May 13, 2012
View
29 app/templates/backend/posts/_form.html.mustache
@@ -0,0 +1,29 @@
+<div class="row">
+ <div class="span12">
+ {{# form }}
+ {{ input_title }}
+ {{ input_body }}
+ {{ input_excerpt }}
+
+ {{ association_category }}
+
+ {{ input_published_at }}
+
+ {{# images }}
+ {{ input_title }}
+ {{ input_resource }}
+ {{/ images }}
+
+ {{# events }}
+ <fieldset>
+ <legend>Datos del evento</legend>
+ {{ input_title }}
+ {{ input_start_date }}
+ {{ association_place }}
+ </fieldset>
+ {{/ events }}
+
+ {{ submit }}
+ {{/ form }}
+ </div>
+</div>
View
4 app/templates/backend/posts/edit.html.mustache
@@ -0,0 +1,4 @@
+{{> form }}
+
+{{& link_to_view }}
+{{& link_to_back }}
View
3 app/templates/backend/posts/new.html.mustache
@@ -0,0 +1,3 @@
+{{> form }}
+
+{{& link_to_back }}
View
37 app/views/backend/posts/_form.html.erb
@@ -1,37 +0,0 @@
-<div class="row">
- <div class="span12">
- <%= simple_form_for([:backend, @post],
- html: { class: 'form-vertical',
- multipart: true }) do |f| %>
- <%= f.input :title %>
- <%= f.input :body %>
- <%= f.input :excerpt, :as => :text %>
-
- <%= f.association :category %>
-
- <%= f.input :published_at %>
-
- <%= f.simple_fields_for(:images) do |fi| %>
- <%= fi.input :title %>
- <%= fi.input :resource %>
- <% end -%>
-
- <% if @post.event.present? %>
- <%= f.simple_fields_for :event do |fe| %>
- <fieldset>
- <legend>Datos del evento</legend>
-
- <%= fe.input :title %>
- <%= fe.input :start_date %>
-
- <%= fe.association :place %>
-
- </fieldset>
- <% end %>
- <% end %>
-
-
- <%= f.button :submit %>
- <% end %>
- </div>
-</div>
View
21 app/views/backend/posts/blueprint.rb
@@ -0,0 +1,21 @@
+# -*- encoding: utf-8 -*-
+
+module Backend
+ module Posts
+ module Blueprint
+ def self.included(base)
+ base.class_eval do
+ delegate :link_to_view, to: :presenter
+ end
+ end
+
+ def presenter
+ @_presenter ||= h.present @post
+ end
+
+ def link_to_back
+ h.link_to 'Back', h.backend_posts_path
+ end
+ end
+ end
+end
View
6 app/views/backend/posts/edit.html.erb
@@ -1,6 +0,0 @@
-<%- title "Edit post: #{@post.title}" -%>
-
-<%= render 'form' %>
-
-<%= link_to t('Show'), backend_post_path(@post) %> |
-<%= link_to t('Back'), backend_posts_path %>
View
13 app/views/backend/posts/edit.rb
@@ -0,0 +1,13 @@
+# -*- encoding: utf-8 -*-
+
+module Backend
+ module Posts
+ class Edit < ::Canvas::View
+ include ::Backend::Posts::Blueprint
+ include ::Backend::Posts::Form
+
+ title { "Edit post: #{@post.title}" }
+ end
+ end
+end
+
View
51 app/views/backend/posts/form.rb
@@ -0,0 +1,51 @@
+# -*- encoding: utf-8 -*-
+
+module Backend
+ module Posts
+ module Form
+ include ::Canvas::FormBuilder
+
+ def form
+ path = [:backend, @post]
+ options = { html: { class: 'form-vertical', multipart: true } }
+
+ formable(path, options) do |f|
+ {
+ input_title: f.input(:title),
+ input_body: f.input(:body),
+ input_excerpt: f.input(:excerpt, as: :text),
+ association_category: f.association(:category),
+ input_published_at: f.input(:published_at),
+ images: fields_for_images(f),
+ events: fields_for_events(f),
+ submit: f.submit,
+ }
+ end
+ end
+
+ def fields_for_images(f)
+ nested_formable(f, :images) do |fi|
+ {
+ input_title: fi.input(:title),
+ input_resource: fi.input(:resource),
+ }
+ end
+ end
+ protected :fields_for_images
+
+ def fields_for_events(f)
+ if @post.event
+ nested_formable(f, :events) do |fe|
+ {
+ input_title: fe.input(:title),
+ input_start_date: fe.input(:start_date),
+ association_place: fe.association(:place),
+ }
+ end
+ end
+ end
+ protected :fields_for_events
+ end
+ end
+end
+
View
5 app/views/backend/posts/new.html.erb
@@ -1,5 +0,0 @@
-<%- title 'New post' -%>
-
-<%= render 'form' %>
-
-<%= link_to 'Back', backend_posts_path %>
View
13 app/views/backend/posts/new.rb
@@ -0,0 +1,13 @@
+# -*- encoding: utf-8 -*-
+
+module Backend
+ module Posts
+ class New < ::Canvas::View
+ include ::Backend::Posts::Blueprint
+ include ::Backend::Posts::Form
+
+ title 'New post'
+ end
+ end
+end
+
View
10 app/views/backend/posts/show.rb
@@ -3,15 +3,11 @@
module Backend
module Posts
class Show < ::Canvas::View
- title { "Post: #{@post.title}" }
+ include ::Backend::Posts::Blueprint
- def show_post
- h.present @post
- end
+ title { "Post: #{@post.title}" }
- def link_to_back
- h.link_to 'Back', h.backend_posts_path
- end
+ alias_method :show_post, :presenter
end
end
end
View
4 app/views/backend/shared/_page_title.html.erb
@@ -1,8 +1,6 @@
-<%= show_title do %>
<div class="row">
<div class="span12">
- <h1><%= yield :title %></h1>
+ <h1><%= content_for(:title) %></h1>
</div>
</div>
<hr />
-<%- end -%>
View
1 lib/canvas.rb
@@ -7,6 +7,7 @@
require 'canvas/assets'
require 'canvas/view'
require 'canvas/layout'
+require 'canvas/form_builder'
module Canvas
end
View
43 lib/canvas/form_builder.rb
@@ -0,0 +1,43 @@
+# -*- encoding: utf-8 -*-
+
+# The idea was borrowed from:
+# @link: https://gist.github.com/954994
+module Canvas
+ module FormBuilder
+ class Formable < ::Mustache
+ def initialize(data)
+ data.each_pair do |key, value|
+ self.class.send(:define_method, key, proc { value })
+ end
+ end
+
+ def escapeHTML(str)
+ str
+ end
+ end
+
+ def formable(object, *args, &block)
+ lambda do |text|
+ view.simple_form_for(object, *args) do |f|
+ build_form(f, text, &block)
+ end
+ end
+ end
+
+ def nested_formable(f, field, *args, &block)
+ lambda do |text|
+ f.simple_fields_for(field, *args) do |f|
+ build_form(f, text, &block)
+ end
+ end
+ end
+
+ def build_form(f, text, &block)
+ mustache_view = Formable.new(yield(f))
+
+ ::Mustache.render(text, mustache_view).html_safe
+ end
+ protected :build_form
+ end
+end
+
View
14 lib/canvas/view.rb
@@ -33,10 +33,18 @@ def render(*arguments)
end
def _setup
- _execute_callback(:title)
+ unless already_setup?
+ _execute_callback(:title)
+ @already_setup = true
+ end
end
protected :_setup
+ def already_setup?
+ @already_setup
+ end
+ protected :already_setup?
+
def title
_execute_callback(:title) unless view.content_for?(:title)
@@ -62,11 +70,11 @@ def _setup_assets
protected :_setup_assets
def notice?
- notice.present?
+ h.notice.present?
end
def alert?
- alert.present?
+ h.alert.present?
end
end
end

0 comments on commit a78a716

Please sign in to comment.