[PATCH conductor 3/7] Mustachifying deployments/_pretty_view_show

ifarkas at redhat.com ifarkas at redhat.com
Tue Feb 28 14:53:25 UTC 2012


From: Imre Farkas <ifarkas at redhat.com>

---
 src/app/controllers/instances_controller.rb        |    2 +-
 src/app/helpers/application_helper.rb              |    3 +
 src/app/helpers/mustache_helper.rb                 |   38 ++++++++++
 .../views/deployments/_instance_card.html.mustache |   58 +++++++++++++++
 .../views/deployments/_pretty_view_show.html.haml  |   78 +-------------------
 src/public/javascripts/backbone/views.js           |   10 ++-
 6 files changed, 112 insertions(+), 77 deletions(-)
 create mode 100644 src/app/helpers/mustache_helper.rb
 create mode 100644 src/app/views/deployments/_instance_card.html.mustache

diff --git a/src/app/controllers/instances_controller.rb b/src/app/controllers/instances_controller.rb
index ef04cd7..77d932a 100644
--- a/src/app/controllers/instances_controller.rb
+++ b/src/app/controllers/instances_controller.rb
@@ -28,7 +28,7 @@ class InstancesController < ApplicationController
     respond_to do |format|
       format.html
       format.js { render :partial => 'list' }
-      format.json { render :json => @instances }
+      format.json { render :json => @instances.map{ |instance| view_context.instance_for_mustache(instance) } }
     end
   end
 
diff --git a/src/app/helpers/application_helper.rb b/src/app/helpers/application_helper.rb
index 9e24112..8f51b57 100644
--- a/src/app/helpers/application_helper.rb
+++ b/src/app/helpers/application_helper.rb
@@ -18,6 +18,9 @@
 # Likewise, all the methods added will be available for all controllers.
 
 module ApplicationHelper
+
+  include MustacheHelper
+
   def number_to_duration(input_num)
     input_int = input_num.to_i
     hours_to_seconds = [input_int/3600 % 24,
diff --git a/src/app/helpers/mustache_helper.rb b/src/app/helpers/mustache_helper.rb
new file mode 100644
index 0000000..4e844c7
--- /dev/null
+++ b/src/app/helpers/mustache_helper.rb
@@ -0,0 +1,38 @@
+#
+#   Copyright 2012 Red Hat, Inc.
+#
+#   Licensed under the Apache License, Version 2.0 (the "License");
+#   you may not use this file except in compliance with the License.
+#   You may obtain a copy of the License at
+#
+#       http://www.apache.org/licenses/LICENSE-2.0
+#
+#   Unless required by applicable law or agreed to in writing, software
+#   distributed under the License is distributed on an "AS IS" BASIS,
+#   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+#   See the License for the specific language governing permissions and
+#   limitations under the License.
+#
+
+# Filters added to this controller apply to all controllers in the application.
+# Likewise, all the methods added will be available for all controllers.
+
+module MustacheHelper
+
+  def instance_for_mustache(instance)
+    available_actions = instance.get_action_list
+
+    {
+      :id       => instance.id,
+      :name     => instance.name,
+      :path     => instance_path(instance),
+      :uptime   => count_uptime(instance.uptime),
+      :state    => instance.state,
+      :public_addresses     => instance.public_addresses.present? ? instance.public_addresses : I18n.t('deployments.pretty_view_show.no_ip_address'),
+      :instance_key_present => instance.instance_key.present?,
+      :stop_enabled         => available_actions.include?(InstanceTask::ACTION_STOP),
+      :reboot_enabled       => available_actions.include?(InstanceTask::ACTION_REBOOT),
+    }
+  end
+
+end
diff --git a/src/app/views/deployments/_instance_card.html.mustache b/src/app/views/deployments/_instance_card.html.mustache
new file mode 100644
index 0000000..4b9dcb8
--- /dev/null
+++ b/src/app/views/deployments/_instance_card.html.mustache
@@ -0,0 +1,58 @@
+<li class="instance">
+  <h3 class="name">
+    <%= link_to('{{name}}', '{{path}}') %>
+  </h3>
+  <hr>
+  <dl class="statistics">
+    <ul>
+      <li>
+        <dt class="uptime"></dt>
+        <dd>{{uptime}}</dd>
+      </li>
+      <li>
+        <dt class="ip_address"><%= t('deployments.pretty_view_show.ip_address') %></dt>
+        <dd>{{public_addresses}}</dd>
+      </li>
+      <li>
+        <dt class="state"><%= t('deployments.pretty_view_show.state') %></dt>
+        <dd>{{state}}</dd>
+      </li>
+    {{#instance_key_present}}
+      <li>
+        <dt></dt>
+        <dd><%= link_to(t('deployments.pretty_view_show.download_key'), key_instance_path('replace_id').sub('replace_id', '{{id}}')) %></dd>
+      </li>
+    {{/instance_key_present}}
+    </ul>
+  </dl>
+  <hr>
+  <div class="actions">
+    <ul>
+      {{#stop_enabled}}
+        <li>
+          <%= button_to(t('deployments.pretty_view_show.stop'), stop_instance_path('replace_id').sub('replace_id', '{{id}}'),
+                        :disabled => false, :class => 'button danger') %>
+        </li>
+      {{/stop_enabled}}
+      {{^stop_enabled}}
+        <li>
+          <%= button_to(t('deployments.pretty_view_show.stop'), stop_instance_path('replace_id').sub('replace_id', '{{id}}'),
+                        :disabled => true, :class => 'button danger') %>
+        </li>
+      {{/stop_enabled}}
+
+      {{#reboot_enabled}}
+        <li>
+          <%= button_to(t('deployments.pretty_view_show.reboot'), reboot_instance_path('replace_id').sub('replace_id', '{{id}}'),
+                        :disabled => false, :class => 'button danger') %>
+        </li>
+      {{/reboot_enabled}}
+      {{^reboot_enabled}}
+        <li>
+          <%= button_to(t('deployments.pretty_view_show.reboot'), reboot_instance_path('replace_id').sub('replace_id', '{{id}}'),
+                        :disabled => true, :class => 'button danger') %>
+        </li>
+      {{/reboot_enabled}}
+    </ul>
+  </div>
+</li>
diff --git a/src/app/views/deployments/_pretty_view_show.html.haml b/src/app/views/deployments/_pretty_view_show.html.haml
index f40284c..244e1b3 100644
--- a/src/app/views/deployments/_pretty_view_show.html.haml
+++ b/src/app/views/deployments/_pretty_view_show.html.haml
@@ -1,83 +1,11 @@
 %ul.instances-array
   - @deployment.instances.ascending_by_name.each do |instance|
-    %li.instance
-      %h3.name
-        = link_to instance.name, instance
-      %hr
-      %dl.statistics
-        %ul
-          %li
-            %dt.uptime
-            %dd= count_uptime(instance.uptime)
-          %li
-            %dt.ip_address= t('deployments.pretty_view_show.ip_address')
-            %dd= instance.public_addresses.present? ? instance.public_addresses : t('deployments.pretty_view_show.no_ip_address')
-          %li
-            %dt.state= t('deployments.pretty_view_show.state')
-            %dd= instance.state
-          - if instance.instance_key
-            %li
-              %dt
-              %dd= link_to t('deployments.pretty_view_show.download_key'), key_instance_path(instance)
-      %hr
-      .actions
-        %ul
-          - available_actions = instance.get_action_list
-          %li= button_to(t('deployments.pretty_view_show.stop'), stop_instance_path(instance), :disabled => !available_actions.include?(InstanceTask::ACTION_STOP), :class => "button danger")
-          %li= button_to(t('deployments.pretty_view_show.reboot'), reboot_instance_path(instance), :disabled => !available_actions.include?(InstanceTask::ACTION_REBOOT), :class => "button danger")
+    = render :partial => 'instance_card', :mustache => instance_for_mustache(instance)
 
 :javascript
   Conductor.setupPrettyFilterURL(
             '#{url_for(:action => :show, :details_tab => 'instances', :view => 'filter' )}',
             '#{url_for(:action => :show, :details_tab => 'instances', :view => 'pretty' )}');
 
-%script#instanceTemplate{:type => 'text/x-jquery-tmpl'}
-  :plain
-    <li class="instance">
-      <h3 class="name"><a href="#{instance_path('replace_id').sub('replace_id', '${id}')}">${name}</a></h3>
-      <hr>
-      <dl class="statistics">
-        <ul>
-          <li>
-            <dt class="uptime"></dt>
-            <dd>  ${uptime}</dd>
-          </li>
-          <li>
-            <dt class="ip_address">#{t('deployments.pretty_view_show.ip_address')}</dt>
-            <dd>
-              {{if public_addresses}}
-                ${public_addresses}
-              {{else}}
-                #{t('deployments.pretty_view_show.no_ip_address')}
-              {{/if}}
-            </dd>
-          </li>
-          <li>
-            <dt class="state">#{t('deployments.pretty_view_show.state')}</dt>
-            <dd>${state}</dd>
-          </li>
-          {{if has_key}}
-            <li>
-              <dt/>
-              <dd><a href="#{key_instance_path('replace_id').sub('replace_id', '${id}')}">#{t('deployments.pretty_view_show.download_key')}</a></dd>
-            </li>
-          {{/if}}
-        </ul>
-      </dl>
-      <hr>
-      <div class="actions">
-        <ul>
-          {{if stop_enabled}}
-            <li>#{button_to(t('deployments.pretty_view_show.stop'), stop_instance_path('replace_id').sub('replace_id', '${id}'), :disabled => false, :class => 'button danger')}</li>
-          {{else}}
-            <li>#{button_to(t('deployments.pretty_view_show.stop'), stop_instance_path('replace_id').sub('replace_id', '${id}'), :disabled => true, :class => 'button danger')}</li>
-          {{/if}}
-
-          {{if reboot_enabled}}
-            <li>#{button_to(t('deployments.pretty_view_show.reboot'), reboot_instance_path('replace_id').sub('replace_id', '${id}'), :disabled => false, :class => 'button danger')}</li>
-          {{else}}
-            <li>#{button_to(t('deployments.pretty_view_show.reboot'), reboot_instance_path('replace_id').sub('replace_id', '${id}'), :disabled => true, :class => 'button danger')}</li>
-          {{/if}}
-        </ul>
-      </div>
-    </li>
+%script#deploymentTemplate{ :type => 'text/html' }
+  = render :partial => 'instance_card'
diff --git a/src/public/javascripts/backbone/views.js b/src/public/javascripts/backbone/views.js
index 2d6da0d..c51be78 100644
--- a/src/public/javascripts/backbone/views.js
+++ b/src/public/javascripts/backbone/views.js
@@ -179,6 +179,9 @@ Conductor.Views.DeploymentsShow = Backbone.View.extend({
   },
 
   render: function() {
+    $template = $('#deploymentTemplate');
+    if($template.length === 0) return;
+
     var $instances = this.$('ul.instances-array');
     if($instances.length === 0) {
       $instances = this.$('table.checkbox_table > tbody');
@@ -186,7 +189,12 @@ Conductor.Views.DeploymentsShow = Backbone.View.extend({
     if($instances.length === 0) return;
 
     $instances.empty();
-    $('#instanceTemplate').tmpl(this.collection.toJSON()).appendTo($instances);
+
+    var instanceCardsHtml = '';
+    $.each(this.collection.toJSON(), function(instanceIndex, instance) {
+      instanceCardsHtml += Mustache.to_html($template.html(), instance);
+    });
+    $instances.html(instanceCardsHtml);
   }
 
 });
-- 
1.7.6.5




More information about the aeolus-devel mailing list