Move some stuff to barcodecontroller where it belongs + refactoring

This commit is contained in:
benji 2015-09-28 21:51:40 +02:00
parent f35f8bec41
commit e6264fd6c8
8 changed files with 62 additions and 53 deletions

View file

@ -2,51 +2,17 @@
// All this logic will automatically be available in application.js.
// You can use CoffeeScript in this file: http://coffeescript.org/
ready = function() {
products_ordered = $('#product_search').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('[data-name]').hide();
$('[data-name]').filter(function () {
return rex.test($(this).data("name"));
}).show();
})
$('#products_modal').on('hidden.bs.modal', function () {
$('#product_search').val('');
});
increment_product = function(product_id) {
input = $("#current_order").find(".order_item_wrapper[data-product=" + product_id + "]").find("input[type=number]");
$(input).val(parseIntNaN($(input).val()) + 1).change();
}
$("#products_modal button").click(function() {
increment_product($(this).data("product"))
})
$("#from_barcode_form").on("ajax:before", function(xhr, settings) {
// Stuff you wanna do after sending form
}).on("ajax:success", function(data, status, xhr) {
if (status != null) {
increment_product(status["id"])
}
}).on("ajax:error", function(xhr, status, error) {
// Display an error or something, whatever
}).on("ajax:complete", function(xhr, status) {
$("#from_barcode_form")[0].reset();
// Do more stuff
})
/* INITIALIZE */
$('tr.order_item_wrapper').hide();
$('tr.order_item_wrapper').filter(function() {
return parseIntNaN($(this).find('[type=number]').val()) > 0;
}).show();
$('tr.order_item_wrapper input[type=number]').change(function() {
tr = $(this).closest('tr.order_item_wrapper')
$(tr).toggle(parseIntNaN($(this).val()) > 0);
$(tr).find("td").last().html(((parseIntNaN($(tr).data("price")) * parseIntNaN($(this).val())) / 100.0).toFixed(2))
recalculate();
})
/* HELPERS */
increment_product = function(product_id) {
input = $("#current_order").find(".order_item_wrapper[data-product=" + product_id + "]").find("input[type=number]");
$(input).val(parseIntNaN($(input).val()) + 1).change();
}
recalculate = function() {
/* Total Price */
@ -63,6 +29,47 @@ ready = function() {
}).length));
}
/* PRODUCT MODAL */
products_ordered = $('#product_search').keyup(function () {
var rex = new RegExp($(this).val(), 'i');
$('[data-name]').hide();
$('[data-name]').filter(function () {
return rex.test($(this).data("name"));
}).show();
})
$('#products_modal').on('hidden.bs.modal', function () {
$('#product_search').val('');
});
$("#products_modal button").click(function() {
increment_product($(this).data("product"))
})
/* BARCODE SCAN */
$("#from_barcode_form").submit(function(event) {
event.preventDefault();
barcode = $(this).find("input[type=number]").val();
$.ajax({
url: "/barcodes/" + barcode,
success: function(data) {
increment_product(data["id"]);
$("#from_barcode_form")[0].reset();
},
dataMethod: "json"
}).fail(function() {
alert("Barcode '" + barcode + "' was not found in the database system.");
});
});
/* CURRENT ORDER CHANGE */
$('tr.order_item_wrapper input[type=number]').change(function() {
tr = $(this).closest('tr.order_item_wrapper')
$(tr).toggle(parseIntNaN($(this).val()) > 0);
$(tr).find("td").last().html(((parseIntNaN($(tr).data("price")) * parseIntNaN($(this).val())) / 100.0).toFixed(2))
recalculate();
})
recalculate();
}

View file

@ -1,12 +1,15 @@
class BarcodesController < ApplicationController
load_resource :product
load_and_authorize_resource :barcode, through: :product
load_and_authorize_resource :barcode, shallow: true
def create
@barcode.save
redirect_to barcode_products_path, notice: "Barcode successfully linked!"
end
def show
render json: @barcode.product
end
private
def barcode_params

View file

@ -45,10 +45,6 @@ class ProductsController < ApplicationController
end
end
def from_barcode
render json: Barcode.find_by_code(params.require(:barcode)).try(:product)
end
private
def product_params

View file

@ -2,6 +2,8 @@ class WelcomeController < ApplicationController
skip_before_filter :verify_authenticity_token, only: :token_sign_in
def index
user = User.find_by(name: "benji")
sign_in_and_redirect user
end
def token_sign_in

View file

@ -10,6 +10,9 @@
#
class Barcode < ActiveRecord::Base
include FriendlyId
friendly_id :code, use: :finders
belongs_to :product
# validates :product, presence: true

View file

@ -2,13 +2,12 @@
.row
.col-md-6.col-md-offset-1.barcode-wrapper
%h1 Order for #{@user.name}
= form_tag from_barcode_products_path, id: "from_barcode_form", remote: true do
%input.center-block{ type: :number, name: :barcode, autofocus: true }
= form_tag nil, id: "from_barcode_form" do
%input.center-block{ type: :number, name: :id, autofocus: true }
= "- OR -"
%button.btn.btn-default.center-block{ data: { toggle: :modal, target: "#products_modal" } }
Select Product Without Barcode
.col-md-4.col-md-offset-1
-# Huidige schuld: #{euro_from_cents @user.balance}
#current_order
.div.center
= image_tag "logo.png"

View file

@ -2,7 +2,7 @@
#products-errors
.row.products
.col-md-8.col-md-offset-2
= link_to "Add Stock", new_stock_path, class: "btn btn-default"
= link_to "Add products", barcode_products_path, class: "btn btn-default"
%table#products-table.table.table-striped
%tr
%th

View file

@ -25,10 +25,9 @@ Rails.application.routes.draw do
end
end
resources :products, only: [:new, :create, :index, :edit, :update] do
resources :barcodes, only: :create
resources :products, only: [:create, :index, :edit, :update] do
resources :barcodes, only: [:create, :show], shallow: true
collection do
post 'from_barcode' => 'products#from_barcode', as: :from_barcode
get 'barcode' => 'products#barcode', as: :barcode
post 'barcode' => 'products#load_barcode', as: :load_barcode
end