This document outlines all the steps taken to set up and migrate the database for the Rails 8.0.1 application, including Devise authentication, database structure, model relationships, RSpec testing, and required gems.
Gem | Version | Purpose & Usage |
---|---|---|
rails |
~> 8.0.1 |
Core framework for building web applications in Ruby. |
puma |
>= 5.0 |
High-performance web server for running Rails applications. |
pg |
~> 1.1 |
PostgreSQL adapter for Active Record, used as the database. |
propshaft |
Modern asset pipeline for Rails applications. |
Gem | Version | Purpose & Usage |
---|---|---|
importmap-rails |
Enables JavaScript ESM import maps without Webpack. | |
turbo-rails |
Hotwire’s Turbo for fast page updates and SPA-like navigation. | |
stimulus-rails |
JavaScript framework for managing UI interactions. | |
tailwindcss-rails |
Integrates Tailwind CSS for styling Rails applications. |
Gem | Version | Purpose & Usage |
---|---|---|
devise |
~> 4.9 |
User authentication, session management, and password recovery. |
brakeman |
Security scanner for finding vulnerabilities in Rails apps. |
Gem | Version | Purpose & Usage |
---|---|---|
solid_cache |
Caching mechanism for Rails applications. | |
solid_queue |
Background job system for handling asynchronous tasks. | |
solid_cable |
WebSockets for real-time features using ActionCable. | |
bootsnap |
Caches expensive operations to reduce application boot time. |
Gem | Version | Purpose & Usage |
---|---|---|
jbuilder |
Allows easy generation of JSON APIs in Rails. |
Gem | Version | Purpose & Usage |
---|---|---|
kamal |
Docker-based deployment tool for Rails applications. | |
thruster |
Adds HTTP caching, compression, and X-Sendfile acceleration. |
Gem | Version | Purpose & Usage |
---|---|---|
debug |
Debugging tool for Rails applications. | |
dotenv-rails |
Manages environment variables securely. | |
rubocop-rails-omakase |
Omakase-style Ruby linting and code style enforcement. | |
web-console |
Allows interactive Rails console access from error pages. |
Gem | Version | Purpose & Usage |
---|---|---|
rspec-rails |
Core RSpec framework for unit testing in Rails. | |
capybara |
Feature testing framework for simulating user interactions. | |
selenium-webdriver |
Enables browser automation for system tests. | |
webdrivers |
5.0.0 |
Manages and updates browser drivers automatically. |
rails-controller-testing |
1.0.5 |
Adds support for controller tests in RSpec. |
shoulda-matchers |
~> 5.0 |
Simplifies testing of models and associations. |
-
users
- Devise authentication implemented.
- Role-based authorization: Roles include
admin
,customer
, andsupplier
. - Wholesale customers: Introduced
discount_rate
for wholesale customers. - Column
user_type
renamed torole
for clarity. - Constraints:
email
: unique, requiredname
: requiredrole
: required, defaultcustomer
-
products
- New columns:
discount_limited_stock
to control how many products can be sold at a discounted rate.maximum_discount
to define the highest possible discount.barcode
is now optional.
- Constraints:
product_sku
: unique, requiredselling_price
: requiredsupplier_id
: foreign key referencesusers
- New columns:
-
inventory
- Each row represents an individual product unit.
- Tracks purchase cost and sale cost per item.
- Status-based tracking:
Available
,Reserved
,Sold
,Lost
,Damaged
,Scrap
,In Transit
. - Stores
purchase_order_id
andsale_order_id
instead of separate order items tables. - Constraints:
product_id
: required, foreign keypurchase_cost
: required, numericstatus
: required
-
purchase_orders
- Handles supplier orders.
- Backorders are supported.
- Purchase orders split when partial shipments occur.
- Updated Columns:
- Added
shipping_cost
,tax_cost
, andother_cost
.
- Added
- Constraints:
id
: primary key (string)user_id
: foreign keysubtotal
,total_order_cost
: required, numericstatus
: required
-
sale_orders
- Handles customer orders.
- Linked to
payments
andshipments
tables. - Constraints:
id
: primary key (string)user_id
: foreign keysubtotal
,total_order_value
: required, numericstatus
: required
-
payments
- Tracks customer payments.
- New
status
column to trackPending
,Completed
,Refunded
, etc. - Constraints:
amount
: required, numericpayment_method
: requiredstatus
: requiredsale_order_id
: foreign key (added in separate migration)
-
shipments
- Tracks shipments for both sales and purchases.
- New columns:
last_update
(timestamp) &status
(e.g., Pending, Shipped, Delivered). - Constraints:
sale_order_id
: required, foreign keytracking_number
,carrier
: required
-
canceled_order_items
- Tracks items from canceled orders, preserving historical pricing data.
- Includes
sale_price
at the time of cancellation. - Constraints:
sale_order_id
: required, foreign keyproduct_id
: required, foreign keycanceled_quantity
: required, numeric
- Explicit Foreign Key Handling:
- Used
belongs_to
withforeign_key: "column_name"
where necessary.
- Used
- Validation of Required Fields:
- Ensured
presence: true
for critical attributes.
- Ensured
- Restrict Delete Dependencies:
dependent: :restrict_with_error
used to prevent accidental deletions.
-
User
- Associations:
has_many :purchase_orders
has_many :sale_orders
- Validations:
email
: required, uniquename
: requiredrole
: required
- Associations:
-
Product
- Associations:
belongs_to :supplier, class_name: "User"
- Validations:
product_sku
,product_name
,selling_price
: required
- Associations:
-
PurchaseOrder
- Associations:
belongs_to :user
has_many :inventory
- Validations:
order_date
,subtotal
,total_order_cost
,status
: required
- Associations:
-
SaleOrder
- Associations:
belongs_to :user
has_one :shipment
has_one :payment
- Validations:
order_date
,total_order_value
: required
- Associations:
-
Payment
- Associations:
belongs_to :sale_order
- Validations:
amount
,payment_method
,status
: required
- Associations:
-
Shipment
- Associations:
belongs_to :sale_order
- Validations:
tracking_number
,carrier
,status
: required
- Associations:
-
Inventory
- Associations:
belongs_to :product
belongs_to :purchase_order, optional: true
belongs_to :sale_order, optional: true
- Validations:
purchase_cost
,status
: required
- Associations:
-
CanceledOrderItem
- Associations:
belongs_to :sale_order
belongs_to :product
- Validations:
canceled_quantity
,sale_price_at_cancellation
: required
- Associations:
- Unknown Validator
DateValidator
in Shipment Model 🚨 → Removed incorrect validator. - Missing Database Columns in PurchaseOrder Tests 🚨 → Updated schema, ensured correct column names.
- User Email Uniqueness Validation Failure 🚨 → Added
before do
block to create a valid user before testing uniqueness.
- User
- Product
- PurchaseOrder
- SaleOrder
- Payment
- Shipment
- Inventory
- CanceledOrderItem
All models successfully pass validation, association, and custom logic tests. 🎉
✅ Finalize integration testing with RSpec. ✅ Push latest updates to Git & deploy. ✅ Optimize queries for better performance.
🚀 Database, models, and testing are now fully set up! 🚀