Skip to content

Instantly share code, notes, and snippets.

@andyferra
Created May 3, 2010 11:22
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save andyferra/387986 to your computer and use it in GitHub Desktop.
Save andyferra/387986 to your computer and use it in GitHub Desktop.
format.json do
response = {}
if lot = @location.customer_product_lots.find_by_barcode(params[:barcode])
product = lot.customer_product
response['lotInfo'] = {
'domId' => "customer_product_lot_#{ lot.id }",
'lotNumber' => lot.lot_number,
'expirationDate' => lot.expires_on.strftime('%x'),
'expectedQuantity' => lot.units_on_hand,
'countedQuantity' => @physical_inventory.actual_count_for(lot),
'difference' => @physical_inventory.actual_difference_for(lot)
}
response['productInfo'] = {
'domId' => "customer_product_#{ lot.customer_product.id }",
'name' => "#{ product.full_name} #{ product.number_per_package } count"
}
end
if @physical_inventory.errors.any?
response['errors'] = @physical_inventory.errors.full_messages
end
render :text => response.to_json
end
$('form.edit_customer_physical_inventory').submit(function(e){
$.ajax({
type : 'POST',
url : $(this).attr('action'),
data : $(this).serialize(),
success : updateScannedInventory,
dataType : 'json'
});
return false;
});
function updateScannedInventory(response){
console.log(response);
if (response.errors != null){
$(errorMessages('customer physical inventory', response.errors))
.insertBefore('form.edit_customer_physical_inventory label:first');
}
if (response.lotInfo != null){
// if the product table doesn't exist...
if ($(response.productInfo.domId).length == 0){
// create the table
}
// if the lot row doesn't exist...
if ($(response.lotInfo.domId).length == 0){
// create the row
} else {
// just update the row
}
}
}
function errorMessages(name, errors){
output = '<div class="errorExplanation" id="errorExplanation">';
output += '<h2>'+ pluralize(errors.length, 'error', 'errors') +' prohibited this '+ name +' from being saved</h2>';
output += '<p>There were problems with the following fields:</p>';
output += listify(errors);
output += '</div>';
return output;
}
function pluralize(count, singular, plural){
if (count > 1) {
return count.toString() + ' ' + plural;
} else {
return '1 ' + singular;
}
}
function listify(items){
return '<ul>' + $.map(items, function(a){ return '<li>'+a+'</li>' }) + '</ul>';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment