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. // All this logic will automatically be available in application.js.
// You can use CoffeeScript in this file: http://coffeescript.org/ // You can use CoffeeScript in this file: http://coffeescript.org/
ready = function() { ready = function() {
products_ordered = $('#product_search').keyup(function () { /* INITIALIZE */
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
})
$('tr.order_item_wrapper').hide(); $('tr.order_item_wrapper').hide();
$('tr.order_item_wrapper').filter(function() { $('tr.order_item_wrapper').filter(function() {
return parseIntNaN($(this).find('[type=number]').val()) > 0; return parseIntNaN($(this).find('[type=number]').val()) > 0;
}).show(); }).show();
$('tr.order_item_wrapper input[type=number]').change(function() { /* HELPERS */
tr = $(this).closest('tr.order_item_wrapper') increment_product = function(product_id) {
$(tr).toggle(parseIntNaN($(this).val()) > 0); input = $("#current_order").find(".order_item_wrapper[data-product=" + product_id + "]").find("input[type=number]");
$(tr).find("td").last().html(((parseIntNaN($(tr).data("price")) * parseIntNaN($(this).val())) / 100.0).toFixed(2)) $(input).val(parseIntNaN($(input).val()) + 1).change();
recalculate(); }
})
recalculate = function() { recalculate = function() {
/* Total Price */ /* Total Price */
@ -63,6 +29,47 @@ ready = function() {
}).length)); }).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(); recalculate();
} }

View file

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

View file

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

View file

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

View file

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

View file

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

View file

@ -2,7 +2,7 @@
#products-errors #products-errors
.row.products .row.products
.col-md-8.col-md-offset-2 .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 %table#products-table.table.table-striped
%tr %tr
%th %th

View file

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