ikuseiGmbH/smart-village-app-cms

View on GitHub
app/views/shared/partials/_opening_hours_form.html.erb

Summary

Maintainability
Test Coverage
<% fields = ["date_from", "date_to", "time_from", "time_to", "description", "weekday", "open"] %>
<% requireds ||= [] %>
<% labels ||= {
     date_from: "Startdatum",
     date_to: "Enddatum",
     time_from: "Startzeit",
     time_to: "Endzeit"
   } %>
<% list_of_opening_hours = record.opening_hours.presence || [OpenStruct.new] %>

<div class="row">
  <div class="col">
    <h3 class="d-sm-flex align-items-center justify-content-between my-4">
      Öffnungszeiten
    </h3>
  </div>
</div>

<div id="nested-opening-hours">
  <% list_of_opening_hours.each_with_index do |opening_hour, index| %>
    <%= fields_for "#{record_type}[opening_hours][#{index}]", opening_hour do |foh| %>
      <div class="nested-opening-hour-form<%= " d-none" if empty?(opening_hour, fields) %>">
        <div class="card mb-4">
          <div class="d-flex flex-wrap justify-content-between card-header py-3 bg-dark text-white">
            <h4>
              Öffnungszeit
            </h4>
            <%= link_to "#", class: "remove btn btn-sm btn-danger" do %>
              <i class="fa fa-trash text-white"></i>
            <% end %>
          </div>
          <div class="card-body">
            <div class="row">
              <div class="col-lg-6">
                <div class="form-group">
                  <% if labels[:date_from] %>
                    <%= foh.label :date_from do %>
                      <%= labels[:date_from] %>
                      <% if requireds.include?("date_from") %>
                        *
                      <% end %>
                    <% end %>
                  <% end %>
                  <%= foh.date_field(
                        :date_from,
                        required: requireds.include?("date_from"),
                        class: "form-control",
                        value: foh.object.date_from,
                        max: (Time.now + 50.years).end_of_year.to_date.to_s,
                        pattern: "\d{4}-\d{2}-\d{2}"
                      ) %>
                </div>
              </div>
              <div class="col-lg-6">
                <div class="form-group">
                  <% if labels[:date_to] %>
                    <%= foh.label :date_to do %>
                      <%= labels[:date_to] %>
                      <% if requireds.include?("date_to") %>
                        *
                      <% end %>
                    <% end %>
                  <% end %>
                  <%= foh.date_field(
                        :date_to,
                        required: requireds.include?("date_to"),
                        class: "form-control",
                        value: foh.object.date_to,
                        max: (Time.now + 50.years).end_of_year.to_date.to_s,
                        pattern: "\d{4}-\d{2}-\d{2}"
                      ) %>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="col-12 col-sm-6">
                <div class="form-group">
                  <% if labels[:time_from] %>
                    <%= foh.label :time_from do %>
                      <%= labels[:time_from] %>
                      <% if requireds.include?("time_from") %>
                        *
                      <% end %>
                    <% end %>
                  <% end %>
                  <%= foh.time_field(
                        :time_from,
                        required: requireds.include?("time_from"),
                        class: "form-control",
                        value: foh.object.time_from,
                        pattern: "[0-9]{2}:[0-9]{2}"
                      ) %>
                </div>
              </div>
              <div class="col-12 col-sm-6">
                <div class="form-group">
                  <% if labels[:time_to] %>
                    <%= foh.label :time_to do %>
                      <%= labels[:time_to] %>
                      <% if requireds.include?("time_to") %>
                        *
                      <% end %>
                    <% end %>
                  <% end %>
                  <%= foh.time_field(
                        :time_to,
                        required: requireds.include?("time_to"),
                        class: "form-control",
                        value: foh.object.time_to ,
                        pattern: "[0-9]{2}:[0-9]{2}"
                      ) %>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="col-12 col-sm-6">
                <div class="form-group">
                  <%= foh.label :description, "Beschreibung" %>
                  <%= foh.text_field :description, class: "form-control" %>
                </div>
              </div>
              <div class="col-12 col-sm-6">
                <div class="form-group">
                  <%= foh.label :weekday, "Wochentag" %>
                  <%= foh.select(
                        :weekday,
                        options_for_select(
                          [
                            "Montag",
                            "Dienstag",
                            "Mittwoch",
                            "Donnerstag",
                            "Freitag",
                            "Samstag",
                            "Sonntag"
                          ],
                          foh.object.weekday
                        ),
                        { include_blank: true },
                        { class: "form-control"  }
                      ) %>
                </div>
              </div>
            </div>

            <div class="row">
              <div class="col-12 col-sm-6">
                <div class="form-group">
                  <%= foh.hidden_field(
                        :open,
                        value: false,
                        id: nil
                      ) %>
                  <%= foh.check_box(
                        :open,
                        { checked: foh.object.open.nil? ? true : foh.object.open },
                        true,
                        false
                      ) %>
                  <%= foh.label :open, "geöffnet?" %>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
    <% end %>
  <% end %>
</div>

<%= link_to "#", class: "btn btn-sm btn-secondary", id: "nested-add-opening-hour" do %>
  <i class="fas fa-plus text-white mr-2"></i>
  Öffnungszeit hinzufügen
<% end %>

<hr />