noesya/osuny

View on GitHub
app/views/admin/communication/websites/menus/items/_form.html.erb

Summary

Maintainability
Test Coverage
<%= simple_form_for item, url: item.persisted?  ? [:admin, item, { menu_id: @menu.id }]
                                                : admin_communication_website_menu_items_path(@menu) do |f| %>
  <%= f.error_notification %>
  <%= f.error_notification message: f.object.errors[:base].to_sentence if f.object.errors[:base].present? %>

  <%= f.input :kind,
              as: :hidden,
              input_html: {
                data: {
                  url: kind_switch_admin_communication_website_menu_items_path
                }
              } %>
  <div>
    <label class="form-label required">
      <%= Communication::Website::Menu::Item.human_attribute_name('kind') %>
      <abbr title="required">*</abbr>
    </label>
    <div class="row g-3 mb-4">
      <% @website.menu_item_kinds.keys.each do |kind| %>
        <div class="col-6 col-lg-4 col-xl-3 col-xxl-2">
          <div class="card kind" data-kind="<%= kind %>">
            <div class="card-body">
              <%= osuny_label t("enums.communication.website.menu.item.kind.#{kind}") %>
              <p class="text-end mb-0">
                <i class="<%= Communication::Website::Menu::Item.icon_for(kind) %>"></i><br>
              </p>
              <a class="stretched-link"></a>
            </div>
          </div>
        </div>
      <% end %>
    </div>
  </div>
  <div class="row pure__row--small">
    <div class="col-lg-6">
      <%= f.input :title %>
      <%= f.association :parent,
                        collection: collection_tree(@item.menu.items, @item),
                        label_method: ->(p) { sanitize p[:label] },
                        value_method: ->(p) { p[:id] } %>

    </div>
    <div class="col-lg-6">
      <div data-kind-if="url">
        <%= f.input :url, as: :string %>
      </div>
      <div class="<%= 'd-none' unless item.has_about? %> js-about-fields">
        <%
          if item.has_about?
            about_collection = Communication::Website::Menu::Item.collection_for(item.kind, @website)
            with_tree = about_collection.first.respond_to?(:parent_id)
            helper_method = with_tree ? :osuny_collection_tree : :osuny_collection
            localized = about_collection.first.respond_to?(:localizable?)
            options_collection = public_send(helper_method, about_collection, localized: localized)
          else
            options_collection = []
          end
        %>
        <%= f.association :about,
                          collection: options_collection,
                          input_html: { class: 'js-about-id' } %>
        <%= f.input :about_type, as: :hidden, input_html: { class: 'js-about-type' } %>
      </div>
    </div>
  </div>
  <% content_for :action_bar_right do %>
    <%= submit f %>
  <% end %>
<% end %>