Skip to content

Instantly share code, notes, and snippets.

@zamith
Created October 17, 2017 13:38
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save zamith/6585761650f2c83f227f4b686d0e7c7b to your computer and use it in GitHub Desktop.
Save zamith/6585761650f2c83f227f4b686d0e7c7b to your computer and use it in GitHub Desktop.
diff --git a/app/models/shipping/commodity.rb b/app/models/shipping/commodity.rb
index ae7b51bc..1cdcf3f7 100644
--- a/app/models/shipping/commodity.rb
+++ b/app/models/shipping/commodity.rb
@@ -2,10 +2,13 @@ module Shipping
class Commodity < ApplicationRecord
belongs_to :shipments
+ monetize :sales_unit_price_cents
+ monetize :total_price_cents
+
+ validates_with CurrencyValidator, field: :sales_unit_price_currency
+ validates_with CurrencyValidator, field: :total_price_currency
+
validates_presence_of :generated_id, :category, :name,
- :quantity, :sales_unit_price_cents, :total_price_cents, :trans_num,
- :shipment
- #leaving off shipment_id as we sometimes need to create a commodity before
- #we know what the shipment_id will be
+ :quantity, :trans_num, :shipment
end
end
diff --git a/app/models/shipping/shipment.rb b/app/models/shipping/shipment.rb
index e50e7ab8..ab14766e 100644
--- a/app/models/shipping/shipment.rb
+++ b/app/models/shipping/shipment.rb
@@ -5,7 +5,15 @@ module Shipping
belongs_to :billing_address, class_name: "Address"
belongs_to :merchant, class_name: "User"
- validates_presence_of :merchant, :billing_address, :date, :time_offset,
- :transaction_currency, :insured_amount_cents, :package_value_cents
+ monetize :insured_amount_cents
+ monetize :package_value_cents
+
+ validates_with CurrencyValidator, field: :insured_amount_currency
+ validates_with CurrencyValidator, field: :total_price_currency
+
+ validates_presence_of :merchant, :billing_address,
+ :generated_id, :carrier_code, :transaction_code,
+ :transaction_received_on, :buyer_first_name,
+ :buyer_last_name, :buyer_email, :buyer_company
end
end
diff --git a/db/migrate/20171002130849_create_shipments.rb b/db/migrate/20171002130849_create_shipments.rb
index 0287d7f4..d0646f41 100644
--- a/db/migrate/20171002130849_create_shipments.rb
+++ b/db/migrate/20171002130849_create_shipments.rb
@@ -3,10 +3,9 @@ class CreateShipments < ActiveRecord::Migration[5.1]
create_table :shipments do |t|
t.belongs_to :billing_address, null: false
t.belongs_to :merchant, null: false
- t.string :merchant_order_id, null: false #DONT NEED THIS
- t.json :customer_info, null: false #don't need this buyer_address
- t.json :extra_info #delete this
- # need carrier tracking_number
+ t.string :merchant_order_id, null: false
+ t.json :customer_info, null: false
+ t.json :extra_info
t.timestamps
end
diff --git a/db/migrate/20171016171630_add_columns_to_shipping.rb b/db/migrate/20171016171630_add_columns_to_shipping.rb
index e69de29b..b6cd57de 100644
--- a/db/migrate/20171016171630_add_columns_to_shipping.rb
+++ b/db/migrate/20171016171630_add_columns_to_shipping.rb
@@ -0,0 +1,23 @@
+class AddColumnsToShipping < ActiveRecord::Migration[5.1]
+ def change
+ remove_column :shipments, :merchant_order_id, :string
+ remove_column :shipments, :customer_info, :json
+ remove_column :shipments, :extra_info, :json
+
+ change_table :shipments do |t|
+ t.string :generated_id, null: false # Shall we use UUID instead? Or name it external_id?
+ t.string :carrier_code, null: false
+ t.string :transaction_code, null: false
+ t.datetime :transaction_started_at, null: false # This will have the offset if one is given no need for separate column
+ t.monetize :insured_amount, null: false
+ t.monetize :package_value, null: false # This is create a package_value_currency, no need for transaction_currency
+ t.string :tracking_number
+
+ t.string :buyer_first_name, null: false
+ t.string :buyer_last_name, null: false
+ t.string :buyer_email, null: false
+ t.string :buyer_company, null: false
+ t.string :buyer_middle_name
+ end
+ end
+end
diff --git a/db/migrate/20171016172708_generate_commodity_table.rb b/db/migrate/20171016172708_generate_commodity_table.rb
index 3950fba0..8b5d88f8 100644
--- a/db/migrate/20171016172708_generate_commodity_table.rb
+++ b/db/migrate/20171016172708_generate_commodity_table.rb
@@ -1,16 +1,16 @@
class GenerateCommodityTable < ActiveRecord::Migration[5.1]
def change
create_table :commodities do |t|
- t.belongs_to :shipment, index: true
- t.string :generated_id, null: false
- t.string :category, null: false
- t.string :name, null: false
- t.integer :quantity, null: false
- t.integer :sales_unit_price_cents, null: false
- t.integer :total_price_cents, null: false
- t.integer :trans_num, null: false
- t.integer :shipment_id, null: false
- t.timestamps
+ t.belongs_to :shipment, index: true, foreign_key: true
+ t.string :generated_id, null: false # Shall we use UUID instead? Or name it external_id?
+ t.string :category, null: false
+ t.string :name, null: false
+ t.integer :quantity, null: false
+ t.monetize :sales_unit_price, null: false
+ t.monetize :total_price, null: false
+ t.string :transaction_number, null: false # why is this needed if we have the FK?
+
+ t.timestamps
end
end
end
diff --git a/db/schema.rb b/db/schema.rb
index 8d3b5234..afa48ee3 100644
--- a/db/schema.rb
+++ b/db/schema.rb
@@ -10,11 +10,10 @@
#
# It's strongly recommended that you check this file into your version control system.
-ActiveRecord::Schema.define(version: 20171016193200) do
+ActiveRecord::Schema.define(version: 20171016172708) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
- enable_extension "pg_stat_statements"
enable_extension "uuid-ossp"
create_table "active_admin_comments", id: :serial, force: :cascade do |t|
@@ -22,8 +21,8 @@ ActiveRecord::Schema.define(version: 20171016193200) do
t.text "body"
t.string "resource_id", null: false
t.string "resource_type", null: false
- t.integer "author_id"
t.string "author_type"
+ t.integer "author_id"
t.datetime "created_at"
t.datetime "updated_at"
t.index ["author_type", "author_id"], name: "index_active_admin_comments_on_author_type_and_author_id"
@@ -87,14 +86,16 @@ ActiveRecord::Schema.define(version: 20171016193200) do
end
create_table "commodities", force: :cascade do |t|
- t.integer "shipment_id", null: false
+ t.bigint "shipment_id"
t.string "generated_id", null: false
t.string "category", null: false
t.string "name", null: false
t.integer "quantity", null: false
- t.integer "sales_unit_price_cents", null: false
- t.integer "total_price_cents", null: false
- t.integer "trans_num", null: false
+ t.integer "sales_unit_price_cents", default: 0, null: false
+ t.string "sales_unit_price_currency", default: "USD", null: false
+ t.integer "total_price_cents", default: 0, null: false
+ t.string "total_price_currency", default: "USD", null: false
+ t.string "transaction_number", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.index ["shipment_id"], name: "index_commodities_on_shipment_id"
@@ -125,8 +126,8 @@ ActiveRecord::Schema.define(version: 20171016193200) do
t.integer "merchant_id", null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
- t.datetime "fulfilled_at", default: -> { "now()" }, null: false
t.string "status", null: false
+ t.datetime "fulfilled_at", default: -> { "now()" }, null: false
t.index ["merchant_id", "merchant_order_id"], name: "index_customer_orders_on_merchant_id_and_merchant_order_id", unique: true
t.index ["merchant_id"], name: "index_customer_orders_on_merchant_id"
end
@@ -146,7 +147,7 @@ ActiveRecord::Schema.define(version: 20171016193200) do
end
create_table "invoices", id: :serial, force: :cascade do |t|
- t.string "batch_type", null: false
+ t.string "batch_type"
t.integer "batch_id", null: false
t.string "file_file_name"
t.string "file_content_type"
@@ -179,7 +180,7 @@ ActiveRecord::Schema.define(version: 20171016193200) do
create_table "pending_payments", id: :serial, force: :cascade do |t|
t.string "kind", null: false
- t.json "info", null: false
+ t.jsonb "info", null: false
t.boolean "released", default: false, null: false
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
@@ -291,8 +292,8 @@ ActiveRecord::Schema.define(version: 20171016193200) do
t.integer "duration"
t.integer "original_price_per_year_cents", default: 0
t.boolean "auto_accept", default: false, null: false
- t.string "source"
t.json "additional_info", default: {}, null: false
+ t.string "source"
t.string "zip_code", null: false
t.string "insurable_item_type"
t.index ["insurable_item_id"], name: "index_price_quotes_on_insurable_item_id"
@@ -332,7 +333,7 @@ ActiveRecord::Schema.define(version: 20171016193200) do
end
create_table "reports", id: :serial, force: :cascade do |t|
- t.string "counterparty_type", null: false
+ t.string "counterparty_type"
t.integer "counterparty_id", null: false
t.date "start_date", null: false
t.date "end_date", null: false
@@ -378,19 +379,19 @@ ActiveRecord::Schema.define(version: 20171016193200) do
t.datetime "created_at", null: false
t.datetime "updated_at", null: false
t.string "generated_id", null: false
- t.date "transaction_time", null: false
- t.string "transaction_time_offset", null: false
- t.string "transaction_currency", null: false
- t.integer "insured_amount_cents", null: false
- t.integer "package_value_cents", null: false
- t.string "tracking_number"
t.string "carrier_code", null: false
t.string "transaction_code", null: false
+ t.datetime "transaction_started_at", null: false
+ t.integer "insured_amount_cents", default: 0, null: false
+ t.string "insured_amount_currency", default: "USD", null: false
+ t.integer "package_value_cents", default: 0, null: false
+ t.string "package_value_currency", default: "USD", null: false
+ t.string "tracking_number"
t.string "buyer_first_name", null: false
- t.string "buyer_middle_name"
t.string "buyer_last_name", null: false
- t.string "buyer_company", null: false
t.string "buyer_email", null: false
+ t.string "buyer_company", null: false
+ t.string "buyer_middle_name"
t.index ["billing_address_id"], name: "index_shipments_on_billing_address_id"
t.index ["merchant_id"], name: "index_shipments_on_merchant_id"
end
@@ -418,8 +419,8 @@ ActiveRecord::Schema.define(version: 20171016193200) do
create_table "transactions", id: :serial, force: :cascade do |t|
t.string "kind", null: false
t.string "recipient_name", null: false
+ t.string "recipient_type"
t.integer "recipient_id", null: false
- t.string "recipient_type", null: false
t.string "status", null: false
t.string "identifier"
t.datetime "created_at"
@@ -477,6 +478,7 @@ ActiveRecord::Schema.define(version: 20171016193200) do
end
add_foreign_key "commissions", "policies"
+ add_foreign_key "commodities", "shipments"
add_foreign_key "companies", "users"
add_foreign_key "customer_orders", "users", column: "merchant_id"
add_foreign_key "customers", "addresses"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment