appirits/comable

View on GitHub
backend/app/views/comable/admin/products/_form.slim

Summary

Maintainability
Test Coverage
= error_messages_for @product

- url = comable.admin_products_path if @product.new_record?
- url ||= comable.admin_product_path(@product)

= form_for @product, url: url, html: { multipart: true } do |f|
  .hidden
    = f.submit

  fieldset
    .col-md-3
      legend
        = Comable.t('admin.general')
      .help-block

    .col-md-9
      .form-group
        = f.label :name
        = f.text_field :name

      .form-group
        = f.label :caption
        = f.text_area :caption

      / TODO: Refactoring
      javascript:
        comable_tagit_available_tags = #{raw Comable::Category.all.map(&:path).map { |path| path.map(&:name).join(' > ') }.sort.to_json};

      .form-group
        = f.label :categories
        ul#comable-tagit
          - @product.categories.each do |category|
            li
              = category.path.map(&:name).join(' > ')

  hr

  fieldset
    .col-md-3
      legend
        = Comable.t('admin.images')
      .help-block

    .col-md-9
      - images = @product.images
      - main_image = images.first || @product.images.build
      - sub_images = images - [main_image]

      .row
        .col-md-6
          = f.fields_for :images, main_image do |ff|
            label
              = Comable.t('admin.main_image')
            p
              = ff.file_field :file
            - if ff.object.url
              .thumbnail
                = image_tag ff.object.url
                - if ff.object.persisted?
                  label.remove
                    span> = ff.check_box :_destroy
                    span> = Comable.t('admin.actions.destroy')

        .col-md-6
          .row
            - sub_images.each.with_index(1) do |image, index|
              = f.fields_for :images, image do |ff|
                = render 'comable/admin/shared/images_fields', f: ff, index: index

            .col-md-6
              = link_to_add_fields Comable.t('admin.add_sub_image'), f, :images

  hr

  - if @product.new_record? || @product.master?
    = f.fields_for :variants, f.object.variants.first do |ff|
      = render 'comable/admin/shared/variant_form', f: ff
      = ff.hidden_field :_destroy, value: !f.object.master?
    hr

  - if @product.new_record? || @product.persisted? && !@product.master?
    fieldset
      .col-md-3
        legend
          = Comable.t('admin.variants')

      .col-md-9
        = f.fields_for :option_types, f.object.option_types do |ff|
          = render 'comable/admin/shared/option_types_fields', f: ff, readonly: @product.persisted?

        - if @product.new_record?
          = link_to_add_fields Comable.t('admin.add_variants'), f, :option_types, class: 'btn btn-link js-add-option'

          table.table.table-striped.js-variants-table class="#{'hidden' if @product.master?}"
            thead
              th
              th
                = Comable.t('admin.variants')
              th
                = @product.variants.human_attribute_name(:sku)
              th
                = @product.variants.human_attribute_name(:price)
              th
                = @product.variants.human_attribute_name(:quantity)
            tbody
              - if @product.persisted?
                = f.fields_for :variants do |ff|
                  = render 'comable/admin/shared/variants_fields', f: ff
          = link_to_add_fields nil, f, :variants, class: 'hidden js-add-variant'

        - if @product.persisted?
          table.table.table-striped
            thead
              th
                = @product.variants.human_attribute_name(:sku)
              th
                = @product.variants.human_attribute_name(:price)
              th
                = @product.variants.human_attribute_name(:quantity)
              th
                = Comable.t('admin.variants')
              th
                = Comable.t('admin.operation')
            tbody
              - @product.variants.each do |variant|
                tr
                  td
                    = variant.sku
                  td
                    = number_to_currency variant.price
                  td
                    - quantity = variant.quantity
                    strong class="#{(quantity <= 0) ? 'text-danger' : (quantity <= 10) ? 'text-warning' : 'text-success'}"
                      = number_with_delimiter quantity
                  td
                    - variant.options.each do |option|
                      span.comable-variant-name
                        = option.value
                  td
                    = link_to comable.admin_product_variant_path(@product, variant), class: 'btn btn-default'
                      i.fa.fa-edit>
                      = Comable.t('admin.actions.edit')

    hr

  fieldset
    .col-md-3
      legend
        = Comable.t('admin.nav.products.property')
      .help-block

    .col-md-9
      .form-group
        table.table#properties
          - @product.properties.each do |property|
            = render 'comable/admin/products/property_fields', property_key: property['property_key'], property_value: property['property_value']
        = f.hidden_field :property
        = add_property_button_tag Comable.t('admin.nav.products.add_property')

  hr

  fieldset
    .col-md-3
      legend
        = Comable.t('admin.nav.products.visibility')

    .col-md-9
      .form-group
        .radio-inline
          label
            = f.radio_button :published_at, :published, checked: @product.published_at.present?
            = Comable.t('admin.nav.products.published')
        .radio-inline
          label
            = f.radio_button :published_at, :unpublished, checked: @product.published_at.blank?
            = Comable.t('admin.nav.products.unpublished')
        = f.text_field :published_at, value: @product.published_at.try(:strftime, '%Y-%m-%d %H:%M'), class: 'datetimepicker'