foodcoop-adam/foodsoft

View on GitHub
app/views/articles/index.html.haml

Summary

Maintainability
Test Coverage
- title t('.title', supplier: @supplier.name, count: @supplier.articles.undeleted.count)

- content_for :actionbar do
  .btn-group
    = link_to '#', data: {toggle: 'dropdown'}, class: 'btn dropdown-toggle' do
      = t '.change_supplier'
      %span.caret
    %ul.dropdown-menu
      - Supplier.undeleted.where('id != ?', @supplier.id).order('suppliers.name ASC').each do |supplier|
        %li= link_to supplier.name, supplier_articles_path(supplier), tabindex: -1

.well.well-small
  .btn-toolbar
    = form_tag supplier_articles_path(@supplier), method: :get, remote: true, class: 'form-search pull-right',
      'data-submit-onchange' => true do
      = text_field_tag :query, params[:query], class: 'input-medium search-query',
        placeholder: t('.search_placeholder')

    - if @supplier.shared_supplier
      .btn-group
        - if @supplier.shared_sync_method == 'import'
          = link_to t('.ext_db.import'), "#import", 'data-toggle-this' => '#import', class: 'btn btn-primary'
        = link_to t('.ext_db.sync'), sync_supplier_articles_path(@supplier), method: :post, class: 'btn btn-success'

    .btn-group
      = link_to t('.new'), new_supplier_article_path(@supplier), remote: true, class: "btn #{'btn-primary' unless @supplier.shared_supplier}"
      = link_to t('.edit_all'), edit_all_supplier_articles_path(@supplier), class: 'btn'
      = link_to t('.upload'), upload_supplier_articles_path(@supplier), class: 'btn'
      - if current_user.role_orders?
        = link_to t('.new_order'), new_order_path(supplier_id: @supplier), class: 'btn'

- unless @supplier.shared_supplier.nil?
  #import.well.well-small(style="display:none;")
    = form_tag shared_supplier_articles_path(@supplier), method: :get, remote: true, class: 'form-search',
      'data-submit-onchange' => true do
      %h3{style: 'display: inline; vertical-align: middle; margin-right: 1em;'}= t('.import.title') + ' '
      = text_field_tag "search[name_contains_all]", "", class: 'input-medium search-query',
        placeholder: t('.import.placeholder')
      -#%label.checkbox
        = check_box_tag "search[origin_equals]", "REG", false
        = t '.import.restrict_region'
      .pull-right{style: 'line-height: 40px'}
        = label_tag :article_category_id, t('.import.category')
        = collection_select :article_category, :id, ArticleCategory.all, :id, :name,
          {include_blank: true}, {'data-ignore-onchange' => true}
      #search_results.clearfix
    = link_to t('ui.close'), "#import", 'data-toggle-this' => '#import', class: 'btn'

= form_tag update_selected_supplier_articles_path(@supplier), id: "articlesInListForm",
  'data-submit-onchange' => true do
  #table= render 'articles'

- content_for :javascript do
  :javascript
    // keep import button pressed when import section is shown
    $(document).on('touchclick', 'a[data-toggle-this=#import]', function() {
      var state = $('#import').is(':visible');
      $('.btn-toolbar a[data-toggle-this=#import]').toggleClass('active', !state);
      if (!state) {
        // also load articles when shown
        $('#import form').submit();
        // select category when form is shown, to avoid errors
        $('#import #article_category_id').val('');
      }
    });
    // propagate category select to article import
    // XXX quite a url hack
    $(document).on('touchclick', '#import .actions a[href]', function() {
      var category_id = $('#import #article_category_id').children(':selected').val();
      var url = $(this).attr('href');
      url = url.replace(/(&article_category_id=[0-9]*)?$/, '&article_category_id='+category_id);
      $(this).attr('href', url);
    });