summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--app/models/card.rb17
-rw-r--r--db/schema.rb15
-rw-r--r--spec/factories/card.rb12
-rw-r--r--spec/models/card_spec.rb40
4 files changed, 82 insertions, 2 deletions
diff --git a/app/models/card.rb b/app/models/card.rb
index ae2b1e5..594ae43 100644
--- a/app/models/card.rb
+++ b/app/models/card.rb
@@ -1,3 +1,20 @@
+# frozen_string_literal: true
+
+# Card
+# number: string
+# expiration_month: integer
+# expiration_year: integer
+# expiration_day: integer
+# security_code: integer
class Card < ApplicationRecord
belongs_to :user_account
+ validates :number, presence: true, uniqueness: true, length: { is: 16 }
+ validates :expiration_year, presence: true, numericality: { greater_than: 1970 }
+ validates :expiration_month, presence: true, inclusion: { in: 1..12 }
+ validates :expiration_day, presence: true, inclusion: { in: 1..31 }
+ validates :security_code, presence: true
+
+ def expiration_date
+ "#{expiration_day}/#{expiration_month}/#{expiration_year}"
+ end
end
diff --git a/db/schema.rb b/db/schema.rb
index 26cf4a6..11c178a 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_25_024534) do
+ActiveRecord::Schema[7.0].define(version: 2023_03_25_061452) 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
@@ -49,6 +49,18 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_25_024534) do
t.datetime "updated_at", null: false
end
+ create_table "cards", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
+ t.string "number"
+ t.integer "expiration_month"
+ t.integer "expiration_year"
+ t.integer "expiration_day"
+ t.integer "security_code"
+ t.bigint "user_account_id", null: false
+ t.datetime "created_at", null: false
+ t.datetime "updated_at", null: false
+ t.index ["user_account_id"], name: "index_cards_on_user_account_id"
+ end
+
create_table "companies", charset: "utf8mb4", collation: "utf8mb4_general_ci", force: :cascade do |t|
t.string "name"
t.string "country"
@@ -93,5 +105,6 @@ ActiveRecord::Schema[7.0].define(version: 2023_03_25_024534) 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 "cards", "user_accounts"
add_foreign_key "products", "companies"
end
diff --git a/spec/factories/card.rb b/spec/factories/card.rb
new file mode 100644
index 0000000..ddcafaf
--- /dev/null
+++ b/spec/factories/card.rb
@@ -0,0 +1,12 @@
+# frozen_string_literal: true
+
+FactoryBot.define do
+ factory :card, class: 'Card' do
+ number { "#{rand(1000..9999)}-#{rand(1000..9999)}-#{rand(1000..9999)}-#{rand(1000..9999)}" }
+ expiration_day { rand(1..31) }
+ expiration_month { rand(1..12) }
+ expiration_year { rand(1971..2025) }
+ security_code { rand(100..999) }
+ user_account { create(:user_account) }
+ end
+end
diff --git a/spec/models/card_spec.rb b/spec/models/card_spec.rb
index 6f49b4a..11bee2d 100644
--- a/spec/models/card_spec.rb
+++ b/spec/models/card_spec.rb
@@ -1,5 +1,43 @@
+# frozen_string_literal: true
+
require 'rails_helper'
RSpec.describe Card, type: :model do
- pending "add some examples to (or delete) #{__FILE__}"
+ let(:card) { build(:card) }
+
+ it { should belong_to(:user_account)}
+ it { should validate_presence_of(:number) }
+ it { should validate_presence_of(:security_code) }
+ it { should validate_presence_of(:expiration_year) }
+ it { should validate_presence_of(:expiration_month) }
+ it { should validate_presence_of(:expiration_day) }
+ it { should validate_numericality_of(:expiration_year) }
+
+ describe 'expiration_month' do
+ it "can't be less than 1 or greater than 12" do
+ card.expiration_month = 0
+ expect(card.save).to be_falsey
+ card.expiration_month = 13
+ expect(card.save).to be_falsey
+ end
+ end
+
+ describe 'expiration_day' do
+ it "can't be less than 1 or greater than 31" do
+ card.expiration_day = 0
+ expect(card.save).to be_falsey
+ card.expiration_day = 32
+ expect(card.save).to be_falsey
+ end
+ end
+
+ describe '#expiration_date' do
+ it 'renders the expiration date in a friendly format' do
+ card.expiration_year = 2025
+ card.expiration_month = 3
+ card.expiration_day = 24
+
+ expect(card.expiration_date).to eq('24/3/2025')
+ end
+ end
end