From bfe86ae4810155e1dac8ccf8972dba3e4a19698b Mon Sep 17 00:00:00 2001 From: benji Date: Wed, 23 Sep 2015 11:09:24 +0200 Subject: [PATCH] Change barcode to a has_many relation --- app/controllers/products_controller.rb | 2 +- app/models/barcode.rb | 17 +++++++++++++++++ app/models/product.rb | 3 +-- .../20150919091214_add_barcode_to_products.rb | 7 ++++++- db/schema.rb | 10 +++++++++- spec/factories/products.rb | 1 - spec/models/product_spec.rb | 1 - 7 files changed, 34 insertions(+), 7 deletions(-) create mode 100644 app/models/barcode.rb diff --git a/app/controllers/products_controller.rb b/app/controllers/products_controller.rb index 956d5cf..17a876c 100644 --- a/app/controllers/products_controller.rb +++ b/app/controllers/products_controller.rb @@ -32,7 +32,7 @@ class ProductsController < ApplicationController end def from_barcode - render json: Product.find_by_barcode(params.require(:barcode)) + render json: Barcode.find_by_code(params.require(:barcode)).try(:product) end private diff --git a/app/models/barcode.rb b/app/models/barcode.rb new file mode 100644 index 0000000..be0a5ef --- /dev/null +++ b/app/models/barcode.rb @@ -0,0 +1,17 @@ +# == Schema Information +# +# Table name: barcodes +# +# id :integer not null, primary key +# product_id :integer +# code :string default(""), not null +# created_at :datetime +# updated_at :datetime +# + +class Barcode < ActiveRecord::Base + belongs_to :product + + validates :product, presence: true + validates :code, uniqueness: true +end diff --git a/app/models/product.rb b/app/models/product.rb index 1c81f38..3d63911 100644 --- a/app/models/product.rb +++ b/app/models/product.rb @@ -15,13 +15,13 @@ # stock :integer default("0"), not null # calories :integer # deleted :boolean default("f") -# barcode :string default(""), not null # class Product < ActiveRecord::Base include Avatarable has_many :order_items + has_many :barcodes enum category: %w(food beverages other) @@ -29,7 +29,6 @@ class Product < ActiveRecord::Base validates :price_cents, presence: true, numericality: { only_integer: true, greater_than: 0 } validates :stock, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } validates :calories, numericality: { only_integer: true, allow_nil: true, greater_than_or_equal_to: 0 } - validates :barcode, presence: true, uniqueness: true scope :for_sale, -> { where deleted: false } diff --git a/db/migrate/20150919091214_add_barcode_to_products.rb b/db/migrate/20150919091214_add_barcode_to_products.rb index 6952895..d0bf2af 100644 --- a/db/migrate/20150919091214_add_barcode_to_products.rb +++ b/db/migrate/20150919091214_add_barcode_to_products.rb @@ -1,5 +1,10 @@ class AddBarcodeToProducts < ActiveRecord::Migration def change - add_column :products, :barcode, :string, null: false, default: "" + create_table :barcodes do |t| + t.references :product + t.string :code, index: true, null: false, default: "" + + t.timestamps + end end end diff --git a/db/schema.rb b/db/schema.rb index 6c24368..a8562ce 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -13,6 +13,15 @@ ActiveRecord::Schema.define(version: 20150919091214) do + create_table "barcodes", force: :cascade do |t| + t.integer "product_id" + t.string "code", default: "", null: false + t.datetime "created_at" + t.datetime "updated_at" + end + + add_index "barcodes", ["code"], name: "index_barcodes_on_code" + create_table "delayed_jobs", force: :cascade do |t| t.integer "priority", default: 0, null: false t.integer "attempts", default: 0, null: false @@ -60,7 +69,6 @@ ActiveRecord::Schema.define(version: 20150919091214) do t.integer "stock", default: 0, null: false t.integer "calories" t.boolean "deleted", default: false - t.string "barcode", default: "", null: false end create_table "users", force: :cascade do |t| diff --git a/spec/factories/products.rb b/spec/factories/products.rb index 5d84eeb..5748ee6 100644 --- a/spec/factories/products.rb +++ b/spec/factories/products.rb @@ -15,7 +15,6 @@ # stock :integer default("0"), not null # calories :integer # deleted :boolean default("f") -# barcode :string default(""), not null # require 'faker' diff --git a/spec/models/product_spec.rb b/spec/models/product_spec.rb index 24f7acc..476a4fc 100644 --- a/spec/models/product_spec.rb +++ b/spec/models/product_spec.rb @@ -15,7 +15,6 @@ # stock :integer default("0"), not null # calories :integer # deleted :boolean default("f") -# barcode :string default(""), not null # describe Product do