BathHacked/energy-sparks

View on GitHub
app/components/energy_tariff_table_component/energy_tariff_table_component.html.erb

Summary

Maintainability
Test Coverage
<table class="table advice-table <%= table_sorted %>" id="<%= @id %>">
  <thead>
    <tr>
      <th class="col-1 text-right"><%= t('schools.user_tariffs.tariff_partial.start_date') %></th>
      <th class="col-1 text-right"><%= t('schools.user_tariffs.tariff_partial.end_date') %></th>
      <th class="col-3 text-left"><%= t('schools.user_tariffs.name') %></th>
      <th class="col-1 text-left"><%= t('schools.user_tariffs.tariff_partial.type') %></th>
      <% if show_prices? %>
        <th class="col-3 text-right" data-orderable="false">
          <%= t('schools.user_tariffs.tariff_partial.consumption_charges') %>
        </th>
      <% end %>
      <% if show_meters? %>
        <th class="col-1 text-left" data-orderable="false"><%= t('schools.user_tariffs.meters.meter_list') %></th>
      <% end %>
      <% if show_actions? %>
        <th class="col-2 text-right" data-orderable="false"></th>
      <% end %>
    </tr>
  </thead>
  <tbody>
    <% @tariffs.each do |energy_tariff| %>
      <tr class="<%= class_for_tariff(energy_tariff) %>" id="energy-tariff-<%= energy_tariff.id %>">
        <td class="text-right" data-order="<%= start_date_sortable(energy_tariff) %>"><%= start_date(energy_tariff) %></td>
        <td class="text-right" data-order="<%= end_date_sortable(energy_tariff) %>"><%= end_date(energy_tariff) %></td>
        <td class="text-left">
          <% if show_actions? && can?(:manage, energy_tariff) %>
            <%= link_to energy_tariff.name, energy_tariffs_path(energy_tariff) %>
          <% else %>
            <%= energy_tariff.name %>
          <% end %>
        </td>
        <td class="text-left">
          <%= flat_rate_label(energy_tariff) %>
        </td>
        <% if show_prices? %>
          <td class="text-right">
            <ul style="list-style: none; padding-left: 0px;">
              <% energy_tariff.energy_tariff_prices.order(start_time: :asc).each do |price| %>
                <li>
                  <% if energy_tariff.differential? %>
                    <%= t('schools.user_tariffs.tariff_partial.price_from_to', price_start_time: start_time(price),
                                                                               price_end_time: end_time(price)) %>
                  <% end %>
                  <%= t('schools.user_tariffs.rates_table.price_per_kwh',
                        price_value: convert_value_to_long_currency(price.value)) %>
                </li>
              <% end %>
            </ul>
          </td>
        <% end %>
        <% if show_meters? %>
          <td class="text-left">
            <% if energy_tariff.meters.any? %>
              <ul style="list-style: none; padding-left: 0px;">
                <% energy_tariff.meters.each do |meter| %>
                  <li><%= meter.mpan_mprn_and_name %></li>
                <% end %>
              </ul>
            <% else %>
                <%= t('schools.user_tariffs.meters.all_meters', meter_type: energy_tariff.meter_type) %>
            <% end %>
          </td>
        <% end %>
        <% if show_actions? %>
          <td class="text-right">
            <% if can?(:manage, energy_tariff) %>
              <% if energy_tariff.dcc? %>
                <%= link_to t('schools.user_tariff_charges.edit_charges'),
                            energy_tariffs_path(energy_tariff, [:energy_tariff_charges]),
                            class: 'btn btn-sm' %>
              <% else %>
                <%= link_to energy_tariff.usable? ? t('common.labels.edit') : t('common.labels.finish'),
                            energy_tariffs_path(energy_tariff),
                            class: 'btn btn-sm' %>
              <% end %>
              <% if can_toggle_status?(energy_tariff) %>
                <%= link_to energy_tariff.enabled ? t('common.labels.disable') : t('common.labels.enable'),
                            energy_tariffs_path(energy_tariff, [], { action: :toggle_enabled }),
                            class: 'btn btn-sm',
                            id: "energy-tariff-#{energy_tariff.id}-toggle",
                            remote: true, method: :post, data: { confirm: t('common.confirm') } %>
                <% if !energy_tariff.dcc? %>
                  <%= link_to t('common.labels.delete'),
                              energy_tariffs_path(energy_tariff),
                              method: :delete,
                              data: { confirm: t('common.confirm') },
                              class: 'btn btn-sm btn-danger',
                              style: energy_tariff.enabled ? 'display: none' : '',
                              id: "energy-tariff-#{energy_tariff.id}-delete" %>
                <% end %>
              <% elsif can_delete?(energy_tariff) %>
                <%= link_to t('common.labels.delete'),
                            energy_tariffs_path(energy_tariff),
                            method: :delete,
                            data: { confirm: t('common.confirm') },
                            class: 'btn btn-sm btn-danger',
                            id: "energy-tariff-#{energy_tariff.id}-delete" %>
              <% end %>
            <% end %>
          </td>
        <% end %>
      </tr>
    <% end %>
  </tbody>
</table>