First commit

This commit is contained in:
Vikram Rangnekar
2019-03-24 09:57:29 -04:00
commit b9d38a5e9d
153 changed files with 18120 additions and 0 deletions

View File

@ -0,0 +1,48 @@
# frozen_string_literal: true
class DeviseCreateUsers < ActiveRecord::Migration[5.2]
def change
create_table :users do |t|
t.string :full_name, null: false
t.string :phone
t.string :avatar
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.inet :current_sign_in_ip
# t.inet :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :users, :email, unique: true
add_index :users, :reset_password_token, unique: true
# add_index :users, :confirmation_token, unique: true
# add_index :users, :unlock_token, unique: true
end
end

View File

@ -0,0 +1,47 @@
# frozen_string_literal: true
class DeviseCreateCustomers < ActiveRecord::Migration[5.2]
def change
create_table :customers do |t|
t.string :full_name, null: false
t.string :phone
## Database authenticatable
t.string :email, null: false, default: ""
t.string :encrypted_password, null: false, default: ""
## Recoverable
t.string :reset_password_token
t.datetime :reset_password_sent_at
## Rememberable
t.datetime :remember_created_at
## Trackable
# t.integer :sign_in_count, default: 0, null: false
# t.datetime :current_sign_in_at
# t.datetime :last_sign_in_at
# t.inet :current_sign_in_ip
# t.inet :last_sign_in_ip
## Confirmable
# t.string :confirmation_token
# t.datetime :confirmed_at
# t.datetime :confirmation_sent_at
# t.string :unconfirmed_email # Only if using reconfirmable
## Lockable
# t.integer :failed_attempts, default: 0, null: false # Only if lock strategy is :failed_attempts
# t.string :unlock_token # Only if unlock strategy is :email or :both
# t.datetime :locked_at
t.timestamps null: false
end
add_index :customers, :email, unique: true
add_index :customers, :reset_password_token, unique: true
# add_index :customers, :confirmation_token, unique: true
# add_index :customers, :unlock_token, unique: true
end
end

View File

@ -0,0 +1,12 @@
class CreateProducts < ActiveRecord::Migration[5.2]
def change
create_table :products do |t|
t.string :name
t.text :description
t.decimal :price, precision: 7, scale: 2
t.belongs_to :user, foreign_key: true
t.timestamps
end
end
end

View File

@ -0,0 +1,12 @@
class Purchases < ActiveRecord::Migration[5.2]
def change
create_table :purchases do |t|
t.references :customer, foreign_key: true
t.references :product, foreign_key: true
t.string :sale_type
t.integer :quantity
t.datetime :due_date
t.datetime :returned
end
end
end

71
example/db/schema.rb Normal file
View File

@ -0,0 +1,71 @@
# This file is auto-generated from the current state of the database. Instead
# of editing this file, please use the migrations feature of Active Record to
# incrementally modify your database, and then regenerate this schema definition.
#
# Note that this schema.rb definition is the authoritative source for your
# database schema. If you need to create the application database on another
# system, you should be using db:schema:load, not running all the migrations
# from scratch. The latter is a flawed and unsustainable approach (the more migrations
# you'll amass, the slower it'll run and the greater likelihood for issues).
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 2019_03_22_200743) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
create_table "customers", force: :cascade do |t|
t.string "full_name", null: false
t.string "phone"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_customers_on_email", unique: true
t.index ["reset_password_token"], name: "index_customers_on_reset_password_token", unique: true
end
create_table "products", force: :cascade do |t|
t.string "name"
t.text "description"
t.decimal "price", precision: 7, scale: 2
t.bigint "user_id"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["user_id"], name: "index_products_on_user_id"
end
create_table "purchases", force: :cascade do |t|
t.bigint "customer_id"
t.bigint "product_id"
t.string "sale_type"
t.integer "quantity"
t.datetime "due_date"
t.datetime "returned"
t.index ["customer_id"], name: "index_purchases_on_customer_id"
t.index ["product_id"], name: "index_purchases_on_product_id"
end
create_table "users", force: :cascade do |t|
t.string "full_name", null: false
t.string "phone"
t.string "avatar"
t.string "email", default: "", null: false
t.string "encrypted_password", default: "", null: false
t.string "reset_password_token"
t.datetime "reset_password_sent_at"
t.datetime "remember_created_at"
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
end
add_foreign_key "products", "users"
add_foreign_key "purchases", "customers"
add_foreign_key "purchases", "products"
end

82
example/db/seeds.rb Normal file
View File

@ -0,0 +1,82 @@
# This file should contain all the record creation needed to seed the database with its default values.
# The data can then be loaded with the rails db:seed command (or created alongside the database with db:setup).
#
# Examples:
#
# movies = Movie.create([{ name: 'Star Wars' }, { name: 'Lord of the Rings' }])
# Character.create(name: 'Luke', movie: movies.first)
pwd = "123456"
user_count = 10
customer_count = 100
product_count = 50
purchase_count = 100
user_count.times do |i|
user = User.create(
full_name: Faker::Name.name,
avatar: Faker::Avatar.image,
phone: Faker::PhoneNumber.cell_phone,
email: Faker::Internet.email,
password: pwd,
password_confirmation: pwd
)
user.save!
puts user.inspect
end
customer_count.times do |i|
customer = Customer.create(
full_name: Faker::Name.name,
phone: Faker::PhoneNumber.cell_phone,
email: Faker::Internet.email,
password: pwd,
password_confirmation: pwd
)
customer.save!
puts customer.inspect
end
product_count.times do |i|
desc = [
Faker::Beer.brand,
Faker::Beer.style,
Faker::Beer.hop,
Faker::Beer.yeast,
Faker::Beer.ibu,
Faker::Beer.alcohol,
Faker::Beer.blg,
].join(", ")
product = Product.create(
name: Faker::Beer.name,
description: desc,
price: rand(5.5..19.0),
user_id: rand(1..user_count)
)
product.save!
puts product.inspect
end
purchase_count.times do |i|
sale_type = ["rented", "bought"].sample
if sale_type == "rented"
due_date = Faker::Date.forward(30)
if [0, 3].sample == 1
returned = Faker::Date.forward(60)
end
end
purchase = Purchase.create(
customer_id: rand(1..customer_count),
product_id: rand(1..product_count),
sale_type: sale_type,
quantity: rand(1..10),
due_date: due_date,
returned: returned
)
purchase.save!
puts purchase.inspect
end