From f8fca4d630f8b889284680533fea65d2f3e4ceeb Mon Sep 17 00:00:00 2001 From: wvengen Date: Mon, 24 Feb 2014 11:37:06 +0100 Subject: [PATCH] fix order article add + test --- app/helpers/order_articles_helper.rb | 19 ++++--------------- app/models/order.rb | 8 ++++++++ app/views/order_articles/_new.html.haml | 2 +- spec/integration/balancing_spec.rb | 15 +++++++++++++++ 4 files changed, 28 insertions(+), 16 deletions(-) diff --git a/app/helpers/order_articles_helper.rb b/app/helpers/order_articles_helper.rb index 6685b684..c2dc232f 100644 --- a/app/helpers/order_articles_helper.rb +++ b/app/helpers/order_articles_helper.rb @@ -1,19 +1,8 @@ module OrderArticlesHelper - def new_order_articles_collection(&block) - if @order.stockit? - articles = StockArticle.undeleted.reorder('articles.name') - else - articles = @order.supplier.articles.undeleted.reorder('articles.name') - end - unless block_given? - block = Proc.new do |a| - pkg_info = pkg_helper(a, plain: true) - a.name + ' ' + - "(#{a.unit}" + - (pkg_info.blank? ? '' : " #{pkg_info}") + ")" - end - end - articles.map {|a| block.call(a)} + def article_label_with_unit(article) + pkg_info = pkg_helper(article, plain: true) + "#{article.name} (#{[article.unit, pkg_info].reject(&:blank?).join(' ')})" end + end diff --git a/app/models/order.rb b/app/models/order.rb index feb913ee..99ccaaf9 100644 --- a/app/models/order.rb +++ b/app/models/order.rb @@ -52,6 +52,14 @@ class Order < ActiveRecord::Base end end + def supplier_articles + if stockit? + StockArticle.undeleted.reorder('articles.name') + else + supplier.articles.undeleted.reorder('articles.name') + end + end + # Save ids, and create/delete order_articles after successfully saved the order def article_ids=(ids) @article_ids = ids diff --git a/app/views/order_articles/_new.html.haml b/app/views/order_articles/_new.html.haml index c0d501ab..0f513a06 100644 --- a/app/views/order_articles/_new.html.haml +++ b/app/views/order_articles/_new.html.haml @@ -3,7 +3,7 @@ = link_to t('ui.marks.close').html_safe, '#', class: 'close', data: {dismiss: 'modal'} %h3= t '.title' .modal-body - = form.association :article, collection: new_order_articles_collection + = form.association :article, collection: @order.supplier_articles, label_method: lambda {|a| article_label_with_unit(a)} .modal-footer = link_to t('ui.close'), '#', class: 'btn', data: {dismiss: 'modal'} = form.submit class: 'btn btn-primary' diff --git a/spec/integration/balancing_spec.rb b/spec/integration/balancing_spec.rb index 05c7bfd3..c5aa3bdc 100644 --- a/spec/integration/balancing_spec.rb +++ b/spec/integration/balancing_spec.rb @@ -133,6 +133,7 @@ describe 'settling an order', :type => :feature do fill_in 'group_order_article_result', :with => 8 find('input[type="submit"]').click end + expect(page).to_not have_selector('form#new_group_order_article') expect(page).to have_content(user.ordergroup.name) goa = GroupOrderArticle.last expect(goa).to_not be_nil @@ -163,6 +164,20 @@ describe 'settling an order', :type => :feature do expect(find("#group_order_articles_#{oa.id} tfoot td:nth-child(3)").text.to_f).to eq 8 end + it 'can add an article' do + new_article = create :article, supplier: supplier + expect(page).to_not have_content(new_article.name) + click_link I18n.t('finance.balancing.edit_results_by_articles.add_article') + expect(page).to have_selector('form#new_order_article') + within('#new_order_article') do + select new_article.name, :from => 'order_article_article_id' + find('input[type="submit"]').click + end + expect(page).to_not have_selector('form#new_order_article') + expect(page).to have_content(new_article.name) + expect(order.order_articles.where(article_id: new_article.id)).to_not be nil + end + end end