ikuseiGmbH/smart-village-app-cms

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

Summary

Maintainability
Test Coverage
<% fields ||= ["date_start", "date_end"] %>
<% force_visibility ||= false %>
<% requireds ||= [] %>
<% labels ||= {
     date_start: "Startdatum",
     date_end: "Enddatum",
     time_start: "Startzeit",
     time_end: "Endzeit"
   } %>
<% values ||= {} %>
<% with_removal ||= false %>
<% list_of_dates = record.dates.presence || [OpenStruct.new] %>

<%#
  response options of a survey can have just one `date`,
  so we need to check for that and build a dates array
%>
<% if record.try(:date).present? %>
  <% list_of_dates = [record.date] %>
<% end %>

<%#
  if we have an event with recurring dates, we just want to show the first date, because
  the others are generated and are not allowed to be edited
%>
<% if record.try(:recurring?) && record.dates.present? %>
  <% list_of_dates = [record.dates.first] %>
<% end %>

<div id="nested-dates">
  <% list_of_dates.each_with_index do |date, index| %>
    <%= form.fields_for "dates[#{index}]", date do |fd| %>
      <div class="nested-date-form<%= " d-none" if !force_visibility && empty?(date, fields) %>">
        <div class="row">
          <% if fields.include?("date_start") %>
            <div class="col-12 col-sm-6">
              <div class="form-group">
                <% if labels[:date_start] %>
                  <%= fd.label :date_start do %>
                    <%= labels[:date_start] %>
                    <% if requireds.include?("date_start") %>
                      *
                    <% end %>
                  <% end %>
                <% end %>
                <%= fd.date_field(
                      :date_start,
                      required: requireds.include?("date_start"),
                      class: "form-control",
                      value: fd.object.date_start || values[:date_start].presence,
                      max: (Time.now + 50.years).end_of_year.to_date.to_s,
                      pattern: "\d{4}-\d{2}-\d{2}"
                    ) %>
              </div>
            </div>
          <% end %>
          <% if fields.include?("date_end") %>
            <div class="col-12 col-sm-6">
              <div class="form-group">
                <% if labels[:date_end] %>
                  <%= fd.label :date_end do %>
                    <%= labels[:date_end] %>
                    <% if requireds.include?("date_end") %>
                      *
                    <% end %>
                  <% end %>
                <% end %>
                <%= fd.date_field(
                      :date_end,
                      required: requireds.include?("date_end"),
                      class: "form-control",
                      value: fd.object.date_end || values[:date_end].presence,
                      max: (Time.now + 50.years).end_of_year.to_date.to_s,
                      pattern: "\d{4}-\d{2}-\d{2}"
                    ) %>
              </div>
            </div>
          <% end %>
        </div>

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

        <% if with_removal %>
          <%= link_to "#", class: "remove btn btn-sm btn-danger mb-4" do %>
            <i class="fa fa-trash text-white mr-2"></i>
            Termin entfernen
          <% end %>
        <% end %>
      </div>
    <% end %>
  <% end %>
</div>