From d66573919b658cc53ae6e5cdc85e751668250c1e Mon Sep 17 00:00:00 2001 From: Mic Szillat Date: Sat, 4 Nov 2017 20:35:26 +0100 Subject: [PATCH] :bug: fix barcode table schema --- app/models/barcode.rb | 3 +- app/models/drink.rb | 3 +- .../20171104191523_recreate_barcodes.rb | 14 ++++++ db/schema.rb | 44 ++++++++++--------- 4 files changed, 41 insertions(+), 23 deletions(-) create mode 100644 db/migrate/20171104191523_recreate_barcodes.rb diff --git a/app/models/barcode.rb b/app/models/barcode.rb index bda2051c..470fcae1 100644 --- a/app/models/barcode.rb +++ b/app/models/barcode.rb @@ -1,5 +1,6 @@ class Barcode < ActiveRecord::Base - validates_presence_of :drink + validates_presence_of :drink, :value + belongs_to :drink end diff --git a/app/models/drink.rb b/app/models/drink.rb index 4e788be0..f075c0da 100644 --- a/app/models/drink.rb +++ b/app/models/drink.rb @@ -6,7 +6,8 @@ class Drink < ActiveRecord::Base validates_attachment_content_type :logo, :content_type => %w(image/jpeg image/jpg image/png) before_post_process :normalize_filename after_initialize :set_defaults, unless: :persisted? - + has_one :barcode + def as_json(options) h = super(options) h["donation_recommendation"] = price # API compatibility diff --git a/db/migrate/20171104191523_recreate_barcodes.rb b/db/migrate/20171104191523_recreate_barcodes.rb new file mode 100644 index 00000000..7a0392c0 --- /dev/null +++ b/db/migrate/20171104191523_recreate_barcodes.rb @@ -0,0 +1,14 @@ +class RecreateBarcodes < ActiveRecord::Migration[5.1] + def change + remove_column :drinks, :barcode, :integer + drop_table :barcodes, id: false do |t| + t.string :id + t.primary_key :id + end + create_table :barcodes do |t| + t.string :value + t.index :value + t.references :drinks + end + end +end diff --git a/db/schema.rb b/db/schema.rb index c709bc6e..defbf8f5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,43 +10,45 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20170929201659) do +ActiveRecord::Schema.define(version: 20171104191523) do create_table "audits", force: :cascade do |t| t.datetime "created_at" - t.decimal "difference", precision: 20, scale: 2, default: "0.0" - t.integer "drink" - t.integer "user" + t.decimal "difference", precision: 20, scale: 2, default: "0.0" + t.integer "drink" + t.integer "user" end - create_table "barcodes", id: :string, force: :cascade do |t| - t.integer "drink", null: false - t.index ["id"], name: "sqlite_autoindex_barcodes_1", unique: true + create_table "barcodes", force: :cascade do |t| + t.string "value" + t.integer "drinks_id" + t.index ["drinks_id"], name: "index_barcodes_on_drinks_id" + t.index ["value"], name: "index_barcodes_on_value" end create_table "drinks", force: :cascade do |t| - t.string "name" - t.decimal "bottle_size", precision: 20, scale: 2, default: "0.0" - t.integer "caffeine" - t.decimal "price", precision: 20, scale: 2, default: "0.0" - t.string "logo_file_name" + t.string "name" + t.decimal "bottle_size", precision: 20, scale: 2, default: "0.0" + t.integer "caffeine" + t.decimal "price", precision: 20, scale: 2, default: "0.0" + t.string "logo_file_name" t.datetime "created_at" t.datetime "updated_at" - t.string "logo_content_type" - t.integer "logo_file_size" + t.string "logo_content_type" + t.integer "logo_file_size" t.datetime "logo_updated_at" - t.boolean "active", default: true + t.boolean "active", default: true end create_table "users", force: :cascade do |t| - t.string "name" - t.string "email" + t.string "name" + t.string "email" t.datetime "created_at" t.datetime "updated_at" - t.decimal "balance", precision: 20, scale: 2, default: "0.0" - t.boolean "active", default: true - t.boolean "audit", default: false - t.boolean "redirect", default: true + t.decimal "balance", precision: 20, scale: 2, default: "0.0" + t.boolean "active", default: true + t.boolean "audit", default: false + t.boolean "redirect", default: true end end