From e6264fd6c8aca206485835dc9f35908963823afb Mon Sep 17 00:00:00 2001 From: benji Date: Mon, 28 Sep 2015 21:51:40 +0200 Subject: [PATCH] Move some stuff to barcodecontroller where it belongs + refactoring --- app/assets/javascripts/orders.js | 87 ++++++++++++---------- app/controllers/barcodes_controller.rb | 7 +- app/controllers/products_controller.rb | 4 - app/controllers/welcome_controller.rb | 2 + app/models/barcode.rb | 3 + app/views/orders/new.html.haml | 5 +- app/views/products_list/listview.html.haml | 2 +- config/routes.rb | 5 +- 8 files changed, 62 insertions(+), 53 deletions(-) diff --git a/app/assets/javascripts/orders.js b/app/assets/javascripts/orders.js index 81deb19..ae3f0c2 100644 --- a/app/assets/javascripts/orders.js +++ b/app/assets/javascripts/orders.js @@ -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(); } diff --git a/app/controllers/barcodes_controller.rb b/app/controllers/barcodes_controller.rb index 629225a..c0fcf57 100644 --- a/app/controllers/barcodes_controller.rb +++ b/app/controllers/barcodes_controller.rb @@ -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 diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index e05d985..d2f2874 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -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 diff --git a/app/controllers/welcome_controller.rb b/app/controllers/welcome_controller.rb index b351b34..bfb8835 100644 --- a/app/controllers/welcome_controller.rb +++ b/app/controllers/welcome_controller.rb @@ -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 diff --git a/app/models/barcode.rb b/app/models/barcode.rb index 43663e6..c3e34ed 100644 --- a/app/models/barcode.rb +++ b/app/models/barcode.rb @@ -10,6 +10,9 @@ # class Barcode < ActiveRecord::Base + include FriendlyId + friendly_id :code, use: :finders + belongs_to :product # validates :product, presence: true diff --git a/app/views/orders/new.html.haml b/app/views/orders/new.html.haml index 3ec0ee0..2573f91 100644 --- a/app/views/orders/new.html.haml +++ b/app/views/orders/new.html.haml @@ -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" diff --git a/app/views/products_list/listview.html.haml b/app/views/products_list/listview.html.haml index e165162..918adfa 100644 --- a/app/views/products_list/listview.html.haml +++ b/app/views/products_list/listview.html.haml @@ -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 diff --git a/config/routes.rb b/config/routes.rb index 5afbe97..2b90cc8 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -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