[PATCH conductor 3/8] Mustachifying deployments/_pretty_view_show
ifarkas at redhat.com
ifarkas at redhat.com
Thu Apr 19 07:30:01 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/_pretty_view_show.html.haml | 78 +-------------------
.../views/instances/_instance_card.html.mustache | 58 +++++++++++++++
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/_deployment_card.html.mustache
create mode 100644 src/app/views/instances/_instance_card.html.mustache
diff --git a/src/app/controllers/instances_controller.rb b/src/app/controllers/instances_controller.rb
index 7773d81..014b5a4 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 3443038..b74a341 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..086caf5
--- /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),
+ :translated_state => t("instances.states.#{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/_deployment_card.html.mustache b/src/app/views/deployments/_deployment_card.html.mustache
new file mode 100644
index 0000000..e69de29
diff --git a/src/app/views/deployments/_pretty_view_show.html.haml b/src/app/views/deployments/_pretty_view_show.html.haml
index c354ca9..696fd38 100644
--- a/src/app/views/deployments/_pretty_view_show.html.haml
+++ b/src/app/views/deployments/_pretty_view_show.html.haml
@@ -1,30 +1,6 @@
%ul.instances-array
- @instances.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= t("instances.states.#{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 => 'instances/instance_card', :mustache => instance_for_mustache(instance)
= render_pagination(@instances)
@@ -33,53 +9,5 @@
'#{url_for(:action => :show, :details_tab => 'instances', :view => 'filter', :page => params[:page])}',
'#{url_for(:action => :show, :details_tab => 'instances', :view => 'pretty', :page => params[:page])}');
-%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>${translated_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#instanceCardTemplate{ :type => 'text/html' }
+ = render :partial => 'instances/instance_card'
diff --git a/src/app/views/instances/_instance_card.html.mustache b/src/app/views/instances/_instance_card.html.mustache
new file mode 100644
index 0000000..1cb651b
--- /dev/null
+++ b/src/app/views/instances/_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>{{translated_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/public/javascripts/backbone/views.js b/src/public/javascripts/backbone/views.js
index a28bb1f..4c956ca 100644
--- a/src/public/javascripts/backbone/views.js
+++ b/src/public/javascripts/backbone/views.js
@@ -211,6 +211,9 @@ Conductor.Views.DeploymentsShow = Backbone.View.extend({
},
render: function() {
+ $template = $('#instanceCardTemplate');
+ if($template.length === 0) return;
+
var $instances = this.$('ul.instances-array');
if($instances.length === 0) {
$instances = this.$('table.checkbox_table > tbody');
@@ -218,7 +221,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