[PATCH conductor 6/8] Mustachifying pools/_list

ifarkas at redhat.com ifarkas at redhat.com
Thu Apr 19 07:30:04 UTC 2012


From: Imre Farkas <ifarkas at redhat.com>

---
 src/app/controllers/pools_controller.rb     |    4 +-
 src/app/helpers/mustache_helper.rb          |   26 ++++++++++++++++
 src/app/views/pools/_list.html.haml         |   43 ++++++--------------------
 src/app/views/pools/_pool_row.html.mustache |   26 ++++++++++++++++
 src/public/javascripts/backbone/views.js    |   12 +++++--
 5 files changed, 73 insertions(+), 38 deletions(-)
 create mode 100644 src/app/views/pools/_pool_row.html.mustache

diff --git a/src/app/controllers/pools_controller.rb b/src/app/controllers/pools_controller.rb
index 2ae502a..4a3a9d8 100644
--- a/src/app/controllers/pools_controller.rb
+++ b/src/app/controllers/pools_controller.rb
@@ -78,11 +78,11 @@ class PoolsController < ApplicationController
       format.json do
         case @details_tab[:id]
         when 'pools'
-          render :json => @pools
+          render :json => @pools.map{ |pool| view_context.pool_for_mustache(pool) }
         when 'instances'
           render :json => @instances
         when 'deployments'
-          render :json => @deployments
+          render :json => @deployments.map{ |deployment| view_context.deployment_for_mustache(deployment) }
         end
       end
     end
diff --git a/src/app/helpers/mustache_helper.rb b/src/app/helpers/mustache_helper.rb
index 60cc9a1..b13ef67 100644
--- a/src/app/helpers/mustache_helper.rb
+++ b/src/app/helpers/mustache_helper.rb
@@ -63,4 +63,30 @@ module MustacheHelper
     }
   end
 
+  def pool_for_mustache(pool)
+    pool_statistics = pool.statistics
+    user_can_access_pool_family = check_privilege(Privilege::VIEW, pool.pool_family)
+
+    {
+      :id               => pool.id,
+      :name             => pool.name,
+      :filter_view_path => pool_path(pool, :view => :filter),
+      :failed_instances_present => pool_statistics[:instances_failed_count] > 0,
+      :deployments_count        => pool.deployments.count,
+
+      :statistics => {
+        :total_instances        => pool_statistics[:total_instances],
+        :instances_pending      => pool_statistics[:instances_pending],
+        :instances_failed_count => pool_statistics[:instances_failed_count],
+        :quota_percent          => pool_statistics[:quota_percent],
+      },
+
+      :user_can_access_pool_family => user_can_access_pool_family,
+      :pool_family => {
+        :name => pool.pool_family.name,
+        :path => user_can_access_pool_family ? pool_family_path(pool.pool_family) : nil
+      }
+    }
+  end
+
 end
diff --git a/src/app/views/pools/_list.html.haml b/src/app/views/pools/_list.html.haml
index 5dc5e5d..bd1edd5 100644
--- a/src/app/views/pools/_list.html.haml
+++ b/src/app/views/pools/_list.html.haml
@@ -24,36 +24,13 @@
     });
 
 = filter_table(@header, @pools) do |pool|
-  - pool_stats = pool.statistics
-  %tr{:class => cycle('nostripe','stripe')}
-    %td{:class => 'checkbox'}
-      %input{:name => "pools_selected[]", :type => "checkbox", :value => pool.id, :id => "pool_checkbox_#{pool.id}" }
-    %td{:class => 'alert'}= (@statistics[:instances_failed] & pool.instances).present? ? image_tag("sb_icon_instance_failure.png") : ""
-    %td= link_to(pool.name, pool_path(pool, :view => :filter))
-    %td{:class => 'center'}= pool.deployments.count
-    %td{:class => 'center'}= pool_stats[:total_instances]
-    %td{:class => 'center'}= pool_stats[:instances_pending]
-    %td{:class => 'center'}= pool_stats[:instances_failed_count]
-    %td{:class => 'center'}= pool_stats[:quota_percent]
-    - if check_privilege(Privilege::VIEW, pool.pool_family)
-      %td= link_to(pool.pool_family.name, pool.pool_family)
-    - else
-      %td= pool.pool_family.name
-%script#poolTemplate{ :type => 'text/x-jquery-tmpl' }
-  :plain
-    <tr>
-      <td class="checkbox"><input type="checkbox" id="pool_checkbox_${id}"
-                 name="pools_selected[]" value="${id}"></td>
-      <td class="alert">
-        {{if statistics.instances_failed_count > 0}}
-          #{image_tag("sb_icon_instance_failure.png")}
-        {{/if}}
-      </td>
-      <td><a href="#{pool_path('replace_id', :view => :filter).sub('replace_id', '${id}')}">${name}</a></td>
-      <td class="center">${deployments_count}</td>
-      <td class="center">${statistics.total_instances}</td>
-      <td class="center">${statistics.instances_pending}</td>
-      <td class="center">${statistics.instances_failed_count}</td>
-      <td class="center">${statistics.quota_percent}</td>
-      <td><a href="#{pool_family_path('replace_id').sub('replace_id', '${pool_family.id}')}">${pool_family.name}</a></td>
-    </tr>
+  = render :partial => 'pool_row', :mustache => pool_for_mustache(pool)
+
+:javascript
+  $(document).ready(function(){
+    $('tr:odd').addClass('stripe');
+    $('tr:even').addClass('nostripe');
+  });
+
+%script#poolRowTemplate{ :type => 'text/html' }
+  = render :partial => 'pool_row'
diff --git a/src/app/views/pools/_pool_row.html.mustache b/src/app/views/pools/_pool_row.html.mustache
new file mode 100644
index 0000000..63d3ee2
--- /dev/null
+++ b/src/app/views/pools/_pool_row.html.mustache
@@ -0,0 +1,26 @@
+<tr>
+  <td class="checkbox">
+    <%= check_box_tag("pools_selected[]", '{{id}}', false, :id => "pool_checkbox_{{id}}") %>
+  </td>
+  <td class="alert">
+    {{#failed_instances_present}}
+      <%= image_tag("sb_icon_instance_failure.png") %>
+    {{/failed_instances_present}}
+  </td>
+  <td>
+    <%= link_to('{{name}}', '{{filter_view_path}}') %>
+  </td>
+  <td class="center">{{deployments_count}}</td>
+  <td class="center">{{statistics.total_instances}}</td>
+  <td class="center">{{statistics.instances_pending}}</td>
+  <td class="center">{{statistics.instances_failed_count}}</td>
+  <td class="center">{{statistics.quota_percent}}</td>
+  <td>
+    {{#user_can_access_pool_family}}
+      <%= link_to('{{pool_family.name}}', '{{pool_family.path}}') %>
+    {{/user_can_access_pool_family}}
+    {{^user_can_access_pool_family}}
+      {{pool_family.name}}
+    {{/user_can_access_pool_family}}
+  </td>
+</tr>
diff --git a/src/public/javascripts/backbone/views.js b/src/public/javascripts/backbone/views.js
index 0d29417..4535ac5 100644
--- a/src/public/javascripts/backbone/views.js
+++ b/src/public/javascripts/backbone/views.js
@@ -31,8 +31,8 @@ Conductor.Views.PoolsIndex = Backbone.View.extend({
   template: function() {
     if (this.currentView() == 'table') {
       switch(this.currentTab()) {
-        case 'pools': return $('#poolTemplate');
-        case 'deployments': return $('#deploymentTemplate');
+        case 'pools': return $('#poolRowTemplate');
+        case 'deployments': return $('#deploymentRowTemplate');
         case 'instances': return $('#instanceTemplate');
       }
     }
@@ -66,7 +66,13 @@ Conductor.Views.PoolsIndex = Backbone.View.extend({
       var $table = this.$('table.checkbox_table > tbody');
       if($table.length === 0 || $template.length === 0) return;
       var checkboxes = Conductor.saveCheckboxes('td :checkbox', $table);
-      $table.empty().append($template.tmpl(this.collection.toJSON()))
+
+      var rowsHtml = '';
+      $.each(this.collection.toJSON(), function(index, value) {
+        rowsHtml += Mustache.to_html($template.html(), value);
+      });
+
+      $table.empty().append(rowsHtml);
       Conductor.restoreCheckboxes(checkboxes, 'td :checkbox', $table);
       $table.find('tr:even').addClass('nostripe');
       $table.find('tr:odd').addClass('stripe');
-- 
1.7.6.5




More information about the aeolus-devel mailing list