0

I would like to pair the open-close hours of each store by the day. Fore example if Wednesday has two different open-close hours I would like to show them together under the Wednesday. Currently I'm doing the following which is returning everything separately... if Wednesday has two open-close hours the open-close hours show separately as two Wednesdays. Any idea on how to implement this?

@open_hours = OpenHour.where(store_id: params[:store_id]).order('day ASC')

    <% @open_hours.each do |open| %>
      <% if open.day == 1 %>
        <p><strong>Monday:</strong>
          <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %></p>
      <% end %>
      <% if open.day == 2 %>
        <p><strong>Tuesday:</strong>
          <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %></p>
      <% end %>
      <% if open.day == 3 %>
        <p><strong>Wednesday:</strong>
          <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %></p>
      <% end %>
      <% if open.day == 4 %>
        <p><strong>Thursday:</strong>
          <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %></p>
      <% end %>
      <% if open.day == 5 %>
        <p><strong>Friday:</strong>
          <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %></p>
      <% end %>
      <% if open.day == 6 %>
        <p><strong>Saturday:</strong>
          <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %></p>
        <% end %>
      <% if open.day == 0 %>
        <p><strong>Sunday:</strong>
          <%= I18n.l open.opens, :format => :custom %> -<%= I18n.l open.closes, :format => :custom %></p>
        <% end %>
    <% end %>

1 Answer 1

1

I assume the @open_hours are sorted in day order. Test to see if the previous open was for the same day, and if so skip the day name. Use a table to get alignment.

<% last_day = nil %>
<% days = %w(Sunday Monday Tuesday Wednesday Thursday Friday Saturday) $>
<table>
  <% @open_hours.each do |open| %>
    <tr>
      <td>
        <%= days[open.day] if open.day != last_day %>
        <% last_day = open.day%>
      </td>
      <td>
        <%= I18n.l open.opens, :format => :custom %>-<%= I18n.l open.closes, :format => :custom %>
      </td>
    </tr>
  <% end %>
</table>
Sign up to request clarification or add additional context in comments.

1 Comment

You can use Date::DAYNAMES instead of days.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.