UNC-Libraries/hy-c

View on GitHub
app/views/records/edit_fields/_person.html.erb

Summary

Maintainability
Test Coverage
<% person_term_display = "#{person_term.split('_').map(&:capitalize).join(' ')} #1" %>
<% hidden_index = current_user&.admin? ? '' : 'hidden' %>
<hr class="person-divider">
<div class="form-group multi_value">
  <div id="<%= person_term %>">
    <%# sort people by index; can remove index portion of conditional when all people have index values %>
    <% sorted_values = curation_concern[person_key] %>
    <% if !curation_concern[person_key].blank? && !curation_concern[person_key].first.index.blank? %>
       <% sorted_values = curation_concern[person_key].sort_by{|person| person.index.first.to_i} %>
    <% end %>

    <%= f.fields_for person_key, sorted_values do |person| %>
    <div class="<%= person_term %> row card card-body bg-light field-wrapper person-object">
      <div class="person col-sm-12 <%= person_term %>-name">
        <%= person.input :name,
                         label: person_term_display,
                         input_html: { placeholder: 'Smith, John',
                                        class: "form-control #{person_term} #{person_term}-name",
                                        value: (person.object.name.first || '') },
                         required: (f.object.required?(person_term.to_sym)) %>
      </div>

      <div class="person col-sm-12 <%= person_term %>-affiliation">
        <% is_required = person_term == 'creator' && f.object_name == 'honors_thesis' %>
        <%# Replace old value of UNC Libraries to avoid duplicate key in the yaml file %>
        <% aff_val = person.object.affiliation.first.to_s %>
        <%= person.input :affiliation, as: :select,
                         label: "UNC affiliation (#{person_term_display})",
                         collection: DepartmentsService.select_all_options.sort,
                         include_blank: true,
                         required: is_required,
                         selected: aff_val,
                         item_helper: DepartmentsService.method(:include_current_value),
                         input_html: { class: 'form-control' }
        -%>
      </div>

      <div class="person col-sm-12 <%= person_term %>-other-affiliation">
        <%= person.input :other_affiliation,
                         label: "Additional affiliation (#{person_term_display})",
                         input_html: { class: 'form-control',
                                                            value: (person.object.other_affiliation.first || '') } %>
      </div>

      <div class="person col-sm-12 <%= person_term %>-orcid">
        <%= person.input :orcid,
                         label: "ORCID (#{person_term_display})",
                         input_html: { placeholder: 'https://orcid.org/xxxx-xxxx-xxxx-xxxx',
                                       class: 'form-control',
                                       value: (person.object.orcid.first || '') } %>
      </div>

      <div class="person col-sm-12 <%= person_term %>-index <%= hidden_index %>">
        <%= person.input :index,
                         label: "Index (#{person_term_display})",
                         input_html: { class: 'form-control',
                                       value: (person.object.index.first || '')} %>
      </div>

      <% if @form.instance_eval(person_key.to_s).count > 1 && person.index > 0 %>
      <span class="people-remove field-controls multi_value">
        <button type="button" class="btn btn-link remove remove-<%= person_term %>">
          <span class="fa fa-remove"></span> <span class="controls-remove-text">Remove <%= person_term.split('_').map(&:capitalize).join(' ') %></span>
        </button>
      </span>
      <% end %>
    </div>
  <% end %>

  <div class="<%= person_term %> row cloning d-none field-wrapper" id='<%= person_term %>-cloning_row'>
    <%= f.fields_for person_key do |person| %>
      <% if person.index <= @form.instance_eval(person_key.to_s).count %>
        <div class="<%= person_term %> row card card-body bg-light">
          <div class="col-sm-12 <%= person_term %>-name">
            <%= person.input :name,
                             label: person_term_display,
                             input_html: { placeholder: 'Smith, John',
                                           class: "form-control #{person_term} #{person_term}-name",
                                           value: '' },
                             required: (f.object.required?(person_term.to_sym)) %>
          </div>

          <div class="col-sm-12 <%= person_term %>-affiliation">
            <% is_required = (person_term == 'advisor' && f.object_name == 'honors_thesis') ? false : f.object.required?(:affiliation) %>
            <%= person.input :affiliation, as: :select,
                             label: "UNC affiliation (#{person_term_display})",
                             collection: DepartmentsService.select_all_options.sort,
                             include_blank: true,
                             required: is_required,
                             selected: '',
                             item_helper: DepartmentsService.method(:include_current_value),
                             input_html: { class: 'form-control' }
            -%>
          </div>

          <div class="col-sm-12 <%= person_term %>-other-affiliation">
            <%= person.input :other_affiliation,
                             label: "Additional affiliation (#{person_term_display})",
                             input_html: { class: 'form-control', value: '' } %>
          </div>

          <div class="col-sm-12 <%= person_term %>-orcid">
            <%= person.input :orcid,
                             label: "ORCID (#{person_term_display})",
                             input_html: { placeholder: 'https://orcid.org/xxxx-xxxx-xxxx-xxxx',
                                           class: 'form-control',
                                           value: '' } %>
          </div>

          <div class="person col-sm-12 <%= person_term %>-index <%= hidden_index %>">
            <%= person.input :index,
                             label: "Index (#{person_term_display})",
                             input_html: { class: 'form-control', value: ''} %>
          </div>

          <span class="people-remove field-controls multi_value">

          <button type="button" class="btn btn-link remove remove-<%= person_term %>">
            <span class="fa fa-remove"></span> <span class="controls-remove-text">Remove <%= person_term.split('_').map(&:capitalize).join(' ') %></span>
          </button>
        </span>
        </div>
      <% end %>
    <% end %>
  </div>
  </div>

  <button id="add-another-<%= person_term %>" class="btn btn-link add">
    <span class="fa fa-plus"></span>
    <span>Add another <%= person_term.split('_').map(&:capitalize).join(' ') %></span>
  </button>
</div>

<hr class="person-divider">