LiteracyVolunteersOfMA/lvm-rails

View on GitHub
app/views/layouts/_index.html.erb

Summary

Maintainability
Test Coverage
<div class="row">
  <div class="col-md-12">
    <div class="panel panel-default">
      <div class="panel-body">
        <h1 class="index-header text-center">
          <%= @page_title %>
          <% if @page_title_subtext %>
            <br>
            <small>
              <%= @page_title_subtext %>
            </small>
          <% end %>
        </h1>
      </div>
    </div>
  </div>
</div>

<% if @new_button %>
  <div class="col-md-2 text-left">
    <div class="index-new-button">
      <a href=<%= @new_button[:url] %> role="button" class="btn btn-success"> <%= @new_button[:text] %> </a>
    </div>
  </div>
<% end %>

<% if @assessments_button %>
  <span class="col-md-6 text-left">
    <span class="index-new-button">
      <a href=<%= @assessments_button[:url] %> role="button" class="btn btn-info"> <%= @assessments_button[:text] %> </a>
    </span>
  </span>
<% end %>

<% if @download_button %>
  <div class="col-md-10 text-right">
    <div class="index-download-button">
      <a href=<%= @download_button[:url] %> role="button" class="btn btn-primary"> <%= @download_button[:text] %> </a>
    </div>
  </div>
<% end %>

<table id="indexTable" class="display hover order-column" width="100%">
  <thead>
    <tr>
      <% @headers.each do |header| %>
        <th> <%= header %> </th>
      <% end %>
  </thead>

  <% @models.each do |model| %>
    <% if @clickable_rows %>
      <tr class="clickable" data-url=<%= url_for(model) %>>
    <% else %>
      <tr>
    <% end %>
    <% @columns.each do |column| %>
      <td> <%= model.send("#{column}") %> </td>
    <% end %>
    </tr>
  <% end %>
</table>

<script>
  // Because turbolinks gets the page from the cache instead of making another
  // GET request, functionality of DataTables is broken.  DataTables thinks it
  // needs to recreate the table, causing a duplicate table to be created and the
  // modal appears on the screen but does not repsond to the close buttons. The
  // solution is when reading from the turbolinks cache to destroy the old
  // DataTables table.
  $(document).ready(function(){
    var indexTable;

    document.addEventListener('turbolinks:load', function() {
      indexTable = $('#indexTable').DataTable();
    });

    document.addEventListener("turbolinks:before-cache", function() {
      if ($('#indexTable_wrapper').length == 1)
        indexTable.destroy();
    });
  });

</script>

<% if @clickable_rows %>
  <script>
    // Linkify the columns!
    $('#indexTable tbody tr').click(function(e) {
      window.location.assign($(e.currentTarget).attr('data-url'));
    });
  </script>
<% end %>