fix articles_for_select2 helper

This commit is contained in:
wvengen 2013-12-18 17:34:45 +01:00
parent 2b511ee559
commit 1ab09b41bd
4 changed files with 14 additions and 17 deletions

View file

@ -10,12 +10,17 @@ module DeliveriesHelper
end end
end end
def articles_for_select2(supplier) def articles_for_select2(articles, except = [], &block)
supplier.articles.undeleted.reorder('articles.name ASC').map {|a| {:id => a.id, :text => "#{a.name} (#{number_to_currency a.price}/#{a.unit})"} } 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 end
def stock_articles_for_table(supplier) def articles_for_table(articles)
supplier.stock_articles.undeleted.reorder('articles.name ASC') articles.undeleted.reorder('articles.name ASC')
end end
def stock_change_remove_link(stock_change_form) def stock_change_remove_link(stock_change_form)

View file

@ -1,12 +1,3 @@
# :encoding:utf-8: # :encoding:utf-8:
module Finance::ReceiveHelper 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 end

View file

@ -20,7 +20,7 @@
$('#new_stock_article').removeAttr('disabled').select2({ $('#new_stock_article').removeAttr('disabled').select2({
placeholder: '#{t '.create_stock_article'}', placeholder: '#{t '.create_stock_article'}',
data: #{articles_for_select2(@supplier).to_json}, data: #{articles_for_select2(@supplier.articles).to_json},
createSearchChoice: function(term) { createSearchChoice: function(term) {
return { return {
id: 'new', id: 'new',
@ -115,12 +115,12 @@
%tfoot %tfoot
%tr %tr
%th{:colspan => 5} %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' = link_to t('.create_stock_article'), new_stock_article_path, :remote => true, :class => 'btn'
- else - else
%input#new_stock_article{:style => 'width: 500px;'} %input#new_stock_article{:style => 'width: 500px;'}
%tbody %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} = render :partial => 'stock_article_for_adding', :locals => {:article => article}
%h2= t '.title_fill_quantities' %h2= t '.title_fill_quantities'

View file

@ -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 - content_for :javascript do
:javascript :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) { function update_delta(input) {
var units = $(input).val(); var units = $(input).val();