Rails: Generate table based on data containing dates

0

I am working on an application in RoR to keep track of maintenance services performed on various machines.

To try to simplify a bit, let's say we have two models, on the one hand machines and other services, so that service belongs_to machine and machine has_many services. These services are defined based on the date in which they must be performed, the machine in which they are carried out and the status (to be done, not OK, etc.). My idea is, as a main screen, to show a summary of services that must be carried out, such as a table of this type, where the months are shown in the header of the table and in each "cell" the day of the month is indicated that a certain Service must be carried out for a certain machine. It would be something like this:

Machine A has a scheduled service on January 25, the B on March 3 and so on. Any idea how I can do this correctly?

Thank you.

    
asked by Ernesto G 18.09.2016 в 18:19
source

3 answers

0

According to my understanding, in your database you have something like the following:

Table Name: Services
machine | date_service(type date)
   A    |   23/01/2017
   B    |   03/03/2017
   C    |   25/02/2017
   B    |   14/07/2017

In the controller you should have the show method like this:

def show
  @services_a = Service.where(:machine => "A").reorder(:date_service)
  @services_b = Service.where(:machine => "B").reorder(:date_service)
  @services_c = Service.where(:machine => "C").reorder(:date_service)
  ...
end

and in the view:

<h1>Listing Services</h1>

<table>
  <tr>
    <th>Maquina</th>
    <th>Enero</th>
    <th>Febrero</th>
    <th>Marzo</th>
    <th>Abril</th>
    ...
  </tr>

#A cycle of these for each machine you have

<% @services_a.each do |service| %>
    <% if service.date_service.strftime("%m") == "01" %>
      <tr><td><%= service.date_service %></td>
    <% else %>
      <tr><td>&nbsp;</td>
    <% end %>
    <% if service.date_service.strftime("%m") == "02" %>
      <td><%= service.date_service %></td>
    <% else %>
      <td>&nbsp;</td>
    <% end %>
    <% if service.date_service.strftime("%m") == "03" %>
      <td><%= service.date_service %></td>
    <% else %>
      <td>&nbsp;</td>
    <% end %>
    <% if service.date_service.strftime("%m") == "04" %>
      <td><%= service.date_service %></td>
    <% else %>
      <td>&nbsp;</td>
    <% end %>
    ...
    <% if service.date_service.strftime("%m") == "04" %>
      <td><%= service.date_service %></td></tr>
    <% else %>
      <td>&nbsp;</td></tr>
    <% end %>
<% end %>

<% @services_b.each do |service| %>
    <% if service.date_service.strftime("%m") == "01" %>
      <tr><td><%= service.date_service %></td>
    <% else %>
      <tr><td>&nbsp;</td>
    <% end %>
    <% if service.date_service.strftime("%m") == "02" %>
      <td><%= service.date_service %></td>
    <% else %>
      <td>&nbsp;</td>
    <% end %>
    <% if service.date_service.strftime("%m") == "03" %>
      <td><%= service.date_service %></td>
    <% else %>
      <td>&nbsp;</td>
    <% end %>
    <% if service.date_service.strftime("%m") == "04" %>
      <td><%= service.date_service %></td>
    <% else %>
      <td>&nbsp;</td>
    <% end %>
    ...
    <% if service.date_service.strftime("%m") == "04" %>
      <td><%= service.date_service %></td></tr>
    <% else %>
      <td>&nbsp;</td></tr>
    <% end %>
<% end %>

</table>

Although the code lacks opotimization, it seems to work, and if it fails then that is the idea.

I hope I really serve you.

    
answered by 16.12.2016 / 18:27
source
0

I do not understand your question well, what do you mean correctly? If the user interface you propose is right ?, If you need help with how to do it in RoR? ...

The user interface is just going to depend on your users ... that is why you should ask the users.

    
answered by 18.09.2016 в 18:23
0

I would do a method in the machine class that given a month tell what services are for that month ....

class Maquina
    def servicios_por_mes(mes, anho)

        proximo_mes = (mes == 12) ? 1 : mes+1
        proximo_anho = (mes == 12) ? anho+1 : anho

        Servicios.where(maquina_id: maquina_id)
            .where(["fecha_servicio >= ?", "#{anho}-#{mes}-01"])
            .where(["fecha_servicio < ?", "#{proximo_anho}-#{proximo_mes}-01"])
            .count
    end
end

Then I use that in the view ... and two cycles are done one per machine and one per month ...

    
answered by 18.09.2016 в 20:01