From 1ab09b41bde0593afadca8d6e4309eb3f6f62e76 Mon Sep 17 00:00:00 2001 From: wvengen Date: Wed, 18 Dec 2013 17:34:45 +0100 Subject: [PATCH] fix articles_for_select2 helper --- app/helpers/deliveries_helper.rb | 13 +++++++++---- app/helpers/finance/receive_helper.rb | 9 --------- app/views/deliveries/_form.html.haml | 6 +++--- app/views/finance/receive/_edit_articles.html.haml | 3 ++- 4 files changed, 14 insertions(+), 17 deletions(-) diff --git a/app/helpers/deliveries_helper.rb b/app/helpers/deliveries_helper.rb index 2ef5d14b..03a8f3ec 100644 --- a/app/helpers/deliveries_helper.rb +++ b/app/helpers/deliveries_helper.rb @@ -10,12 +10,17 @@ module DeliveriesHelper end end - def articles_for_select2(supplier) - supplier.articles.undeleted.reorder('articles.name ASC').map {|a| {:id => a.id, :text => "#{a.name} (#{number_to_currency a.price}/#{a.unit})"} } + def articles_for_select2(articles, except = [], &block) + articles = articles.reorder('articles.name ASC') + articles.reject! {|a| not except.index(a.id).nil? } if except + block_given? or block = Proc.new {|a| "#{a.name} (#{number_to_currency a.price}/#{a.unit})" } + articles.map do |a| + {:id => a.id, :text => block.call(a)} + end end - def stock_articles_for_table(supplier) - supplier.stock_articles.undeleted.reorder('articles.name ASC') + def articles_for_table(articles) + articles.undeleted.reorder('articles.name ASC') end def stock_change_remove_link(stock_change_form) diff --git a/app/helpers/finance/receive_helper.rb b/app/helpers/finance/receive_helper.rb index 055337d7..fe844d08 100644 --- a/app/helpers/finance/receive_helper.rb +++ b/app/helpers/finance/receive_helper.rb @@ -1,12 +1,3 @@ # :encoding:utf-8: module Finance::ReceiveHelper - # TODO currently duplicate a bit of DeliveriesHelper.articles_for_select2 - # except is an array of article id's to omit - def articles_for_select2(supplier, except = []) - articles = supplier.articles.reorder('articles.name ASC') - articles.reject! {|a| not except.index(a.id).nil? } if except - articles.map do |a| - {:id => a.id, :text => "#{a.name} (#{a.unit_quantity}⨯#{a.unit})"} - end - end end diff --git a/app/views/deliveries/_form.html.haml b/app/views/deliveries/_form.html.haml index 40337dde..8e2d7f75 100644 --- a/app/views/deliveries/_form.html.haml +++ b/app/views/deliveries/_form.html.haml @@ -20,7 +20,7 @@ $('#new_stock_article').removeAttr('disabled').select2({ placeholder: '#{t '.create_stock_article'}', - data: #{articles_for_select2(@supplier).to_json}, + data: #{articles_for_select2(@supplier.articles).to_json}, createSearchChoice: function(term) { return { id: 'new', @@ -115,12 +115,12 @@ %tfoot %tr %th{:colspan => 5} - - if articles_for_select2(@supplier).empty? + - if @supplier.articles.empty? = link_to t('.create_stock_article'), new_stock_article_path, :remote => true, :class => 'btn' - else %input#new_stock_article{:style => 'width: 500px;'} %tbody - - for article in stock_articles_for_table(@supplier) + - for article in articles_for_table(@supplier.stock_articles) = render :partial => 'stock_article_for_adding', :locals => {:article => article} %h2= t '.title_fill_quantities' diff --git a/app/views/finance/receive/_edit_articles.html.haml b/app/views/finance/receive/_edit_articles.html.haml index b1b32eab..7e99d1be 100644 --- a/app/views/finance/receive/_edit_articles.html.haml +++ b/app/views/finance/receive/_edit_articles.html.haml @@ -1,7 +1,8 @@ +- new_article_data = articles_for_select2(@order.articles, @order_articles.map(&:article_id)) {|a| "#{a.name} (#{a.unit_quantity}⨯#{a.unit})" } - content_for :javascript do :javascript - var new_article_data = #{articles_for_select2(@order, @order_articles.map(&:article_id)).to_json}; + var new_article_data = #{new_article_data.to_json}; function update_delta(input) { var units = $(input).val();