summaryrefslogtreecommitdiff
path: root/app/models
diff options
context:
space:
mode:
Diffstat (limited to 'app/models')
-rw-r--r--app/models/address.rb24
-rw-r--r--app/models/user_account.rb3
-rw-r--r--app/models/user_account_address.rb7
3 files changed, 34 insertions, 0 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