summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDavid Gay <david@davidgay.org>2021-05-02 20:28:53 -0400
committerDavid Gay <david@davidgay.org>2021-05-02 20:40:37 -0400
commitcd04bcc4b4e4f5174d2f042bfcf6b840e6e18fee (patch)
tree30f869867ff5b120255febe3db47dacd28f432f0
parent2b32a88a8e9f1d0bb5055843ac987d9cb9981803 (diff)
Add Characters
-rw-r--r--app/models/character.rb5
-rw-r--r--app/models/user.rb2
-rw-r--r--db/migrate/20210503002720_create_characters.rb15
-rw-r--r--db/schema.rb19
-rw-r--r--test/fixtures/characters.yml15
-rw-r--r--test/models/character_test.rb7
6 files changed, 62 insertions, 1 deletions
diff --git a/app/models/character.rb b/app/models/character.rb
new file mode 100644
index 0000000..1c022ea
--- /dev/null
+++ b/app/models/character.rb
@@ -0,0 +1,5 @@
+class Character < ApplicationRecord
+ belongs_to :user
+ belongs_to :activity, optional: true
+ validates :name, presence: true
+end
diff --git a/app/models/user.rb b/app/models/user.rb
index 6083f21..59d9ac3 100644
--- a/app/models/user.rb
+++ b/app/models/user.rb
@@ -3,4 +3,6 @@ class User < ApplicationRecord
# :confirmable, :lockable, :timeoutable, :trackable and :omniauthable
devise :database_authenticatable, :registerable, :recoverable, :rememberable, :validatable, :trackable,
:confirmable, :lockable
+ has_many :characters
+ belongs_to :active_character, class_name: "Character", optional: true
end
diff --git a/db/migrate/20210503002720_create_characters.rb b/db/migrate/20210503002720_create_characters.rb
new file mode 100644
index 0000000..1420a0b
--- /dev/null
+++ b/db/migrate/20210503002720_create_characters.rb
@@ -0,0 +1,15 @@
+class CreateCharacters < ActiveRecord::Migration[6.1]
+ def change
+ create_table :characters do |t|
+ t.string :name
+ t.references :user, null: false, foreign_key: true
+ t.references :activity, foreign_key: true
+ t.timestamp :activity_started_at
+ t.timestamp :recent_request_at
+
+ t.timestamps
+ end
+
+ add_reference :users, :active_character, foreign_key: { to_table: :characters }
+ end
+end
diff --git a/db/schema.rb b/db/schema.rb
index 85aa952..9072ac3 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.define(version: 2021_05_03_001859) do
+ActiveRecord::Schema.define(version: 2021_05_03_002720) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@@ -24,6 +24,18 @@ ActiveRecord::Schema.define(version: 2021_05_03_001859) do
t.datetime "updated_at", precision: 6, null: false
end
+ create_table "characters", force: :cascade do |t|
+ t.string "name"
+ t.bigint "user_id", null: false
+ t.bigint "activity_id"
+ t.datetime "activity_started_at"
+ t.datetime "recent_request_at"
+ t.datetime "created_at", precision: 6, null: false
+ t.datetime "updated_at", precision: 6, null: false
+ t.index ["activity_id"], name: "index_characters_on_activity_id"
+ t.index ["user_id"], name: "index_characters_on_user_id"
+ end
+
create_table "items", force: :cascade do |t|
t.string "gid"
t.string "name"
@@ -63,10 +75,15 @@ ActiveRecord::Schema.define(version: 2021_05_03_001859) do
t.datetime "locked_at"
t.datetime "created_at", precision: 6, null: false
t.datetime "updated_at", precision: 6, null: false
+ t.bigint "active_character_id"
+ t.index ["active_character_id"], name: "index_users_on_active_character_id"
t.index ["confirmation_token"], name: "index_users_on_confirmation_token", unique: true
t.index ["email"], name: "index_users_on_email", unique: true
t.index ["reset_password_token"], name: "index_users_on_reset_password_token", unique: true
t.index ["unlock_token"], name: "index_users_on_unlock_token", unique: true
end
+ add_foreign_key "characters", "activities"
+ add_foreign_key "characters", "users"
+ add_foreign_key "users", "characters", column: "active_character_id"
end
diff --git a/test/fixtures/characters.yml b/test/fixtures/characters.yml
new file mode 100644
index 0000000..34def98
--- /dev/null
+++ b/test/fixtures/characters.yml
@@ -0,0 +1,15 @@
+# Read about fixtures at https://api.rubyonrails.org/classes/ActiveRecord/FixtureSet.html
+
+one:
+ name: MyString
+ user: one
+ activity: one
+ activity_started_at: 2021-05-02 20:27:20
+ recent_request_at: 2021-05-02 20:27:20
+
+two:
+ name: MyString
+ user: two
+ activity: two
+ activity_started_at: 2021-05-02 20:27:20
+ recent_request_at: 2021-05-02 20:27:20
diff --git a/test/models/character_test.rb b/test/models/character_test.rb
new file mode 100644
index 0000000..edf3fac
--- /dev/null
+++ b/test/models/character_test.rb
@@ -0,0 +1,7 @@
+require "test_helper"
+
+class CharacterTest < ActiveSupport::TestCase
+ # test "the truth" do
+ # assert true
+ # end
+end