From 306431dc52dc5a18034fc13792d4af97cd86a7e9 Mon Sep 17 00:00:00 2001 From: Tom Naessens Date: Mon, 20 Apr 2015 20:15:00 +0200 Subject: [PATCH] Reenable barcodes for items, generate migration to assign barcode data --- app/models/item.rb | 2 + ...20150420181046_add_barcode_to_all_items.rb | 10 ++++ db/schema.rb | 56 +++++++++---------- 3 files changed, 40 insertions(+), 28 deletions(-) create mode 100644 db/migrate/20150420181046_add_barcode_to_all_items.rb diff --git a/app/models/item.rb b/app/models/item.rb index e3589a5..7ba3b94 100644 --- a/app/models/item.rb +++ b/app/models/item.rb @@ -23,6 +23,8 @@ class Item < ActiveRecord::Base validates :name, presence: true, uniqueness: true validates :price, presence: true, numericality: { only_integer: true, greater_than_or_equal_to: 0 } + before_create :generate_barcode + def price from_cents read_attribute(:price) end diff --git a/db/migrate/20150420181046_add_barcode_to_all_items.rb b/db/migrate/20150420181046_add_barcode_to_all_items.rb new file mode 100644 index 0000000..c54341d --- /dev/null +++ b/db/migrate/20150420181046_add_barcode_to_all_items.rb @@ -0,0 +1,10 @@ +class AddBarcodeToAllItems < ActiveRecord::Migration + def change + Item.all.each do |i| + if i.barcode.nil? + i.generate_barcode + i.save + end + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 9cd3d35..c2d2054 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -11,20 +11,20 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema.define(version: 20140826191310) do +ActiveRecord::Schema.define(version: 20150420181046) do - create_table "items", force: true do |t| - t.string "name" - t.string "description" + create_table "items", force: :cascade do |t| + t.string "name", limit: 255 + t.string "description", limit: 255 t.integer "price" t.datetime "created_at" t.datetime "updated_at" t.integer "quantity" - t.string "barcode" - t.string "barcode_data" + t.string "barcode", limit: 255 + t.string "barcode_data", limit: 255 end - create_table "reservations", force: true do |t| + create_table "reservations", force: :cascade do |t| t.integer "item_id" t.integer "user_id" t.integer "count" @@ -41,44 +41,44 @@ add_index "reservations", ["item_id"], name: "index_reservations_on_item_id" add_index "reservations", ["user_id"], name: "index_reservations_on_user_id" - create_table "settings", force: true do |t| + create_table "settings", force: :cascade do |t| t.datetime "created_at" t.datetime "updated_at" - t.string "email" + t.string "email", limit: 255 t.datetime "deadline" - t.string "name" + t.string "name", limit: 255 end - create_table "users", force: true do |t| - t.string "email", default: "", null: false - t.string "encrypted_password", default: "", null: false - t.string "reset_password_token" + create_table "users", force: :cascade do |t| + t.string "email", limit: 255, default: "", null: false + t.string "encrypted_password", limit: 255, default: "", null: false + t.string "reset_password_token", limit: 255 t.datetime "reset_password_sent_at" t.datetime "remember_created_at" - t.integer "sign_in_count", default: 0, null: false + t.integer "sign_in_count", default: 0, null: false t.datetime "current_sign_in_at" t.datetime "last_sign_in_at" - t.string "current_sign_in_ip" - t.string "last_sign_in_ip" + t.string "current_sign_in_ip", limit: 255 + t.string "last_sign_in_ip", limit: 255 t.datetime "created_at" t.datetime "updated_at" - t.string "authentication_token" - t.string "role", default: "partner" - t.string "name" - t.boolean "sent", default: true - t.string "barcode" - t.string "barcode_data" + t.string "authentication_token", limit: 255 + t.string "role", limit: 255, default: "partner" + t.string "name", limit: 255 + t.boolean "sent", default: true + t.string "barcode", limit: 255 + t.string "barcode_data", limit: 255 end add_index "users", ["authentication_token"], name: "index_users_on_authentication_token" add_index "users", ["email"], name: "index_users_on_email", unique: true add_index "users", ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true - create_table "versions", force: true do |t| - t.string "item_type", null: false - t.integer "item_id", null: false - t.string "event", null: false - t.string "whodunnit" + create_table "versions", force: :cascade do |t| + t.string "item_type", limit: 255, null: false + t.integer "item_id", null: false + t.string "event", limit: 255, null: false + t.string "whodunnit", limit: 255 t.text "object" t.datetime "created_at" t.text "object_changes"