diff options
-rw-r--r-- | app/models/address.rb | 24 | ||||
-rw-r--r-- | app/models/user_account.rb | 3 | ||||
-rw-r--r-- | app/models/user_account_address.rb | 7 | ||||
-rw-r--r-- | db/migrate/20230322234003_create_addresses.rb | 2 | ||||
-rw-r--r-- | db/migrate/20230322234849_create_user_account_addresses.rb | 10 | ||||
-rw-r--r-- | db/schema.rb | 23 | ||||
-rw-r--r-- | spec/models/address_spec.rb | 5 | ||||
-rw-r--r-- | spec/models/user_account_address_spec.rb | 5 |
8 files changed, 77 insertions, 2 deletions
diff --git a/app/models/address.rb b/app/models/address.rb index a19addc..260407d 100644 --- a/app/models/address.rb +++ b/app/models/address.rb @@ -1,2 +1,26 @@ +# frozen_string_literal: true + +# Address +# number: string +# street: string +# zip_code: string +# country: string +# city: string class Address < ApplicationRecord + has_many :user_account_addresses + has_many :user_accounts, through: :user_account_addresses + + validates :number, presence: true + validates :number, comparison: { greater_than_or_equal_to: 0 } + validates :street, presence: true + validates :zip_code, presence: true + validates :country, presence: true + validate :valid_country + validates :city, presence: true + + def valid_country + return unless Country[country].nil? + + errors.add(:country, 'Invalid country') + end end diff --git a/app/models/user_account.rb b/app/models/user_account.rb index cd99441..0f94003 100644 --- a/app/models/user_account.rb +++ b/app/models/user_account.rb @@ -8,6 +8,9 @@ # session_key: string class UserAccount < ApplicationRecord has_secure_password validations: false + has_many :user_account_addresses + has_many :addresses, through: :user_account_addresses + validates :email, presence: true validates :email, uniqueness: true validates :password, presence: true, if: :no_password? diff --git a/app/models/user_account_address.rb b/app/models/user_account_address.rb new file mode 100644 index 0000000..0be86f2 --- /dev/null +++ b/app/models/user_account_address.rb @@ -0,0 +1,7 @@ +# frozen_string_literal: true + +# UserAccountAddress join table +class UserAccountAddress < ApplicationRecord + belongs_to :user_account + belongs_to :address +end diff --git a/db/migrate/20230322234003_create_addresses.rb b/db/migrate/20230322234003_create_addresses.rb index ff36085..cab3dee 100644 --- a/db/migrate/20230322234003_create_addresses.rb +++ b/db/migrate/20230322234003_create_addresses.rb @@ -1,7 +1,7 @@ class CreateAddresses < ActiveRecord::Migration[7.0] def change create_table :addresses do |t| - t.string :number + t.integer :number t.string :street t.string :zip_code t.string :country diff --git a/db/migrate/20230322234849_create_user_account_addresses.rb b/db/migrate/20230322234849_create_user_account_addresses.rb new file mode 100644 index 0000000..a2e0e9b --- /dev/null +++ b/db/migrate/20230322234849_create_user_account_addresses.rb @@ -0,0 +1,10 @@ +class CreateUserAccountAddresses < ActiveRecord::Migration[7.0] + def change + create_table :user_account_addresses do |t| + t.references :user_account, null: false, foreign_key: true + t.references :address, null: false, foreign_key: true + + t.timestamps + end + end +end diff --git a/db/schema.rb b/db/schema.rb index 07de3af..eda3bd5 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.0].define(version: 2023_03_14_020111) do +ActiveRecord::Schema[7.0].define(version: 2023_03_22_234849) do create_table "active_storage_attachments", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| t.string "name", null: false t.string "record_type", null: false @@ -39,6 +39,16 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_14_020111) do t.index ["blob_id", "variation_digest"], name: "index_active_storage_variant_records_uniqueness", unique: true end + create_table "addresses", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.integer "number" + t.string "street" + t.string "zip_code" + t.string "country" + t.string "city" + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + end + create_table "companies", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| t.string "name" t.string "country" @@ -60,6 +70,15 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_14_020111) do t.index ["company_id"], name: "index_products_on_company_id" end + create_table "user_account_addresses", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| + t.bigint "user_account_id", null: false + t.bigint "address_id", null: false + t.datetime "created_at", null: false + t.datetime "updated_at", null: false + t.index ["address_id"], name: "index_user_account_addresses_on_address_id" + t.index ["user_account_id"], name: "index_user_account_addresses_on_user_account_id" + end + create_table "user_accounts", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t| t.string "email" t.string "password_digest" @@ -74,4 +93,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_14_020111) do add_foreign_key "active_storage_attachments", "active_storage_blobs", column: "blob_id" add_foreign_key "active_storage_variant_records", "active_storage_blobs", column: "blob_id" add_foreign_key "products", "companies" + add_foreign_key "user_account_addresses", "addresses" + add_foreign_key "user_account_addresses", "user_accounts" end diff --git a/spec/models/address_spec.rb b/spec/models/address_spec.rb new file mode 100644 index 0000000..5531d0d --- /dev/null +++ b/spec/models/address_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe Address, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end diff --git a/spec/models/user_account_address_spec.rb b/spec/models/user_account_address_spec.rb new file mode 100644 index 0000000..3e358e6 --- /dev/null +++ b/spec/models/user_account_address_spec.rb @@ -0,0 +1,5 @@ +require 'rails_helper' + +RSpec.describe UserAccountAddress, type: :model do + pending "add some examples to (or delete) #{__FILE__}" +end |